Arrays and pointers
array
Array: a collection of elements of the same type
Array declaration
//Some array declarations #include<stdio.h> int main() { int a[10]; //Contains 10 int type elements from a[0] to a [9], excluding a[10] float f[20]; //20 elements of float type double d[20]; //20 elements of type double return 0; }
Initialization of array
int a[10]={0,1,2,3,4,5,6,7,8,9};//Each element is separated by a comma Assign 0 to a The first element of the array a[0],1 Endow a[1]And so on, assign 9 to a[9]; int a[]={0,1,2,3,4,5,6,7,8,9}; In the definition initialization, you can also directly assign values in the back without adding numbers in square brackets. The computer automatically matches the array size according to the number of elements in the back. Partial initialization int a[10]={0,1,2,3,4,5}; This sentence only initializes a[0]reach a[5]Six elements, and all the remaining elements will be initialized to 0 stay C The element to be initialized can also be specified in int a[10]={[5]=11}; That's it a[5]Initialized to 11;
Multidimensional array
Two dimensional array int year[2][12]={ {1,2,3,4,5,6,7,8,9,10,11,12} , {1,2,3,4,5,6,7,8,9,10,11,12} }; There are 12 months in a year and 24 months in two years The main array has two array elements, and each array element contains 12 elements
A one-dimensional array is equivalent to writing a line of words
A two-dimensional array is equivalent to writing a few lines of words
A three-dimensional array is equivalent to writing several words. a[2][5][10] this is equivalent to writing two, and then each one writes 5 lines, and each line writes 10 words.
And so on, there are four-dimensional arrays, five-dimensional
Arrays and pointers
The array name is the address of the first element of the array
If a is an array, then a = = & A [0]
Array as function parameter
An array is passed into the function as a function parameter, which is equivalent to an address
Array a [] in the function parameter table is actually a pointer. After replacing a [] with * a, the program can still run normally
In the function parameter table, int a [] actually declares that a is a pointer of type int, and a is also an array element of type int.
The array name is the address of the first element of the array
If we pass in an X to the function, it is equivalent to passing in the address of the first element x[0] of an array
You can also change the program. Let's pass an address directly to the function
The program is still running normally. Does this prove that the formal parameter int a [] allows us to pass in an address (pointer, a[]==*a) to the function.
//test #include<stdio.h> void test(int a[]); int main() { int x[10] = { 0,1,2,3,4,5,6,7,8,9 }; printf("sizeof(x)=%d\n", sizeof(x)); printf("&x=%p\n", &x); printf("x=%p", x); test(&x[0]); printf("x[0]=%d\n", x[0]); return 0; } void test(int a[]) { printf("sizeof(a)=%d\n", sizeof(a)); a[0] = 112; printf("%p\n", &a[0]); }
The following four function prototypes are equivalent
int test(int *a); int test(int a[]); Because the function prototype can omit the parameter name, we can also write it like this int test(int *); int test(int []); However, the parameter name must be added to the function definition and cannot be omitted. Otherwise, who can save the passed in value! Therefore, we have to use the following two functions for function definition: int test(int *a); int test(int a[]);
Pointer
A pointer is an address
int a; int *p; //Declaration of pointer p=&a; //Initialization operator & is used to get the address *p=12 Indirect reference of pointer *p==a *p=12 => a=12 Indirect reference of pointer is also called dereference,* Operator gives the value stored at the address pointed to by the pointer
####Field pointer
There is no pointer to a valid address space, that is, an uninitialized pointer
int *p;//Uninitialized pointer *p=10;//Error p is a random value. We don't know where p will point. It may not make an error, but it may also point to a special place to crash the program.
####Null pointer
To avoid using uninitialized pointers, we define a NULL (NULL==0) so that the pointer points to an invalid access area when declared.
int *p=NULL;
The storage space pointed to by p must be an invalid access area.
####Pointer as function parameter
#include<stdio.h> int sum(int *x,int y); int main() { int a=10; int b=2; sum(*a,b); printf("%d",a); return 0; } int sum(int *x,int y) { return *x=*x+y; }
Is to pass in an address to the function.
####Pointer operation
**Pointer plus an integer n**
Adding an integer n to the pointer is equivalent to adding an n * sizeof (type name) to the pointer
Increment pointers p + +, + p Plus a sizeof (type name)
Pointer minus an integer n
Subtracting an integer n from the pointer is equivalent to subtracting an n * sizeof (type name) from the pointer
Decrement pointers p --, – p subtract a sizeof (type name)
Pointer subtraction
In the same array, two pointers point to different elements. Pointer difference can be used to calculate the distance between two elements.
Pointer and const
The pointer is const
The address pointed to by the pointer cannot be modified. It can only be the address obtained for the first time, but the variable pointed to by the pointer can be modified.
The variable pointed to is const
The address pointed to by the pointer can be modified, but the value of the variable cannot be modified through the pointer. The variable pointed to by the pointer will not become const (unless the variable itself is const), but the variable cannot be modified through the pointer. The object of const is always the pointer.
Arrays and pointers
Array variables are special pointers
The array variable itself represents the address
a==&a[0]
The [] operator can be applied to arrays or pointers.
For example:
*Operators can be used on pointers or arrays.
For example:
#####Pointers and 2D arrays
//A two-dimensional array is equivalent to an array of arrays int a[4][2] = { { 7, 11}, { 12, 31},{ 4, 3},{ 2, 1} }; int (*p)[2]=a; //Array pointer (representing a two-dimensional array) p The address of the first array element of a two-dimensional array => &a[0] p+2 The address of the third array element of a two-dimensional array => &a[2] *(p+2) The address of the first element of the third array element of a two-dimensional array => &a[2][0]=4 *(p+2)+1 The address of the second element of the third array element of the two-dimensional array => &a[1] *(*(p+2)+1) The value of the second element of the third array element of a two-dimensional array => a[2][1]=3