Advanced detailed explanation of C language pointers


Preface

We have already been exposed to pointers in the previous basic pointer blog, and we know the concept of pointers:

  1. Pointer variables are used to store addresses, which are equivalent to the number of the smallest storage unit.
  2. The size of the pointer is fixed, 4/8 bytes on 32/64-bit platforms.
  3. Pointers have types, and the type determines the distance the pointer ± integer moves and the permissions when dereferencing.

Today we start the advanced study of pointers:


1. Character pointer:

In the elementary level of pointers, we know that there is a pointer type called character pointer char*. We know that it points to a character, so let's look at a piece of code:

#include <stdio.h>
int main()
{
    
    
	char* pstr = "hello";
	printf("%s", pstr);
	return 0;
}

Is a string put into the pstr pointer variable here?
The answer is: No. Beginners can easily think that the string "hello" is placed in the character pointer pstr, but the essence is that the address of the first character of "hello" is placed in pstr. middle.
Insert image description here
Let’s practice a question: What is the result of running the following code? ? ?

#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 answer is: "str1 and str2 are not the same", "str3 and str4 are the same".
str1 and str2 are different arrays and will open up different memory blocks. The str3 and str4 here point to the same constant string. In C/C++, the constant string is stored in a separate memory area. When several pointers point to the same constant string, they actually point to the same memory block.

2. Pointer array:

In the previous article, elementary pointers, we have already learned about pointer arrays.
Let’s review again. What arrays do the following pointers point to? ?

int main()
{
    
    
	int* arr2[10];//整形指针的数组
	int** arr3[10];//二级整形指针的数组
	return 0;
}

3. Array pointer:

We also show that it is a pointer by looking at the arraypointer.
The array pointer should be a pointer to the array.
Which of the following codes is an array pointer? ?

int main()
{
    
    
	int *p1[10];
	int(*p2)[10];
	return 0;
}

Explanation: We know through the priority of the operator that the priority of [ ] is higher than *.
The first p1 is first combined with [ ], indicating that it is an array. The previous type indicates that the array element type is int* type. In (*p2), p2 is first combined with *, so p2 is a pointer, and the remaining int [10] outside indicates that p2 points to an integer array.

4. Function pointer:

According to the array pointer above, the function pointer is also a pointer to a function.
Let’s look at a piece of code first:

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

This is different from the array name. The function name and & function name are both the address of the function.

So if we want to save the address of our function, how do we save it? ?

#include <stdio.h>
void test()
{
    
    
	printf("hello");
}
int mian()
{
    
    
	void *ptest1() = &test;//??
	void (*ptest2)() = &test;//??
	return 0;
}

Which code above is correct? ?
The answer is: ptest2. Based on the above understanding of array pointers, ptest1 is first combined with (), so ptest1 is a function. The function has no parameters. The return type depends on the remaining void outside. *. ptest2 is first combined with *, so it is a pointer. Looking at the void () left outside, it means that ptest2 points to a function with no parameters and a return type of void.

5. Function pointer array:

We know that a pointer array is an array that stores pointers. From the literal meaning, we can know that a function pointer array is an array that stores function pointers. How to define the array of function pointers? ?
Which of the following is the definition of an array of function pointers?

	int(*parr1[10])();
	int* parr2[10]();
	int (*)() parr3[10];

The answer is: parr1
Explanation: parr1 is first combined with [ ], indicating that it is an array.
After parr1[ ] is removed, the remaining int (*) [ ] is the type of the array element, which is the function pointer type.

6. Pointer to function pointer array:

#include <stdio.h>
void test()
{
    
    
	printf("hello");
}
int main()
{
    
    
	void (*ptest)() = test;//函数指针ptest
	void (*ptestarr[5])();//函数指针数组ptestarr
	void (*(*pptestarr)[5])();//指向函数指针数组ptestarr的指针pptestarr
	return 0;
}

Summarize

The above is the advanced content of C language pointers discussed today. I hope it will be helpful to you who have just read this blog!

Guess you like

Origin blog.csdn.net/weixin_61661271/article/details/124567461