C language uses pointer arrays to access arrays (pointer arrays and arrays)

1. Define character arrays and character pointer arrays

 	char str[32]={
    
    };
    char str1[32]={
    
    };
    char str2[32]={
    
    };
    char *sensordata[32];

Personal understanding: The character array is a first-level pointer, and the pointer array is a second-level pointer, pointing to the address of the character array or the address of the first-level pointer;

2. Print the base address of the pointer data and the pointing of the pointer

	printf("--------------指针数组的基地址-------------\n");
    printf("sensordata[0]的地址: %p\n",&sensordata[0]);
    printf("sensordata[1]的地址: %p\n",&sensordata[1]);
    printf("sensordata[2]的地址: %p\n",&sensordata[2]);

	printf("\n--------------指针数组的下标指向-------------\n");
    printf("sensordata[0]的地址: %p\n",sensordata[0]);
    printf("sensordata[1]的地址: %p\n",sensordata[1]);
    printf("sensordata[2]的地址: %p\n",sensordata[2]);

Print result:
--------------Base address of pointer array-------------
Address of sensordata[0]: 0x7ffcbe058c10
Address of sensordata[1]: 0x7ffcbe058c18
Address of sensordata[2]: 0x7ffcbe058c20

-------------- The subscript of the pointer array points to -------------
Address of sensordata[0]: 0x7ffcbe058d10
Address of sensordata[1]: 0x3
sensordata Address of [2]: 0x7ffcbe058d00

  • It can be seen from this that the base address of the pointer array is also in order, and the difference between the subscripts is 0x08.
  • The pointers stored in the pointer array point to uncertain, random, and it is impossible to know the size of the space pointed to by the pointer array;

3. The subscript of the pointer array points to the character array, and the content of the character array is formatted

//指针数组的下标指向到数组
    sensordata[0]=str;
    sensordata[1]=str1;
    sensordata[2]=str2;
    
    //格式化数组的内容,sprintf是不安全的函数,会导致缓存区溢出
    sprintf(str,"airH:%.2f ",76.83);
    sprintf(str1,"airT:%.2f ",26.50);
    sprintf(str2,"ill:%d ",356);
    
    printf("\n--------------打印数组的内容-------------\n");
    printf("str0的数据:  %s\n",str);
    printf("str1的数据:  %s\n",str1);
    printf("str2的数据:  %s\n",str2);
    
    printf("\n--------------打印指针数组下标指向的内容-------------\n");
    printf("sensordata[0]的数据: %s\n",sensordata[0]);
    printf("sensordata[1]的数据: %s\n",sensordata[1]);
    printf("sensordata[2]的数据: %s\n",sensordata[2]);

Running effect:
--------------Print the content of the array-------------
str0 data: airH: 76.83
str1 data: airT: 26.50
str2 Data: ill: 356

--------------Print the content pointed to by the subscript of the pointer array-------------
data of sensordata[0]: airH: 76.83
of sensordata[1] Data: airT: 26.50
sensordata[2] data: ill: 356

  • The subscript of the pointer array points to the character array to determine the pointer of the pointer array;
  • Use sprintf to format the content of the character array, the formatted data cannot exceed the length of the character data , otherwise it will cause memory overflow;
  • Because the subscript in the pointer array points to the character array , the data of the array can be obtained directly through the subscript;

4. Print the base address of the character array and the pointer of the pointer array

printf("\n--------------数组的基地址-------------\n");
    printf("str的地址:  %p\n",str);
    printf("str1的地址:  %p\n",str1);
    printf("str2的地址:  %p\n",str2);
    
    printf("\n--------------指针数组的指向的地址-------------\n");
    printf("sensordata[0]的地址: %p\n",sensordata[0]);
    printf("sensordata[1]的地址: %p\n",sensordata[1]);
    printf("sensordata[2]的地址: %p\n",sensordata[2]);

Operation effect:
--------------The base address of the array-------------
Address of str: 0x7ffcbe058d10
Address of str1: 0x7ffcbe058d30
Address of str2: 0x7ffcbe058d50

