C language from entry to proficiency on the 17th day (combined use of pointers and arrays)

The difference between different types of pointer variables

Before understanding the combination of arrays and pointers, we first supplement pointer variables. Let's compare int *p1和char *p2the difference?

Same point:

  • are pointer variables
  • are used to store a memory address number
  • The memory space occupied is the same
int main(){
    
    
    int a = 10;
    char b = 'e';
    int *p1 = &a;
    char *p2 = &b;

    // 输出指针变量所指向值的内容
    printf("%d %c\n",*p1,*p2);

    // 输出指针变量所占空间大小
    printf("%d %d\n",sizeof(p1),sizeof(p2));  // 8 8

    // 输出指针变量所指向空间的大小
    printf("%d %d\n",sizeof(*p1),sizeof(*p2));  // 4 1
}

Note: The memory space occupied by the pointer variable here is 4 sections on the 32-bit machine , and 8 sections on the 64-bit machine . This is because the pointer variable stores the number of a memory address , and the maximum number of 32-bit machine memory addresses is 2 32 − 1 2^{32}-12321 , can be stored in a variable of 4 bytes. In 64-bit machines, the maximum number of memory addresses is2 64 − 1 2^{64}-12641 , can be stored in 8-byte variables.

difference:

  • The function of int *p1 is that the pointer variable p1 treats the binary in the memory space it points to as an int type.
  • The function of char *p2 is that the pointer variable p2 treats the binary in the memory space it points to as a char type.

The difference between p1++ and p2++, let's look at the following code:

int main(){
    
    
    int a = 10;
    char b = 'e';
    int *p1 = &a;
    char *p2 = &b;

    printf("%p %p\n%p %p",p1,p2,p1++,p2++);
    return 0;
}

The output address after p1 self-increment is 4 bytes more than the beginning, and the output address of p2 after self-increment is 1 byte more than the beginning, that is, when the pointer variable is self-incrementing, the offset is a data type, rather than a single byte.

pointer to array

A variable has an address, an array contains several elements, and each array element occupies a storage unit in memory, and they all have corresponding addresses. The so-called array pointer refers to the starting address of the array. That is, the array name represents the first address of the array, so the array name is also a pointer. We learned earlier that arrays can be accessed through subscripts. When the pointer is auto-incremented, the offset is the number of data types, so here we can also access the elements in the array through [array name + offset].

grammar:

*(数组名+偏移量)

  • The offset here refers to the position of the accessed element.
  • *The array name plus offset is actually an address, and then the content in the address is taken out through the pointer operator .

insert image description here

code show as below:

int main(){
    
    
    int ch[] = {
    
    1,2,23,33,3,3,5};

    // 使用下标进行访问数组的第三个元素
    printf("%d\n",ch[2]);  //23

    // 使用偏移量访问数组的第三个元素
    printf("%d\n",*(ch+2));  //23

    return 0;
}

array of pointers

A pointer array is an array that stores pointers. It is essentially an array, and each element in the array is a pointer .

grammar:

数据类型 *数组

  • Data types support all data types.
  • All elements in the array are pointer types, ie addresses

insert image description here

The addresses of several integer constants are saved in the array (it should be noted that the addresses are saved here rather than integer constants).

code show as below:

int main(){
    
    
    int a = 10,b = 12,c = 13;
    int *ch[3];

    // 给指针数组进行初始化
    ch[0] = &a;
    ch[1] = &b;
    ch[2] = &c;

    // 因为ch[0] = &a,则对&a 进行指针运算符则相当于对数组进行操作
    // 所以想得到a的内容需使用 *ch[0]

    printf("b is%d\n",*ch[1]);
    return 0;
}

Note: int *ch[3]等价于(int *)ch[3] ;Because the priority of []the ratio *is higher than that chof the match. So it can be understood here that he is an int *array of a type.

Guess you like

Origin blog.csdn.net/m0_67021058/article/details/130464821