[C Language]——Pointer Array (1)

Preface

Through the previous study, we have already understood the basic concepts and usage of pointers. Today I will share with you some pointer exercises and understand some common internship interview questions.
It is mainly divided into nine modules to share:

  1. character pointer
  2. array pointer
  3. array of pointers
  4. Array parameter passing and pointer parameter passing
  5. function pointer
  6. Array of function pointers
  7. Pointer to array of function pointers
  8. Callback
  9. Analysis of pointer and array interview questions

1. Character pointer

Among the types of pointers, we know that there is a pointer type called character pointer char*;

General use:

  int main()
{
    
    
    char ch = 'w';
    char *pc = &ch;
    *pc = 'w';
    return 0;
}

Another way to use it is as follows:

int main()
{
    
    
    const char* pstr = "hello bit.";//这里是把一个字符串放到pstr指针变量里了吗?
    printf("%s\n", pstr);
    return 0;
}

The code const char* pstr = “hello bit.”; It is particularly easy for students to think that the hello bit of the string is placed in the character pointer pstr, but the essence is to put the address of the first character of the string hellobit. Arrived in pstr.
Insert image description here
The above code means to store the address of the first character h of a constant string into the pointer variable pstr.
Then there are interview questions like this:

#include <stdio.h>
int main()
{
    
    
    char str1[] = "hello bit.";
    char str2[] = "hello bit.";
    const char *str3 = "hello bit.";
    const char *str4 = "hello bit.";
    if(str1 ==str2)
 printf("str1 and str2 are same\n");
    else
 printf("str1 and str2 are not same\n");
      
    if(str3 ==str4)
 printf("str3 and str4 are same\n");
    else
 printf("str3 and str4 are not same\n");
      
    return 0;
}

The final output here is:
Insert image description here

Here str3 and str4 point to the same constant string. C/C++ will store constant strings in a separate memory area as several pointers. When pointing to the same string, they will actually point to the same memory. But when using the same constant string to initialize different arrays, different memory blocks will be opened up. So str1 and str2 are different, str3 and str4 are different.

2. Array pointer

In the chapter "Pointers" we also learned about pointer arrays, which are arrays that store pointers.
Let’s review it again. What does the following pointer array mean?

int* arr1[10]; //整形指针的数组
char *arr2[4]; //一级字符指针的数组
char **arr3[5];//二级字符指针的数组

3. Pointer array

3.1 Definition of array pointer

Is an array pointer a pointer? Or an array?
The answer is: pointers.
We are already familiar with:
Integer pointer: int * pint; A pointer that can point to integer data.
Floating-point pointer: float * pf; A pointer that can point to floating-point data.
The array pointer should be: a pointer that can point to the array.
Which of the following codes is an array pointer?

int *p1[10];
int (*p2)[10];
//p1, p2分别是什么?

explain:

int (*p)[10];
//解释:p先和*结合,说明p是一个指针变量,然后指着
//指向的是一个大小为10个整型的数组。所以p是一个
//指针,指向一个数组,叫数组指针。
//这里要注意:[]的优先级要高于*号的,所以必须加上()来保证p先和*结合。

3.2 &Array name VS array name

For the following array:

int arr[10];

What are arr and &arr respectively?
We know that arr is the array name, and the array name represents the address of the first element of the array.
So what is the name of the &arr array?

Let's look at a piece of code:

#include <stdio.h>
int main()
{
    
    
    int arr[10] = {
    
    0};
    printf("%p\n", arr);
    printf("%p\n", &arr);
    return 0;
}

Insert image description here

It can be seen that the address printed by the array name and &array name is the same.
Are the two the same?

Let’s look at another piece of code:

#include <stdio.h>
int main()
{
    
    
 int arr[10] = {
    
     0 };
 printf("arr = %p\n", arr);
 printf("&arr= %p\n", &arr);
 printf("arr+1 = %p\n", arr+1);
 printf("&arr+1= %p\n", &arr+1);
 return 0;
}

The running results are as follows:
Insert image description here

According to the above code, we found that in fact, although the values ​​​​of &arr and arr are the same, their meanings should be different.

In fact: &arr represents the address of the array, not the address of the first element of the array. (Think about it carefully)
In this example, the type of &arr is: int(*)[10], which is an array pointer type. The address of the array + 1, skipping the entire array size, so the difference between &arr+1 and &arr is 40.

3.3 Use of array pointers

❓How to use array pointers?
Since the array pointer points to an array, the array pointer should store the address of the array.

Look at the code:

#include <stdio.h>
int main()
{
    
    
    int arr[10] = {
    
    1,2,3,4,5,6,7,8,9,0};
    int (*p)[10] = &arr;//把数组arr的地址赋值给数组指针变量p
    //但是我们一般很少这样写代码
    return 0;
}

Use of an array pointer:

#include <stdio.h>
void print_arr1(int arr[3][5], int row, int col)
{
    
    
    int i = 0;
    for(i=0; i<row; i++)
   {
    
    
        for(j=0; j<col; j++)
       {
    
    
            printf("%d ", arr[i][j]);
       }
               printf("\n");
   }
}
void print_arr2(int (*arr)[5], int row, int col)
{
    
    
    int i = 0;
    for(i=0; i<row; i++)
   {
    
    
        for(j=0; j<col; j++)
       {
    
    
            printf("%d ", arr[i][j]);
       }
        printf("\n");
   }
}
int main()
{
    
    
    int arr[3][5] = {
    
    1,2,3,4,5,6,7,8,9,10};
   print_arr1(arr, 3, 5);
    //数组名arr,表示首元素的地址
    //但是二维数组的首元素是二维数组的第一行
    //所以这里传递的arr,其实相当于第一行的地址,是一维数组的地址
    //可以数组指针来接收
    print_arr2(arr, 3, 5);
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_73807021/article/details/129553127