arrays and pointers

protect data in array

The pointer is only passed when the program needs to change the value in the function. For arrays, pointers must be passed because this is efficient. If the function passes an array by value, it must allocate enough memory to store a copy of the original array, and then copy all the data in the original array into the new array. Of course, if the address of the array is passed to the function, the function will It can directly deal with the original array efficiency just

However, in the process of passing the address, it will cause some problems. The C language usually passes data by value, because doing so can ensure the integrity of the data, the function uses a copy of the original data, and the original data will not be accidentally modified. , sometimes we need to modify the data in the array, but sometimes we don't, so how to protect the data in the array?


use const for formal parameters

int sum(const int arr[],int n);		//函数原型
int sum(const int arr[],int n)		//函数定义
{
    
    
……
}

const informs the compiler that the function cannot modify the contents of the array pointed to by arr, it can only be read, not written. If a write operation like arr[i]+1 is used, the compiler will catch this error and generate a The use of const in the error message
does not require that the data in the original array be constant, but after adding const, it is treated as a constant when processing the array and cannot be changed, which protects the data in the array from being modified, just like pressing Passing by value can protect the original value of the data type from being changed. Generally speaking, if we write a function that does not modify the array, it is best to use const when declaring the array parameters.


Pointers and Multidimensional Arrays

int a[4][2];//内含int数组的数组,即二维数组

The array name ais the address of the first element of the array, and athe first element is an array containing two int values, so it ais the address of the array of these two int values.

Because it ais the address of the first element of the array, athe value and &a[0]the value are the same. It a[0]is an address that occupies one int-sized object, abut occupies two int-sized objects, because they both start at the same address, so aand a[0]the same value

Add 1 to a pointer or address, and its value will increase the value of the corresponding type size, but pay attention to the following differences:
a[0]it is an address that occupies an int-sized object, abut occupies two int-sized objects. Therefore, the a+1sum a[0]+1of different values

Dereference a pointer (*) or use the subscripted [] operator after the array name to get the value represented by the referenced object

Pointer is the essence of C language. It is the hardest, most complicated, and most powerful in C, because it involves the basic knowledge of computer. Come on!

#include<stdio.h>
int main()
{
    
    
    int a[4][2] = {
    
    {
    
    2,4},{
    
    6,8},{
    
    1,3},{
    
    5,7}};
    printf("a=%p  a+1=%p\n",a,a+1);
    printf("a[0]=%p  a[0]+1=%p\n",a[0],a[0]+1);
    printf("*a=%p  *a+1=%p\n",*a,*a+1);
    printf("a[0][0]=%d\n",a[0][0]);   
    printf("*a[0]=%d\n",*a[0]);  
    printf("**a=%d\n",**a);
    printf("a[2][1]=%d\n",a[2][1]);
    printf("*(*(a+2)+1)=%d\n",*(*(a+2)+1));
    return 0;
}

insert image description here


variable length array

C allows variables to represent the dimensions of an array

int a=3;
int b=5;
double c[a][b];		//一个变长数组

Variable-length arrays have some restrictions. Variable-length arrays must be of automatic storage type, and cannot use static and extern storage class specifiers.
Note: Variable-length arrays cannot change their size. Change in variable-length arrays does not mean modifying the size of the created array. Once a variable-length array is created, its size remains the same. What has changed here is that you can use variables to specify the dimensions of the array

int a(int i,int j,int c[i][j]);		//a是一个变长数组
int a(int c[i][j],int i,int j);		//无效的顺序

insert image description here
After the lightning storm, it must be a world full of blue sky and white clouds, full of green grass and flowers

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324320409&siteId=291194637