--------------The address pointed to by the pointer array-------------
Address of sensordata[0]: 0x7ffcbe058d10
Address of sensordata[1]: 0x7ffcbe058d30
sensordata The address of [2]: 0x7ffcbe058d50
It can be seen that the pointer in the pointer array points to the same base address as the character array, so that the pointer data can be stored and retrieved from the array!

5. Complete test code and effect display

//gcc 7.4.0

#include  <stdio.h>
#include <string.h>

int main(void)
{
    
    
    char str[32]={
    
    };
    char str1[32]={
    
    };
    char str2[32]={
    
    };
    char *sensordata[32];
    
    printf("--------------指针数组的基地址-------------\n");
    printf("sensordata[0]的地址: %p\n",&sensordata[0]);
    printf("sensordata[1]的地址: %p\n",&sensordata[1]);
    printf("sensordata[2]的地址: %p\n",&sensordata[2]);
    
    printf("\n--------------指针数组的下标指向-------------\n");
    printf("sensordata[0]的地址: %p\n",sensordata[0]);
    printf("sensordata[1]的地址: %p\n",sensordata[1]);
    printf("sensordata[2]的地址: %p\n",sensordata[2]);
    
    //指针数组的下标指向到数组
    sensordata[0]=str;
    sensordata[1]=str1;
    sensordata[2]=str2;
    
    //格式化数组的内容,sprintf是不安全的函数,会导致缓存区溢出
    sprintf(str,"airH:%.2f ",76.83);
    sprintf(str1,"airT:%.2f ",26.50);
    sprintf(str2,"ill:%d ",356);
    
    printf("\n--------------打印数组的内容-------------\n");
    printf("str0的数据:  %s\n",str);
    printf("str1的数据:  %s\n",str1);
    printf("str2的数据:  %s\n",str2);
    
    printf("\n--------------打印指针数组下标指向的内容-------------\n");
    printf("sensordata[0]的数据: %s\n",sensordata[0]);
    printf("sensordata[1]的数据: %s\n",sensordata[1]);
    printf("sensordata[2]的数据: %s\n",sensordata[2]);
    
    
    printf("\n--------------数组的基地址-------------\n");
    printf("str的地址:  %p\n",str);
    printf("str1的地址:  %p\n",str1);
    printf("str2的地址:  %p\n",str2);
    
    printf("\n--------------指针数组的指向的地址-------------\n");
    printf("sensordata[0]的地址: %p\n",sensordata[0]);
    printf("sensordata[1]的地址: %p\n",sensordata[1]);
    printf("sensordata[2]的地址: %p\n",sensordata[2]);
    printf("sensordata的数据: %s\n",sensordata);
    
    return 0;
}
Compilation time: 0.44 sec, absolute running time: 0.15 sec, cpu time: 0 sec, memory peak: 5 Mb, absolute service time: 0,69 sec
--------------指针数组的基地址-------------
sensordata[0]的地址: 0x7ffcbe058c10
sensordata[1]的地址: 0x7ffcbe058c18
sensordata[2]的地址: 0x7ffcbe058c20

--------------指针数组的下标指向-------------
sensordata[0]的地址: 0x7ffcbe058d10
sensordata[1]的地址: 0x3
sensordata[2]的地址: 0x7ffcbe058d00

--------------打印数组的内容-------------
str0的数据:  airH:76.83 
str1的数据:  airT:26.50 
str2的数据:  ill:356 

--------------打印指针数组下标指向的内容-------------
sensordata[0]的数据: airH:76.83 
sensordata[1]的数据: airT:26.50 
sensordata[2]的数据: ill:356 

--------------数组的基地址-------------
str的地址:  0x7ffcbe058d10
str1的地址:  0x7ffcbe058d30
str2的地址:  0x7ffcbe058d50

--------------指针数组的指向的地址-------------
sensordata[0]的地址: 0x7ffcbe058d10
sensordata[1]的地址: 0x7ffcbe058d30
sensordata[2]的地址: 0x7ffcbe058d50
sensordata的数据: ���

At the same time, an online compiler is recommended to test the case directly on the web page;

Guess you like

Origin blog.csdn.net/a6662580/article/details/124381655