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 of Array

(1) Pointer of one-dimensional array

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]);

(2) Pointer of two-dimensional array

(1) 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] = *(p + i * n + j);    //n The number of columns in an array
7 printf("%d", a[2][3]);

(2) Line pointer (pointer to 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. Pointer to 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);  //call
 8     return 1;
 9 }
10 int func(int a)
11 {
12     printf("%d", a);
13     return 1;
14 }

4. Pointing to the pointer (sometimes wondering if the front * can be written indefinitely... )

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

5. Pointer Array

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

6. Functions that return pointer values

 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;

The words here are not very precise. According to c Language Programming (Third Edition), pointer is an address, and pointer variable is a variable that stores the address. Pointer and pointer variable are different concepts.

In most cases, however, pointer variables are directly referred to as pointers. In addition to the second point and the sixth point, "pointers" refer to pointer variables.

Tags: C Programming

Posted on Tue, 08 Oct 2019 02:57:14 -0700 by Zergman