I have to say that pointers are really a headache but especially important to people who learn C + +, and here I share my learning experience with you, hoping to help more people learn. Speaking of pointers, perhaps you are still in the end what is a pointer? are pointers and arrays exactly equal? What is an array of pointers? What is an array pointer? What is a function pointer? function pointer array, function pointer array pointer what are they? After reading this you will have a deeper understanding of the pointer, I would like to answer for everyone.
1. What is a pointer?
To understand what is the pointer first look at such an example: int *a;
...
*a=12;
Think first, is this code correct? In fact, this code is wrong, it first declares that a pointer variable A is created, and the last statement is to store 12 in the memory pointed to by a, where * means A is a pointer, it is an integer variable pointing to 4 bytes, * It is like the lock core of the security door, without this *, we can not read or write this block of memory. The code error is that when declaring a pointer variable, we do not initialize it, so we have no way to predict where the value of 12 will be stored, because we don't know where a is pointing.
So, the address of a variable is called the "pointer" of the variable, and the pointer variable is a variable that is specifically used to hold the address (that is, the pointer) of another variable, and the value of the pointer variable is the address, which accounts for 4 bytes. So before you can indirectly access or dereference pointers, make sure that they are initialized, so it's best to initialize them at the same time when declaring pointer variables.
2. Is the pointer and array equal?
In fact, pointers and arrays are not equal, although they can be interoperable in many cases. If you still have doubts about this conclusion, take a look at the following example:
int a[5];
int *b;
Both lines of code are declarations, and both A and B have pointer values, because the array name a represents the address of the first element of the array, the address of a[0], and B is a pointer variable, and they can both dereference and subscript reference operations, but this does not mean that they are equal. Because when declaring an array, the compiler retains the appropriate memory for the number of elements specified by the declaration, and then creates the array name, whereas when declaring a pointer variable, the compiler retains only the memory space for the pointer itself, and it does not allocate memory space for any integer value, and The pointer variable is not initialized to point to any existing memory space, and if it is an automatic variable, it will not even be initialized.
So the upper-*a is perfectly legal. *b is illegal, it accesses an indeterminate location in memory, or causes the program to terminate. In addition, b++ can be compiled, a++ is not, because a is a constant value, which represents the address of the first element of the array. Pointers and arrays are therefore unequal.
A pointer variable is always 4 bytes under a 32-bit system, and its value is the address of a memory. The size of the array is related to the type and number of elements, the type and number of elements must be specified when the array is defined, and the array can store any type of data, but it cannot store the function.
3. What is an array of pointers? What is an array pointer?
Pointer array It is first an array, the elements of the array are pointers, and how many bytes of the array are determined by the array itself. Array pointer First it is a pointer to an array that is always only 4 bytes under the 32-bit system. Here, we need to know [] the priority is higher than *, and () priority is higher than [], so we can easily determine the int *p1[10]; is an array of pointers, int (*P2) [10]; is an array pointer.
4. What is a function pointer?
A function pointer is a pointer to a function, which is a pointer to a function. Let's look at an example of a function pointer application:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Char *fun (char *p1, char * p2)
{
int i = 0;
i = strcmp ( p1, p2 );
if (0 = = i)
{
return p1 ;
}
Else
{
return p2 ;
}
}
int Main ()
{
Char * (*PF) (char *p1, char *p2); //Through (*PF) the function that exists on this address is the fun function, and then it is called.
PF = &fun;
( *pf) ( "AA", "BB" );
System ( "pause");
return 0;
}
Here, we need to look at an interesting example: (* (Void (*) ()) 0) (); Take a look. Can you read what it stands for? Do not understand do not worry, I will slowly analyze to you to see, and then you will feel it is not so difficult to understand the surface. (* (Void (*) ()) 0) (); This separates the above code, does it look much nicer?
1, Void (*) () This is a function pointer type, this function has no parameters, and there is no return value.
2, (Void (*) ()) 0 This is the 0 coercion type conversion function pointer type, here 0 is different from the usual 0, it is here is an address, that is, a function is stored in a section of the first address is 0 in the area.
3, (* (Void (*) ()) 0) This is a reference to what is said above.
4, (* (Void (*) ()) 0) ( ) This is a function call.
5. What is an array of function pointers? What is a pointer to an array of function pointers?
char * (*pf[3]) (char *p); This code is the definition of a function pointer array, which is an array, the array name is PF, the array is stored in the three pointers to the function
Needles, these pointers point to some pointers that return a value type to a character, and a function that points to a pointer to a character, so that parsing is not easy to understand much. In fact, there's a simple way to look backwards, it's an array, and then it stores pointers, which point to functions.
An array pointer to a function pointer is actually a pointer to an array that stores pointers to functions, which can also be analyzed using the inverted method I mentioned above.
This article from the "10911544" blog, reproduced please contact the author!
The pointer is no longer a stumbling block for you ........ Don't miss it!!