c language pointer summary

1. A pointer to a single variable;

1 int a = 5;
2 int* p = &a;
3 printf("%d", *p);

2. pointer array

(1) one-dimensional array of pointers

1 int a[5] = { 1,2,3,4,5 };
2 int *p;
3 p = a;
4 printf("%d\n", a[1]);
5 printf("%d\n", *(p + 1));
6 printf("%d", p[1]);

Pointer (2) two-dimensional array

  (1) the column pointer

1 int a[3][4] = { 1,2,3,4,5,6,7,8,9,10,11,12 };
2 int* p;
3 //p = &a[0][0];
4 //p = a[0];
5 p = *a;
6 printf("%d\n", *(p + 4 * 2 + 3));  // A [I] [J] = * (n * P + I + J);     // number of columns n in the array 
. 7 the printf ( " % D " , A [ 2 ] [ . 3 ]);

  (2) row pointer (pointer to an array)

1 int a[3][4] = { 1,2,3,4,5,6,7,8,9,10,11,12 };
2 int(*p)[4];
3 p = a;
4 printf("%d\n", a[2][3]);
5 printf("%d", *(*(p + 2) + 3));

3. A pointer to a function

 1 int main()
 2 {
 3     int func(int a);
 4     int (*p)(int a);
 5     int a = 5;
 6     p = func;
 7     (*p)(a);  //调用
 8     return 1;
 9 }
10 int func(int a)
11 {
12     printf("%d", a);
13     return 1;
14 }

4. pointer to a pointer (sometimes wondered front * is not unlimited write down ......)

1 int a = 5;
2 int* p1, ** p2, *** p3;
3 p1 = &a;
4 p2 = &p1;
5 p3 = &p2;
6 printf("%d", ***p3);

The array of pointers

1 char* p[3] = { "hello", " ", "world" };
2 printf("%s", *(p + 2));

6. Return function pointer value

 1 int main()
 2 {
 3     char* myStrcat(char* p1, char* p2);
 4     char str[100] = "hello ";
 5     char* mes = "world";
 6     printf("%s\n", myStrcat(str, mes));
 7     return 1;
 8 }
 9 char* myStrcat(char* p1, char* p2)
10 {
11     char* start = p1;
12     while (*p1 != '\0')
13     {
14         p1++;
15     }
16     while (*p1++ = *p2++)
17     {}
18     return start;
19 }

7. file pointer

  FILE *fp;

Here words are less rigorous. According to "c Programming Language (third edition)", the pointer is an address, and pointer variables are variables to store addresses. "Pointer" and "tag" are different concepts.

However, in most cases, will "tag" directly called "pointer." Here in addition to the second point, a sixth point, the "pointer" refer to "pointer variables."

Guess you like

Origin www.cnblogs.com/ben-/p/11295530.html