Xiao Bai's basic summary of the C language array, and Xiao Bai's C language Array
Array
An array is a set of ordered elements of the same type.
Array definition:
The general format is:Type character array name [constant expression]For example, int a [5]; indicates that the array has five elements, a [0]-a [5], and element a [5] does not exist.
* Note: 1. Constant expressions can contain constants and symbolic constants, for example, "int a [3 + 5];"
2. The C language does not allow dynamic definition of the array size.
// For example, the following definitions of arrays are invalid:
Int n;
Scanf ("% d", & n );
Int a [n];
// An error is reported when this code is compiled in Visual c ++, but not in dev c ++ and gcc. It can run normally!
// The length of an array defined in the called function can be a variable or a non-variable expression, for example:
Viod fun (int n)
{
Int a [n * 2];
}
Array initialization:
By reading books and searching for materials, the following initialization methods are collected:
Int a [5] = {0, 1, 2, 3, 4 };
// A [0] = 0, a [1] = 1, a [2] = 2, a [3] = 3, a [4] = 4
Char ch []
Int a [5] = {0, 1}; // only assign values to some elements
// A [0] = 0, a [1] = 1, a [2] = 0 a [3] = 0 a [4] = 0
Int a [5] = {0}; // all elements are assigned 0;
// A [0] = 0, a [1] = 0 a [2] = 0 a [3] = 0 a [4] = 0
Int a [] = {0.1.2.3.4} // knows the number of elements and does not specify the array length.
// A [0] = 0, a [1] = 1, a [2] = 2, a [3] = 3, a [4] = 4
Char ch [5] = {"hello "};
Char ch [5] = "hello ";
Char ch [10] = {'h', 'E', 'l', 'l', '\ 0 '};
// Do not forget to allocate space for the last '\ 0. If you want to initialize a string "hello", the array defined for it has at least six array elements, but if you forget it, Some compilers will automatically add
Int a [5];
For (I = 0; I <5; I ++)
{
A [I] = I;
} // Use the for loop assignment
// Use memset to assign the specified ASCLL value to the array
Char a [10];
Memset (a, 0, strlen (a); // All values are 0.
Usage: void * memset (void * s, int ch, int size_t n)
Array size and length:
Sizeof (data type) * Number of Elements
Sizeof (array name)
Int a [5];
Sizeof (a); // At this time, a represents the array name, rather than the first address of the array, so the size is 20, not 4;
The malloc function dynamically allocates the array length.
Int main ()
{Int len;
Printf ("input allocated array length: len = ");
Scanf ("% d, & len ");
Int * pArr = (int *) malloc (sizeof (int) * len );
* PArr = 4; // similar to a [0] = 4;
PArr [1] = 10; // array usage similar to a [1] = 10, pointer
* (PArr + 2) = 20; // assign a value through the + 1 Operation
Printf ("% d", * pArr, pArr [1], * (pArr + 2 ));
Free (pArr );
Return 0;
}
When len = 5, the malloc function is used to allocate 20 bytes and convert them to an int address. The malloc function returns only the first byte address, obtain the following address through the + 1 operation.
Use free (pArr) to release memory. Array address: 1. a and &
Although a = & a looks equal in values, it also represents the first address of the array. We can understand their differences through the + 1 operation.
int a [10] = {0,1,2,3,4,5,6,7,8,9};
Printf ("a [0] _size =% d \ n", sizeof (a [0])); // size of array elements
Printf ("a_size =% d \ n", sizeof (a)); // The size of the array
Printf ("a =% p \ n", a);
Printf ("a + 1 =% p \ n", a + 1);
Printf ("& a =% p \ n", & a);
Printf ("& a + 1 =% p \ n", & a + 1);
return 0;
A: Its type is int *. Therefore, the step size of its + 1 is the number of bytes of the array element size, that is, four bytes. a + 1 is the address of a [1.
& A: the number of its types int * [10]. Therefore, the step size of its + 1 is the number of bytes in the array size, that is, 40 bytes, & a + 1 is the address of a [10] (a [10] has crossed the border ).
2. a [0] And & a [0]
int a[10]={0,1,2,3,4,5,6,7,8,9};
printf("a[0]_sizeof=%d\n",sizeof(a[0])};
printf("a[0]=%d\n",a[0]);
printf("a[0]+1=%d\n",a[0]+1);
printf("&a[0]=%p\n",&a[0]);
printf("&a[0]+1=%p\n",&a[0]+1);
A [0]: Elements in the array. The value is 0. a [0] + 1 = 1.
& A [0]: Address of the first element of the array. Its value is the same as that of & a. Its type is int *, therefore, the step of "+ 1" is the number of bytes in the array element size, that is, four bytes. & a [0] + 1 is the address of a [1.
Array usage
1. array form
2. pointer form
Int a [5];
A [0] = 0; // array format
* (A + 1) = 1; // pointer format
The Assembly Process for modifying the array content:
1. Find the first address through the array name;
2. Locate the address of the element to be modified based on the address offset.
3. modify content
I hope you can give me more information.