Not much nonsense, directly on the code, the key things have been commented, see the annotation information understanding can be.
Description: This program discusses the relationship between a one-dimensional array and pointers, discusses the relationship between two-dimensional arrays and pointers, the relationship between pointer arrays and pointers in a string array, the code to give a defined method and a simple use, but some of the usage methods are not listed, if necessary,
Please add your own to the printf debugging.
Reprint please indicate the source, welcome and I discuss, thank you.
----------Cofin_add
#include <stdio.h>
void Fun (int m, char * ((*P) [M]))
{
int i = 0;
char * * BUF = (char * *) p;
for (i=0;i<m;i++)
{
printf ("%s\t", * (Buf+i));
}
printf ("\ n");
}
void fun1 (int m, char **buf)
{
int i = 0;
for (i=0;i<m;i++)
{
printf ("%s\t", * (Buf+i));
}
printf ("\ n");
}
int main (void)
{
Char *buf[] = {"AA", "BB", "CC", "dd", "EE", "FF"}; Pointer array, where each variable (pointer) points to the first address of a string.
Char **pa = buf;//How to point to an element in an array, because each of these elements is a pointer, so you should point to it with a double pointer.
Char **px = &buf[0]; The array name is the first address of the array element, and you can see the same as above.
char * (*PB) [6] = &buf; How do I point to the pointer array itself? Since there are six variables in the array (with 6 pointers or pointer variables (addresses)), the array pointer should be used to point to it.
But because the variable is a pointer, it should be added a *, purely personal speculation, if this is the case, it can also explain why the above is a double pointer, because BUF and &buf[0] equivalent
That is, in fact &buf[0] is the address of the address (because BUF[0] itself is stored in the address), should be pointed with a double pointer.
A re-study of one-dimensional arrays
int a[5] = {0};//definition array, with 5 variables of type int
int *P1 = a;//Defines the pointer to an element in the array
int (*P2) [5] = &a; If you want to point to the array itself, you need to point to it with an array pointer, note the parentheses, [] higher than the * precedence.
Research on the pointer of two-dimensional array
int aa[2][5] = {0};
int (*P3) [5] = aa;//points to an element in a two-dimensional array, the element in a two-dimensional array is a one-dimensional array, referring to the method of a one-dimensional array
int (*P4) [5] = &aa[0];//is the same as above, the name of the array is the address of the first element of the array, which is actually an address, that is, the array address of a one-dimensional array.
int *P5 = &aa[0][0]; Point to the first address of the first element (one-dimensional) of the two-dimensional array, with int *p, with exactly the same type
int (*P6) [2][5] = &aa;//Pointer to the two-dimensional array itself. Summary: Change the name of the array in the definition to a pointer, plus ().
Char *p = buf[1];//buf[0] = The number of pointers in the array is the address of the first element, that is, to initialize an address directly to him.
printf ("buf[1] = 0x%x.\n", buf[1]);
printf ("sizeof (buf[1]) =%d.\n", sizeof (buf[1]));
printf ("buf[1] = 0x%x.\n", buf[1]);
printf ("sizeof (BUF) =%d.\n", sizeof (BUF));
Fun (sizeof (BUF)/sizeof (buf[0]), &buf);//Here is the address of the array pointer, type down: The original is an array of pointers, Char *pt[x], if it is
Normal array words, char a[x], if pointed to the array itself should be written char (*P) [x] = &a; According to this, the analogy to an array of pointers is: char * (*P) [x] = &pt;
FUN1 (sizeof (BUF)/sizeof (buf[0]), buf); Similarly
return 0;
}
Operation Result:
Learning notes for pointers and arrays of one-dimensional and two-dimensional arrays and string pointers