1. Array initialization:
The number of array elements can be displayed or implicitly specified
int main () { int a[10] = {1,2};//Other uninitialized elements, compiler defaults to help you initialize to 0 int b[] = {1, 2};//compiler implicitly specifies a length of two elements int c[20] = {0}; for (i=0; i<10; i++) { printf ("%d", a[i]); } memset (A, 0, sizeof (a)); GetChar (); } |
2, the array name understanding difficulties
int a[10]={1,2}; printf ("&a:%d,a:%d\n", &a,a); printf ("&a+1:%d,a+1:%d\n", &a+1, a+1); |
Array first element address and array address: Array name A represents the first address of the element, and &a represents the array address.
Array first element address = array address that is: a=&a
&a+1 represents the addition of an array length sizeof (a), a+1 represents the addition of an array element length sizeof (a[0))
A one-dimensional array name is a pointer constant
The two-dimensional array name is an array pointer, and the multidimensional array name is also an array pointer, a pointer to a low dimensional array
Char array[10][30]; (array+i)//equivalent to the first address of line I//Level two pointer (* (Array+i))//one-dimensional array of first address//level pointers * (array+i) +j//equivalent to the address in column J of line I &array[i][j] * (* (array+i) +j)//equivalent to the address in column J of line I array[i][j] |
6, multidimensional array in the physical memory is linear storage, but the compiler to help us optimize the
2. The three major problems of the beginner's array type
Array type
Array pointers: An array is a pointer to a pointer to an array (the relationship of an array type and a set of pointers)
Array of pointers: pointers are array elements, which are stored in array elements are pointers
1. Array type
The array in C language has its own specific type, and the type of the array is determined by the element type and the size of the group
1. Define an array type: Define a variable with an array
void Main12 () { int i = 0; Define an array type typedef int MYARRAYTYPE[5]; Int Myarraytype myarray; int myarray[5]; for (i=0; i<5; i++) { Myarray[i] = i +1; } for (i=0; i<5; i++) { printf ("%d \ n", Myarray[i]); } System ("pause"); } |
2. Defines an array type, with a pointer to an array type at the top of the array
void Main13 () { int i = 0; typedef int MYARRAYTYPE[5]; Define an array type Myarraytype *parray = NULL; A pointer to an array type was defined Int a[5]={0}; Parray = &a; For (i=0 i<5; i++)//I manipulate the array pointer by the way A[5] this block of memory { (*parray) [I] = i+1; A[i] = i+1; } for (i=0; i<5; i++) { printf ("%d \ n", (*parray) [i]); } System ("pause"); } |
2. Array pointers
Defining array pointers
int i = 0; //This is the definition of a type, which is the type of an array typedef int myarraytype[5];//int //This defines a type, defines a pointer type, defines a pointer type that points to an array .... typedef Int (*myparrtype) [5];//array pointer myparrtype mypoint; // int b[ 5]; < |