Today, we analyze the basic understanding of the C language two-dimensional arrays and pointers, and feel a bit ignorant ... Code to record, if there is a great God fortunately found where the wrong, welcome correction ~ ~ ~
#include <stdio.h>#include<stdlib.h>#include<string.h>//void func (int p[][])//this is equivalent to void func (int **p) p++ moved four bytes, (*p) + + moved four bytes, does not conform to the two-dimensional array rule//{//}//function for column-first output (i.e., vertical output)voidFuncintp[][4],intAintb//This is correct, so the write is equivalent to void func (int (*p) [4]) p++ moves 16 bytes, (*p) + + moves 4 bytes, A and B are to maintain the dimension, A is the column, B represents the row{ for(inti =0; i < A; i++) { for(intj =0; J < b; J + +) {printf ("%d\n", * (* (P + j) +i)); } }}intMain () {intarray[3][4] = { {0,1,2,3}, {4,5,6,7}, {8,9,Ten, One } }; //printf ("%d\n", sizeof (array[0])); //The result is 16, mom, Array[0] unexpectedly is not a pointer, but an array of names,//printf ("%d\n", sizeof (array)); //Results func (array,
sizeof(array[0])/ sizeof(int), sizeof(array)/ sizeof (array[0])); //
The second argument is the number of columns, the third argument is the number of rows // Array[0] is not a variable, it is the first address of the No. 0 row in a two-dimensional array
// and the following: * (p + 0) + 1; representing int * Backward displacement of a position should be the same in principle. // In a two-dimensional array, *array represents the first address of the array, whereas *array in a one-dimensional array represents the value of the first element. // printf ("%d %d %d\n", array, *array, &array, &array[0][0], array[0]); //
printf ("%d\n", array + 1); //
printf ("%d\n", Array[0] + 1); // the result for array + 1 moved 16 bytes // array[0] + 1 moved 4 bytes // It can be understood that array + 1 as the Commander, Array[0] + 1 as a platoon leader, the company commander each walk a row (that is, walk 4 number, 14 bytes), the platoon leader each walk a column (that is, walk a number, 4 bytes)//int *p = Array; // Pointer to a one-dimensional array
//Int (*p) [4];
is a disguised level two pointer //p = array; //* (p + 0) + 1; Represents an int * backward displacement of a position//for (int i = 0; i < 4; i++)//{ //For (int j = 0; J < 3; j + +)// { //printf ("%d\n", * (* (P + j) + i)); // } //} //* (p + 0) represents a pointer to the first line. So * (p + 0) + 1 is the position of the second element in the first line//printf ("---------------\ n"); //----------- //p + 1; //It's a platoon leader, one line at a time .//P[0][0] = * (* (p + 0) + 0); //on behalf of the company commander, one at a time.//printf ("%d\n", * (* (p+1))); //No. 0 Element of the first rowSystem ("Pause"); return 0;}
The analysis is as follows:
Yellow Code Part : The address of the output is exactly the same, so the difference between a two-dimensional array and a one-dimensional array is that *array represents the first address of the array in a two-dimensional array, whereas *array in a one-dimensional array represents the value of the first element
Red Code part: relative to the yellow code part, the Red Code result is: array + 1 moved 16 bytes, array[0] + 1 moved 4 bytes;
Because array is the first address of an array, and is a two-dimensional array, array+1 should be moving one line, that is, 4 numbers, array[0] is the first address of the "first row array" of a two-dimensional array, so +1 should be moving the pointer inside the first row array;
(if int *p = &i; Then the result of P+1 will move 4 bytes, if char *p = &i; then p+1 result will move 1 bytes, here is actually the same reason, just not good understanding ... )
Can also use the platoon leader and the company commander's example to understand, the array as the company commander, the Array[0] as a platoon leader, the company commander each review, will be a line of walk, the platoon to review, will row a row of walk, so Array (company Commander) Walk 16 bytes, array[0] each walk 4 bytes.
Orange Code section: when defining a level two pointer, it is generally defined,int (*p) [4];
Here is the concept of a "pointer array" and an "array pointer", with the following diagram and code:
pointer Array: Array pointer
int *p[5]; int (*p) [10];
#include <stdio.h>#include<stdlib.h>intMain () {//Array of pointers Char*s[Ten];//is an array that has 10 char * pointers, each pointing to one characterprintf"%d\n",sizeof(s[0]));//4//Array Pointers Char(*S1) [Ten];//is a pointer variable that points to a char[10]; cannot point to any other char[n] arrayprintf"%d\n",sizeof(s1));//4System ("Pause" ); return 0;}
So:Int (*p) [4]; is an array pointer that points to an array of 4 elements,
p = array, that is, the first address of the two-dimensional array is assigned to P,array is also an address (equivalent to a pointer, but if we use sizeof (array) will find actually not a pointer), so p is actually a disguised level two pointer (you do not believe that can use * * P test to see if the first element of the array can be taken.
So * (p + 0) + 1; representing int * Backward displacement of a position is not difficult to understand, P is a two-level pointer, p+0 is actually the first row of the array's first address. * (p+0) is also a pointer, that is, an int * type of variable (pointer is also a variable ), * (p + 0) + 1; represents an int * position in the back
* (p + 0) represents a pointer to the first line. So * (p + 0) + 1 is a pointer to the position of the second element in the first row, * (* (p + 0) + 1) is the value of the second element in the first row;
part of the gray color code: The reason for this is that the dimensions of the two-dimensional array are sent directly, because the receiver function is not aware of the dimensions of the two-dimensional array, and after this writing changes the dimensions of the array, you do not have to change the other code.
C-language two-dimensional array and pointer notes