1. The concept of arrays
(1) An array is an ordered set of variables of the same type
(2) array to store elements in a contiguous memory space
(3) The number of array elements can be displayed or implicitly specified
#include <stdio.h>intMain () {//Array Initialization inta[5] = {1,2};//1th, 2 elements are 1, 2, the remainder is 0 intB[] = {1,2};//During compilation, the compilation will be the size of the computed array b intI=0; for(i =0; I <4; i++) {printf ("a[%d] =%d\n", I,a[i]); } printf ("sizeof (a) =%d\n",sizeof(a));// -printf"sizeof (b) =%d\n",sizeof(b));//8printf"count for a:%d\n",sizeof(a)/sizeof(int));//5printf"count for B:%d\n",sizeof(b)/sizeof(int));//2 return 0;}
2.Array Address (&a) and array name a
(1) array name a represents the address of the array Therefore, the first 2 a+1 ... a or a+i Span class= "FONTSTYLE0" > represents the address of an element. Can be used * (a+i Remove the value of the element, or you can use a[i]
* (a+i) 1 a &a[0], 2 a+1 &a[1]i a+i
(2) The address of the array Span class= "FONTSTYLE0" > need to use & To get. The shape is like &a &a+1 Indicates the position of the last face pointing to the entire array.
(3 first element address value and Span class= "FONTSTYLE0" > The address value of the array two different concepts /span>
Array names and arrays addresses
#include <stdio.h>intMain ()
{ //initializes each element of the array to 0 inta[5] = {0};//meaning, initialize the 1th element to 0 and the remainder to 0.printf"A =%p\n", a);//address of the first elementprintf"&a =%p\n", &a);//The address of the entire array, in numerical terms, is the same as a. printf"&a[0] =%p\n", &a[0]);//address of the 1th element return 0;}
3.The blind spot of the array name
(1) The connotation of the array name is that its reference entity is a data structure, and this data structure is an array. such as int a[5 ] shows the type of a is int[5], so sizeof (a) Represents the size of the entire array,&a represents the address of the array.
(2) Extension of array name: In addition to sizeof (a) and &a, the array name can often be viewed as a constant pointer. Be aware, however, that this is only "considered", not a real pointer. Unlike pointers, the array name is just a symbol in the compilation process, and the compiler does not allocate memory for it, some call it " pseudo-variable ". Therefore, the form a++\a-or a=b (where B is another array name) These are all wrong, because a is just a symbol, the compiler will put the array information (such as size, address) into the symbol table, each time you encounter the array name A, the address of the array will be taken from the symbol table, and then use this fixed address instead of a, So this symbol is not allocated memory space, and the above operations are for variables, so the array name can only be used as the right value.
(3) A reference to an array, such as a[i] or * (a+i), simply accesses the memory once, and the pointer reference such as * (P+i) takes two times, preferring to find the P pointer by &p, then add I, and then remove the contents from the P+i.
(4) When the array name is a formal parameter, it is degraded to a pointer. That is, you can use the array name as a pointer, where sizeof (the array name) is 4, which is the length of the pointer.
Arrays and pointers are not the same
#include <stdio.h>intMain () {//initializes each element of the array to 0 inta[5] = {0}; intb[2]; int* p =NULL; P=A; printf ("A =%p\n", a);//address of the first elementprintf"p =%p\n", p);//P==a. printf"&p =%p\n", &p);//the address of the pointer pprintf"sizeof (a) =%d\n",sizeof(a));//size of the array:printf"sizeof (P) =%d\n",sizeof(p));//the size of the pointer is 4.printf"\ n"); P=b; printf ("B =%p\n", b);//address of the first elementprintf"p =%p\n", p);//p==b. printf"&p =%p\n", &p);//the address of the pointer pprintf"sizeof (b) =%d\n",sizeof(b));//size of the array: 8printf"sizeof (P) =%d\n",sizeof(p));//the size of the pointer is 4. //a = b;//compile error, array name cannot be left value; //a++;//compile error, array name is compiled with a fixed address, quite 0xaabbccdd++ error return 0;}
The array name is not a pointer, and it cannot be equated to a pointer.
C Language Learning notes--arrays