A pointer to an array that initializes an array, using the traditional way of traversing
1 voidMain ()2 {3 inta[5] = {1,2,3,4,5 };4 for(inti =0; I <5; i++)5 {6printf"%d,%x\n", A[i], &a[i]);7printf"%d,%x\n", * (a+i), a+i);//equivalent a[i], &a[i]8 }9}
A is the first address of the array A, that is, the address of element 1, a + 1 for address +1, that is, to increase the length of int 4 bytes, point to the address of the next address is element 2
1 int a[51,2,3,4,5 }; 2 int // Create a null pointer 3 p = A; // p is a pointer variable and a is the address of the array's first ADDRESS element 1
Because we use the pointer traversal to achieve the same effect.
1 // using the pointer loop 2 for (int *pp = A; pp<a+5; pp++) // address + + 3 {4 printf ("%d,%x\n", *pp, pp); 5 }
With the above understanding, the pointer structure is actually the same.
1 //pointer structure Body2 structInfo3 {4 Charname[ -];5 intID;6 };7 8 voidMain ()9 {Ten structInfo myinfo[5] = { {"ZC1",1},{"ZC2",2}, {"ZC3",3}, {"ZC3",3}, {"ZC4",4} }; One A //Normal Mode output - for(inti =0; I <5; i++) - { theprintf"%s,%d\n", Myinfo[i].name, myinfo[i].id); - //Pointers -printf"%s,%d\n", (* (Myinfo+i)). Name, (* (MyInfo +i)). ID); - } + - //using the pointer loop + structInfo *px =MyInfo; A for(; px < MyInfo; px++) at { -printf"%s,%d\n", Px->name, Px->id);//pointer structure output mode pointer variable-Properties -printf"%s,%d\n", (*PX). Name, (*PX). ID);//pointer structure output mode * pointer variable. Properties - //px->id equivalent (*PX). ID - } - in GetChar (); - to}
Pointers to C language pointers arrays and struct bodies