1.指向单个变量的指针;
1 int a = 5; 2 int* p = &a; 3 printf("%d", *p);
2.数组的指针
(1)一维数组的指针
1 int a[5] = { 1,2,3,4,5 }; 2 int *p; 3 p = a; 4 printf("%d ", a[1]); 5 printf("%d ", *(p + 1)); 6 printf("%d", p[1]);
(2)二维数组的指针
(1)列指针
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 ", *(p + 4 * 2 + 3)); //a[i][j] = *(p + i * n + j); //n数组的列数 7 printf("%d", a[2][3]);
(2)行指针 (指向数组的指针)
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 ", a[2][3]); 5 printf("%d", *(*(p + 2) + 3));
3.指向函数的指针
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.指向指针的指针(有时候在想前面的*是不是可以无限写下去……)
1 int a = 5; 2 int* p1, ** p2, *** p3; 3 p1 = &a; 4 p2 = &p1; 5 p3 = &p2; 6 printf("%d", ***p3);
5.指针数组
1 char* p[3] = { "hello", " ", "world" }; 2 printf("%s", *(p + 2));
6.返回指针值的函数
1 int main() 2 { 3 char* myStrcat(char* p1, char* p2); 4 char str[100] = "hello "; 5 char* mes = "world"; 6 printf("%s ", myStrcat(str, mes)); 7 return 1; 8 } 9 char* myStrcat(char* p1, char* p2) 10 { 11 char* start = p1; 12 while (*p1 != '