Detailed explanation of C language—advanced pointers (1)

Table of contents

Introduction to pointers:

1.Character pointer

 example:

2. Pointer array

3.Array pointer

 What is the use of array pointers? 

Analysis: 

4. Array parameter passing and pointer parameter passing

One-dimensional array parameter passing: 

Two-dimensional array parameter transfer:

5. Function pointer

definition

transfer 

 interesting example


Introduction to pointers:

  1. A pointer variable is a variable used to store an address. The address uniquely identifies a memory space.
  2. The size of the pointer is fixed at 4 or 8 bytes, corresponding to 32-bit and 64-bit platforms respectively.
  3. Pointers are typed, and the type of the pointer determines the step size of the pointer's addition and subtraction of integers and the permissions during the pointer dereference operation.
  4. Pointers can perform operations.

1.Character pointer

As the name suggests, a pointer to a character, char*.  

int main()
{
    //指向单个字符
    char ch='w';
    char *pc = &ch;
    printf("%c\n", *pc);
    //指向字符串
    char* p="hello CSDN!";
    printf("%s\n", *p);
    return 0;
}

 Note: the pointer variable p points to the string hello CSDN! The first address of , which is the address of character h

 example:

#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 running results are as follows:

 Explanation: str1 and str2 are two different arrays. They open different memory blocks when they are created, and have nothing to do with their stored contents. str3 and str4 point to the same constant string. C/C++ will store the constant string in a separate memory area as several pointers. When pointing to the same string, they will actually point to the same memory.

2. Pointer array

  • A pointer array is an array that stores pointers, and the stored element type is a pointer.
  • An array of pointers—essentially an array—is an array that stores pointers
  • char* arr[5];—array storing character pointers
  • int* arr2[6];—array storing integer pointers

 

3.Array pointer

  •  Array pointers are essentially pointers! !
  • Character pointer—a pointer to a character—the location where the character is stored
  • char ch = ‘a';
  • char* pc= &ch;
  • Integer pointer—a pointer to an integer—stores the address of the integer
  • int number = 100;
  • int* p    = &num;
  • Array pointer - is a pointer to an array - stores the address of the array
int main()
{
	int arr[10] = { 0 };
	int (*p)[10] = &arr; //p用来存放数组的地址,p就是数组指针

	char* arr2[5];
	char* (*pc)[5] = &arr2;	//指向char*类型

	int arr3[] = { 1,2,3 };
	int(*p3)[3] = &arr3;//数组指针[]不能为空
	return 0;
}

 int (*p)[10]; p is first combined with *, declaring that p is a pointer variable, and then points to an array of 10 integers. The priority of [ ] is higher than *, so ( ) of (*p) cannot be omitted.

 What is the use of array pointers? 

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_arr2(arr, 3, 5);
    return 0;
}

 

Array pointers can be used as parameters to receive arrays

When passing parameters in a two-dimensional array, what is passed is the address of the first element. A two-dimensional array is equivalent to a one-dimensional array in which each element is a one-dimensional array. What is actually passed is the address of the first row.

Analysis: 

4. Array parameter passing and pointer parameter passing

When passing parameters in an array:

  1. What is passed is the address of the first element
  2. One-dimensional array parameters are passed, and the address of the first element is passed
  3. When passing parameters in a two-dimensional array, the address of the first row is passed.
  4.  When passing parameters in an array, the form can be written as an array or as a pointer.

One-dimensional array parameter passing: 

 

The above forms of parameter passing are all correct. The essence of array parameter passing is to pass the address of the first element of the array. The parameter passing form can be written as an array or a pointer. It is recommended to write it as a pointer.

Two-dimensional array parameter transfer:

 

5. Function pointer

#include <stdio.h>
void test()
{
 printf("hehe\n");
}
int main()
{
 printf("%p\n", test);
 printf("%p\n", &test);
 return 0;
}

 

The function name and the address of the function name are essentially the same, they are both the address of the function. 

definition

Next we learn how to define function pointers.

int Add(int x, int y)
{	
    return x + y;
}

Define an Add function with two parameters,

int (*pf1)(int, int) = Add;
int (*pf1)(int x , int y) = Add;

In C or C++, the parameter name of a function pointer is not mandatory, so both methods can be used to define a function pointer, but in actual use, the parameter name is usually omitted 

        pf1 is a function pointer variable. The combination of * and pf1 indicates that pf1 is a pointer type. The brackets immediately following indicate that the pointer pf1 points to the parameters of the function. Just write the corresponding type.

        You can write &Add after the equal sign or you can write Add directly, and the effect is the same.

transfer 

int ret = (* pf1)(2, 3);
int ret1=  pf1(2, 3);
int ret1=  Add(2, 3);

 When called, the above three effects are the same.

The first one: call the function Add through (* pf1), and the following parentheses are the parameters passed into the function.

The second is the same as the third.

Explanation: Because pf1 points to the function Add, that is, the address where Add is stored, that is, pf1 == Add . When calling a function normally, you usually write the function name and parameters directly, that is, Add(2, 3); so we can replace Add with pf1 or Written directly as Add , the result is the same.

 interesting example

The following code is calling the function at address 0. This function has no parameters and the return type is void. 

( *(void (*)()) 0 )();

See it more clearly like this:

(  *(  void (*)()  ) 0 )  ();
  • void (*)() is a function pointer type.
  • ( void (*)( ) ) Casts "0" to a function pointer type.
  • *( void (*)( ) )0    dereferences the function pointer and calls the function at address 0 .
  • This function has no parameters, so it is followed by ( ) parentheses with nothing.
  • The function at address 0 is declared as return  void type. Therefore, no matter what this function does at address 0, it returns nothing ( void), so the return type of the entire expression is  void.

 Let's look at another example:

void (* signal(int , void(*)(int) ) ) (int);
  • This code is a function declaration
  • The signal function is declared
  • The signal function has two parameters:
  • The first one is int type, the second one is function pointer type, the function parameter pointed by this pointer is int, and the return type is void;
  • The return type of the signal function is also a function pointer type, which is void(*)(int). The function parameter pointed to by the function pointer is int, and the return type is void.

 Let’s get into C language—advanced pointers (2) ! ! !

Guess you like

Origin blog.csdn.net/m0_73800602/article/details/132761704