Pointer array && array pointers
Char *a[5]; Defines an array of pointers, the elements of which are char * pointer types. Initialization can also hold characters or strings inside. The type of a is char *[5]//if char *a[5]={"red", "white", "Blue", "dark", "green"};//a here is the type char *[], essentially a level two pointer. That means that the memory represented by a is the address of the first element in the array. The memory that a+1 represents is the address of the second element in the array, and so on ...//a[0] is the address of the "red" string, printf ("%s", A[0]),//a[0]+1, which means that the address of the "red" string is offset by a position, Access character ' C '. printf ("%c", * (a[0]+1))//If the address of a is 0x0000000, then A+1 's address is 0x00000008 (64-bit platform) or 0x00000004;int (*PTR) [5]; Defines a pointer to an array of arrays of PTR, whose actual type is char (*) [5], (number can be ignored) because in some strict compiler does not simultaneously warn and then define a two-dimensional array int a[1][5]={1,2,3,4,5};//a is of type int [1] [5] (numbers can be ignored), the dimensions of the columns are the same, p=a can be assigned values. That is, the array pointer ptr can be seen as a level two pointer, which uses the same usage as Char **a.
from the above we can see that char (*) [], Char [], char * * can actually be considered equivalent
#include <stdio.h>int main (void) {char *a[5]={"red", "white", "Blue", "dark", "green"}; printf ("%s%c%s\n", A[0], * (a[0]+1), a[1]+1); printf ("%x%x\n", a[0], a[1]); char m[2][5]={"xxx", "yyyy"}; char (*PTR) [5]=m; printf ("%x%s%s%x%s\n", ptr[0], PTR, ptr[1], ptr[0]+2, ptr[0]+1); printf ("%d", sizeof (a)); printf ("%d", sizeof (m)); return 0;}
@1 defines an array of pointers, and the elements in the group are char * types
@2 defines an array pointer, the PTR type is char (*) [The],m type is char [], and the two types can be assigned. But they are not exactly equivalent.
The type of @3 a is char*[5],sizeof (a) =sizeof (char *); The type of M is a two-dimensional array of char [], and sizeof (m) evaluates the actual bytes it occupies in memory, and some compilers align the bytes for efficiency reasons.
The operation results are as follows
Common pitfalls
Double n =2.0, *pt=&n;printf ("Double%d%d\n", PT, pt+1);//difference is 8 char c= ' m ', *q=&c;printf ("Char%d%d\n", Q, q+ 1);//difference is 1char **p=&q;printf ("%d%d", p, p+1);//difference is 4printf ("%d%d%d", sizeof (int*), sizeof (double*), sizeof (char*), sizeof (int**));
// from the results you can see that no matter what type of pointer in memory is a four-byte size deposit
nbsp , &NB Sp , &NB Sp , &NB Sp , &NB Sp
function pointers && pointer functions
The function pointer, like any other pointer, must be initialized to point to a function before performing an indirect access to the function pointer.
Here is a way to initialize a function pointer:
int fun (int);
Int (* PF) (int) = &fun;
Create the function pointer pf and point it to the function fun. It is important to have a fun prototype before the initialization of a function pointer, otherwise the compiler cannot check that the type of fun is the same as the type that PF points to.
After initializing the declaration, we can call the function in the following three ways:
int ans;
1) ans = fun (25);
2) ans = (*PF) (25);
3) ans = PF (25);
Statement 1 Simple use of the name call function fun. But it's going to be a different process than you think: the function name fun is first converted to a functional pointer that points to a piece of space in memory. The function call operator then invokes the function, executing the code that begins at that address.
Statement 2 performs an indirect access operation to PF, converting PF to the function name. But this conversion is actually unnecessary, and the compiler will convert it back before executing the function call operator.
Statement 3 and the first two perform the same effect.
The common use of function pointers is to pass function pointers as arguments to functions, as shown below.
Operation Result:
Functions using the above techniques are called callback functions (callback function). The user passes a function pointer as a parameter to other functions, which will "callback" the user's function. Compare integer to String, respectively, for comparison function compar_int (void const *,void const *) and Compar_char (void const *,void const *), through to compare (const void * , const void *,Int (*compare) (void const *, void const *)) pass different function pointers, implementing comparisons of different types of data through the same interface.
pointer function, pointer function refers to a function with pointers, that is, essentially a function. The function has a return type (no value if no return value), except that the pointer function return type is a pointer to a type.
Pointer array, array pointer, function pointer, pointer function summary