Speaking of the pointer to the C language, it is estimated that the C language may be a headache for the comrades who only know little about it, especially when it is connected to the array shamelessly, it is even more lewd !!!
There is a rule: (or criterion)
The array name can be viewed as a pointer to an array Header element. However, the array name is not a pointer (values cannot be assigned to each other ). However, it can be used as a pointer. However, pointers cannot be used as array names.
In the C language's ancestor book K & R, there is such a statement: only when used as a function parameter
Int a [];
Int * a; has the same meaning. This must be understood.
Example: char str [] = "abc ";
Str [0] = 'D'; ----- writable
Char * str = "abc ";
Str [0] = 'D'; ----- An error is reported.
Next, focus on understanding the relationship between two-dimensional arrays and pointers (Note: There is no such thing as a two-dimensional array, the standard name is an array)
Code:
# Include <stdio. h>
Int main ()
{
Char * color [] = {"red", "green", "blue "};
Char colors [] [6] = {"red", "green", "blue "};
Printf ("% p \ n", color );
Printf ("% p \ n", * color + 1 );
Printf ("% p \ n", * color + 2 );
Printf ("% s \ n", * color );
Printf ("% s \ n", color [0]);
Printf ("% c \ n", color [0] [0]);
Printf ("% c \ n", * (* color + 1 ));
Printf ("% c \ n", (* color) [2]);
Printf ("% c \ n", * color [2]);
Printf ("% c \ n", * & (* (color + 1) [4]); // jiu zhe me xiao zhang
Printf ("% c \ n", * (color + 1) + 4 ));
Printf ("% d \ n", & (* (color + 1) [4]);
Printf ("% c \ n", ** color );
Printf ("array \ n"); printf ("% s \ n", * color );
Printf ("% c \ n", ** (colors + 1 ));
Printf ("% s \ n", color [0]);
Printf ("% c \ n", colors [0] [0]);
Printf ("string \ n"); char * str = "abc ";
Printf ("% c \ n", * str );
Printf ("% c \ n", * str + 2 );
Printf ("% c \ n", * (str + 1 ));
Printf ("% s, % d \ n", colors [0], sizeof (colors [0]);
Return 0;
}
Analysis one by one:
Color is the pointer to the first element of the first array. It is also the array name. The first array is actually color [3] [5].
Then * color is red. color points to red and adds a * sign to indicate unreferencing and printing the actual object value;
Color [0] is obviously the first element, which is also red;
Color [0] [0] the first element of red, that is, r;
** Color is equivalent to the previous one. * color is "red", and the string is a char array. Therefore, it is equivalent to * color is a pointer to the char [] array, add another *, and then dereference it to r;
Char [] = {'R', 'E', 'D', '\ 0 '}
Similarly, * (* color + 1) is the second letter e;
Because * color is "red", * color is equivalent to the array name of char []. So (* color) [1], it indicates the second letter of the char [] array, that is, e;
Note: The char [] array I am talking about is char [] = {'R', 'E', 'D', '\ 0 ~
Next, let's make a difference:
(* Color) [1] Output e, that is, e in red
* Color [1] Output g, that is, g of green;
Because * color indicates an array name.
However, because this expression is from the right to the left, if there is no (), color [1] is first generated, that is, the string "green ", the string "green" can also be considered as a char [] array, so color [1], at this time, another role is char [] = {'G', 'R ', the array names of 'E', 'E', 'n', '\ 0'} are now available. Therefore, if you add *, the output is g.
Likewise:
(* (Color + 1) [4]
* (Color + 1) + 4) is equivalent.
Let's take a look at the colors array. Its declaration does not contain the * number. How can it contact? Of course, there is only one: the array name can be seen as a pointer to the first element of the array.
For example, colors [0]
* Colors is equivalent. colors can be regarded as a pointer to "red" and is referenced by the asterisk.
For example, colors [1] [0]
** (Colors + 1) is equivalent. In this example, * (colors + 1) is "green", plus *, indicating the first element of "green", that is, g.
Program running:
Welcome to the discussion ~~