[C Advanced] the first stage of pointer advanced (character pointer, array pointer, pointer array, array parameter transfer and pointer parameter transfer)

catalogue

1. Character pointer

2. Pointer array and array pointer

2.1 pointer array

2.2 array pointer

3. Array parameter, pointer parameter

3.1 array parameter transfer

3.2 pointer transfer parameter

1. Character pointer

         Among the pointer types, we know that one pointer type is character pointer char *, so let's take a look at the following exercise.

#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;
}

        Analysis: str3 and str4 point to the same constant string. C/C + + will store the constant string in a separate memory area when several pointers. When pointing to the same string, they actually point to the same block of memory. However, when initializing different arrays with the same constant string, different memory blocks will be opened up. So str1 and str2 are different, and str3 and str4 are the same.

2. Pointer array and array pointer

2.1 pointer array

        We have learned about pointer array before. Pointer array is an array that stores pointers, and each element in it is a pointer type. There will be no more introduction here.

2.2 array pointer

        The so-called array pointer, you can first know that this is a pointer. The array pointer should be a pointer that can point to the array. We already know that the array name can be implicitly typed into a pointer to the first element of the array. What is the difference between the array pointer and the array name? How should array pointers be defined?

int *p1[10];            //This is an array of pointers
int (*p2)[10];          //This is an array pointer      

        In int (*p)[10], P is first combined with * to indicate that P is a pointer variable, and then points to an array of 10 integers. So p is a pointer to an array, called an array pointer. Note here that the priority of [] is higher than that of * sign, so () must be added to ensure that P is combined with * first.

        Perhaps array pointers should be defined like this, int (*) []   name . In fact, the prototype of array pointer is like this, but later, the pointer variable was moved forward for convenience and beauty, so we should get used to this writing.

        So what's the difference between array pointers and array names? Let's look at the following code.

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

         We can find that the value of arr is the same as that of & arr. Let's look at the next 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;
}

         After the above two codes, we can find that & arr represents the address of the array, not the address of the first element of the array. In this example, the type of & arr is int(*)[10], which is an array pointer type. The address of the array + 1 skips the size of the entire array, so the difference between & arr + 1 and & arr is 40  

3. Array parameter, pointer parameter

         When writing code, it is inevitable to pass [array] or [pointer] to the function. How to design the parameters of the function?

3.1 array parameter transfer

        We know that the parameters of one-dimensional array can be passed to one-dimensional array or pointer. What about two-dimensional array parameters?

void test(int arr[3][5])        //You can pass parameters like this        
{}
void test(int arr[][])        //You can't pass parameters like this
{}
void test(int arr[][5])        //You can pass parameters like this
{}
//Summary: for the design of two-dimensional array parameters and function parameters, only the first [] number can be omitted.
//Because for a two-dimensional array, you can't know how many rows there are, but you must know how many elements there are in a row.
//This is convenient for calculation.

void test(int* arr[5])        //There will be a warning
{}
void test(int (*arr)[5])        //sure
{}
void test(int **arr)            //There will be warnings,
{}

//Similarly, when a two-dimensional array passes parameters, it will also be turned into an array pointer, pointing to the first row of the two-dimensional array, so we also call the array pointer row pointer

3.2 pointer transfer parameter

        We know that when the parameter part of a function is a first-order pointer, we can pass in the corresponding type to get the address or an array name. What about the secondary pointer?

        Similarly, we can know that we can pass a pointer variable to get the address or secondary pointer or pointer array name.         

Tags: C

Posted on Wed, 27 Oct 2021 01:55:22 -0400 by moe180