当前位置:网站首页>Hello C (V) -- pointer and array

Hello C (V) -- pointer and array

2022-06-24 23:42:00 Tianshan old demon

An array is C Language built-in data structure , A thorough understanding of arrays and their usage is the basis for developing efficient applications . Arrays and pointers are closely related , But it is not completely interchangeable .

One 、 Introduction of the array

An array is a continuous collection of elements of the same type that can be accessed by index . The elements of an array are contiguous in memory , There is no gap in the middle , The elements of the array are of the same type .

1、 Array interpretation

Definition of array :int a[10] = {0,1,2,3,4,5};

a[0]: The first element of the array , First element ( When the left value is used, it means the 0 Memory space of elements )

&a: Address of array , Is a constant , You can't be left-handed , Type equivalence int (*)[10]( Array pointer ).

&a[0]: Array number 0 Addresses of elements , And array name a Equivalent

a:a It's an array name , You can't be left-handed , The right value represents the address of the first element of the array , And &a[0] identical .

The address of an array is not the same as the address of the first element of an array .

Array names can be thought of as const The pointer , But the array name is sizeof The parameters of the operator and & Except for parameters of the operator , Array name is not a pointer .

An array is essentially a contiguous memory space .

2、 One dimensional array

One dimensional arrays are linear structures , Access members with an index .

int vector[5] = {1,2,3,4,5};

The internal representation of an array does not contain information about the number of its elements , The array name simply refers to a piece of memory .

3、 Two dimensional array

Two dimensional arrays use rows and columns to identify array elements , A two-dimensional array can be regarded as an array of arrays .

int vetor[2][3] = {
   {1,2,3},{4,5,6}};

The array name of a two-dimensional array is equivalent to the array pointer int (*vector)[3];

Two 、 Array and pointer representations

1、 How to access array elements

A、 Array subscript access

Array name [ Index subscript ];

a[i] <==>i[a]

B、 Pointer access

*( The pointer + Offset );

*(a + 2);// Equivalent to a[2]

The address of the element in the array is continuous .

C、 Pointer operation of array name

a + n ==> p + n*sizeof(*a)

&a + 1 ==> &a + sizeof(a)

C The language does not enforce the bounds of arrays , Therefore, accessing an array with an invalid index will cause the array access to be out of bounds , Cause unexpected behavior .

2、 The difference between array and pointer

int vector[5] = {1,2,3,4,5};
int *p = vector;

vector[i] The way the array elements are accessed represents the slave address vector Start , Move i Take out the contents in places

*(vetcor+i) Accessing array elements means that from vector Start , Add... To the address i, Take out the contents of the address .

sizeof(vector) The result is the number of bytes allocated by the array

sizeof(p) What you get is a pointer variable p The length of

3、 ... and 、 Arrays as function arguments

1、 Pass a one-dimensional array

Passing a one-dimensional array as a parameter to a function actually passes the address of the array by value , There is no need to pass the entire array , There is no need to allocate space on the stack , Usually you also need to pass the array length , Make sure that the access to the array does not cross the bounds .

There are two ways to declare an array in a function declaration :

A、 Array representation

void display(int a[], int size);

By using sizeof(a) It is wrong to count the number of elements of an array ,sizeof(a)/sizeof(int) That's right. .

B、 Pointer representation

void display(int *a, int size);

When an array is a function parameter , Arrays degenerate into pointers .

2、 Pass multidimensional array

When passing a multidimensional array , You need to decide whether to use array representation or pointer representation in the function prototype declaration , And the dimension of the array and the size of each dimension . To use array notation inside a function , The form of the array must be specified , Otherwise the compiler will not be able to use subscripts .

void display(int a[][5], int rows);
void display(int (*a)[5], int rows);
void display(int *a[5], int rows);// Error declaration , Grammar is right , But the compiler will think that the array passed in by the function has 5 An integer pointer .

When passing an array of more than two dimensions , In addition to the first dimension , You need to specify the length of other dimensions .

When a two-dimensional array is used as a function parameter , A two-dimensional array degenerates into an array pointer .

When a pointer array is used as a function parameter , Pointer arrays degenerate into two-dimensional pointers .

Four 、 Array pointer

1、 An array type

C Arrays in the language have their own specific types , The type of array is determined by the number of array elements and the size of the array .

int array[n] The array type of is int[n];

C Language can be used to typedef Rename the array type .

typedef type (name)[size];name Is an array type

The code example is as follows :

#include <stdio.h>
typedef int (array)[5];

int main(int argc, char *argv[])
{
    array a;
    int i;
    for(i = 0; i < sizeof(a) / sizeof(int); i++)
    {   
        a[i] = i;
        printf("%d\n", a[i]);
    }   
    return 0;
}

2、 Array pointer

An array pointer is a pointer to an array .

int (*p)[5];// Array pointer

Array pointers are pointer variables that point to arrays , The array pointer holds the address of the array , Equivalent to a secondary pointer .

int a[5];

p = &a;//&a Is the address of the array , Same as array pointer type

Array pointers can be defined by array types , It can also be defined directly .

Pointer definition of array type :

typedef int (array)[5];

array *a;

Direct definition of array pointer :

type (*array)[n];

Definition of array pointer type :

typedef type (*ArrayPointer)[n];

Operation of array pointer :

array + 1 ===> array + sizeof(*array) ==>array + sizeof(int[5])

Code instance :

#include <stdio.h>

typedef int (*ArrayPointer)[5];

int main(int argc, char *argv[])
{
    ArrayPointer pa;
    int (*p)[5];
    int a[5] = {1,2,3,4,5};
    pa = &a;
    p = &a;
    int i;
    for(i = 0; i < sizeof(a) / sizeof(int); i++)
    {   
        printf("%d\n", a[i]);
        (*pa)[i] = 10;
        printf("%d\n", a[i]);
        (*p)[i] = i+1;
        printf("%d\n", a[i]);
    }   

    return 0;
}

3、 Pointer array

A pointer array is an array whose element type is a pointer , The definition of pointer array is as follows :

type * array[n];

Array array There is n Elements , Each element store type * The pointer .

int *vector[5];// Pointer array , The elements in the array are pointer variables

Code instance :

#include <stdio.h>



int main(int argc, char *argv[])
{
    const char *keyword[] =
    {   
        "do",
        "for",
        "return",
        "while",
        NULL
    };  

    unsigned int i = 0;
    while(keyword[i] != NULL)
    {   
        printf("%s\n", keyword[i++]);
    }   

    return 0;
}

The memory allocation of pointer array and array pointer is shown in the following figure :

原网站

版权声明
本文为[Tianshan old demon]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202211118041337.html