"C language notes: arrays and pointers are not the same"

I. Introduction

When you first learn C programming, you will always hear that "arrays and pointers are the same", which is a very dangerous statement. Today, through the study of "C Expert Programming", I will re-understand pointers and arrays, and analyze the reasons why they are "different".

Second, the introduction of the problem

The following code defines the mango variable in define.c, declares and accesses the value of the mango variable in pointer_arry.c.
When the declaration and definition of mango are both pointers or arrays, the program normally accesses the value of one of its elements in the form of array subscripts.
When mango declares it as a pointer and defines it as an array, when the program accesses the value of one of its elements in the form of an array subscript, a segment fault occurs.
So, arrays and pointers are not the same.

#include <stdio.h>

#define CORRECT 0

#if CORRECT

// mango 声明和定义一致均为指针
char test[10] = {
    
    1,2,3,4,5,6,7,8,9,10};
char *mango = test;

#else
// mango 声明和定义不一致,定义为数组,声明为指针  此时程序出现段错误
char mango[10] = {
    
    1,2,3,4,5,6,7,8,9,10};

#endif


#include <stdio.h>

extern char *mango;
// extern char mango[];

int main()
{
    
    
    printf("mango[9] = %d \n",mango[9]);
}

Three, declaration and definition

If you want to know why the program has a segment error when the mango declaration and definition are inconsistent (defined as an array, declared as a pointer), you first need to know what is a declaration and what is a definition.

An object in C language must have one and only one definition, but it can have multiple declarations. The objects mentioned here refer to functions or variables. The extern declaration just tells the compiler the type and name of the variable. Only when the variable is defined, memory is allocated to the variable according to the type of the variable. Just like the above program, in the main file, the variable mango is declared as a pointer type. There is no memory allocation for the variable mango here. It just tells the compiler that there is such a variable, which is defined elsewhere and can be used. In the link Sometimes it will go to other files to find the location of the variable in memory. The variable is defined in define.c, the type of the variable is determined and the memory is allocated.

Fourth, access to arrays and pointers

The compiler assigns each variable (when defined) an address. This address is known at compile time, and the variable is kept at this address at runtime. In contrast, the value stored in a variable is only known at runtime. When the variable a is declared as an array, the address of a is known, and its content can be obtained directly through its address or address + offset value (subscript). When declaring and defining variable a as a pointer, the address of a is known, but this is the address of the pointer. Through this address, only the address of the content it points to can be obtained, and the content itself cannot be obtained directly.

4.1 Array access

insert image description here

4.2 Pointer access

insert image description here

Five, arrays and pointers are not the same

When a variable is declared as a pointer, such as char *a, then the compiler will consider the symbol a to be a pointer, and the object it points to is a character. When accessing a, it will be accessed as a pointer.
When a variable is declared as an array, such as char a[10], then the compiler will consider the symbol a to be an array. When accessing a, it will be accessed as an array.

5.1 When declared as a pointer and defined as an array

When defining variable a as an array, allocate memory to array a and store it at address 1024. When the variable a is declared as a pointer, the compiler will be told that the variable a is a pointer, and it will be accessed in the form of a pointer, and its address is 1024.
insert image description here

5.2 Other differences between arrays and pointers

pointer array
The address where the data is saved save data
Indirect access to data, first obtain the content of the pointer, use it as an address, and then fetch data from this address. If the pointer has a subscript i, add i to the content of the pointer as the address, and then fetch data from it. For direct access to data, a[i] simply uses a+i as the address to obtain data.
Usually used for dynamic data structures Usually used to store a fixed number of elements of the same data type
The related functions are malloc(), free() Implicit allocation and deletion
Usually points to anonymous data itself is the data name

Guess you like

Origin blog.csdn.net/qq_40709487/article/details/127505173