C array alternative expression
Hedgehog @ http://blog.csdn.net/littlehedgehog
StraightLet's see the code:
# Define ACC_MODE (x) ("/000/004/002/006" [(x) & O_ACCMODE])(From Linux/fs/namei. c)
This is a depressing macro. Have I ever questioned whether such writing is in C? Well, to be honest, the array in C is actually written in this way, but we rarely use it at ordinary times.
Recall, for example, array,A [B]Where a is the base address, and B in [] is the offset. The elements in the array depend onIndex = base + offset(The base address and offset are both values and there is no difference, so the location can be exchanged, and the index is the memory address ). So the above form is a special case.The first element is the base address, and the second element is expanded with [], so it is the offset.. The value of a constant string is its first address, and the address is also a value (C language is just a value ).
Let's look back at the macro ACC_MODE:
# Define ACC_MODE (x) ("/000/004/002/006" [(x) & O_ACCMODE])
This macro "" contains a string of four elements-he isBase Address, The value expression in the following [] is the offset, which can be regarded as a generalized array. Therefore, you can index the value of the base address based on the offset.
So what does the previous string mean? The following is an excerpt from oldlinux.org:
"" The character string is a sequence composed of a single character (Note: it must be a character, otherwise it is not called a string) there are two types of characters: 7-bit ASCII code and 8-bit ASCII code (here we will not consider the unicode wide character). No matter which form you use, the maximum value of the character cannot exceed eight characters, therefore, the maximum decimal value of a character is 255
The common characters are expressed in a similar form like 'A'. In fact, each character in the computer corresponds to a [0-255] decimal number. Therefore, since the fundamental meaning of a character is a number if you use a number to represent the character, you must remove '/' from the string "/004/002/006/377", that is, "004002006377"Then, the computer will treat each number as a character. That is, 0 is supposed to be 0x0, but the computer regards it as '0' (ASCII code: 0x40)
To represent the ASCII value of a real character, the C language uses the '/' Escape Character in syntax. By default, the line is represented by octal characters (1-3 bits, that is, the value range is/0 --/377.
However, there is another character that represents the hexadecimal value of the character in the form of/x33 (1-2 bits, range:/x0 --/xff) in the/xhh format.
The visibility of characters can be divided into two types: printable characters and non-printable characters. Basically, non-printable characters are control characters. The ASCII value 0-31 128-159 is a control character.
In the preceding exampleSome characters are invisible characters (that is, control characters). Because these characters are invisible, you can only use their ASCII values or special symbols (such as line breaks/n) to represent them.