How to define arrays, pointers, and arrays in C language


Preface

The author learned a little bit of C language when he was a freshman, but it has never been used since then. Recently, I have used it and found that there are many things I don't know, so I started a blog to record the learning process.

1. The relationship between arrays and pointers

         C language supports the data structure of array, which is a collection of fixed-size elements of the same type. 

         To declare an array, you need to specify the type and number of elements. As follows:

//type arrayName [arraySize],声明一个int类型、包含10个元素的数组
int p[10];

        Now p is a usable array that can hold 10 int type data. p[0] represents the first element in it

         The array name, which is p here , is a pointer to the first element in the array . That is, the value of p is the address of the first element in the array p.

2. Pointers in C

           Each variable has a memory location, and the address can be accessed using the & operator.

          What is a pointer? A pointer is a variable used to store a memory address (there are also constant pointers). It is just like other variables or constants, except that it stores the address value, as shown below.

int a = 10;
int *p = &a;

         The value of a pointer is an address , but it can also represent the value pointed to by the address. For example, the value of p here is the address of a , and *p can represent the value pointed to by this address, which is 10.

int a = 10;
int *p = &a;
printf("a的地址为%p\n",p);
printf("a的值为%d\n",*p);

How to define pointers?

           The general form of pointer variable declaration is

//type *var_name
int *ip; //一个整型的指针,也就是ip指向的是整型量
doubel *dp; //一个double 型的指针
float *fp; //一个浮点型的指针

Note that the type here is the type of the amount pointed to by the pointer . For example, ip is the address of an integer data, but the value type of the pointer itself is the same, which is a long hexadecimal number representing the address.

 3. How to use pointers to represent arrays:

         As mentioned in the first part, the array name is actually a pointer to the first element in the array, that is, the array name can be used as a constant pointer (the two are not completely equivalent).

          Regarding what are constant pointers and pointer constants:

         Constant pointer : The starting point is the pointer. This is a pointer, but it is a constant pointer, that is, it points to a constant. (The pointer itself can be a variable). As shown in the following example.

const int *p = &a;

  Here p is a pointer variable (can be assigned again), pointing to the constant a.

          Pointer constant : The starting point is a constant, indicating that it is a constant of pointer type. As shown in the following example:

int *const p = &a;//定义一个指针常量

Here p is a constant, its value is the address of a, and p cannot be assigned again.

       How to use pointers to represent arrays?

         For example, an array bear is declared here. The bear below is a pointer to bear[0], that is, bear is equivalent to a constant pointer, and the address of bear[0] is stored.

int bear[2];

         We know that bear is equivalent to a pointer type quantity, so bear can be assigned to the pointer here. Now you can use var_bear to access the array. For example, *(var_bear) points to bear[0], *(var_bear+1) points to bear[1];

 

          By the way, var_bear+1 actually adds 4 to its value. This is because the int type occupies 4 bytes.

        


Summarize

Pointer variables store address values, and array names can be regarded as constant pointers.

Guess you like

Origin blog.csdn.net/weixin_51286347/article/details/123485484