1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| char *p = "abc"; p[0] = 'd'; printf("%s", p);
char p[] = "abc"; p[0] = 'd'; printf("%s", p);
把另一个数组中每个元素的地址赋给指针数组的每个元素。 int *a[5], b[] = {1, 2, 3, 4, 5}; for (int i = 0; i < 5; i++) a[i] = &b[i];
char *c[4] = {"if", "else", "for", "while"}; for (int i = 0; i < 4; i++) puts(c[i]);
char *c[] = {"if", "else", "for", "while", NULL}; for (int i = 0; c[i] != NULL; i++) puts(c[i]);
char *a[5] = {"abcd", "efghi"}; char **p = a; printf("%c\n", *(*(p + 1) + 2)); printf("%c\n", *(p[1] + 2)); printf("%s\n", p[1]); printf("%s\n", *(p + 1));
int a[4][4] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16}; int (*p)[4] = a; printf("%d\n", *(*(p + 1) + 1)); printf("%d\n", *(p[1] + 1));
char str[][10] = {"qaz", "wsx", "edc"}; char (*s)[10] = str; printf("%s\n", s); printf("%s\n", s[0]); printf("%c\n", s[1][1]); printf("%s\n", s[1] + 1); printf("%s\n", *(s + 1)); printf("%c\n", *(*(s + 1) + 1));
难点!&a和a; 数组名 a 可以作数组的首地址,而&a 是数组的指针。 int a[5] = {1, 2, 3, 4, 5}; int *ptr = (int *)(&a + 1); printf("%d,%d", *(a + 1), *(ptr - 1));
int a[5] = {1, 2, 3, 4, 5}; int *ptr = (int *)(a + 1); printf("%d,%d", *(a + 1), *(ptr - 1));
|