c语言数组
#include <stdio.h> #include <string.h> void main() { int a[3] = {1, 2, 3}; int *p; int (*s)[3]; p = a; // one dimensional array avaialbe is like a int* type s = &a; printf("%d\n", *(p + 1)); // 2 p = (int*) (&a[0] + 1); printf("%d\n", *(p - 1)); // 1 p = (int*) (a + 1); printf("%d\n", *(p - 1)); // 1 p = (int*) (&a + 1); printf("%d\n", *(p - 1)); // 3 int b[3][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; int **q; //q = b; // compile error s = b; printf("s[2][2] = %d\n", s[2][2]); // 9 printf("*(*(s + 1) + 1) = %d\n", *(*(s + 1) + 1)); // 5 printf("size:%lu\n", sizeof(b[1])); // 12 p = (int *)b[1]; printf("%d\n", *p); // 4 //printf("%d\n", *b); // compile error //printf("%d\n", b[0]); // compile error printf("*b[1] = %d\n", *b[1]); // 4 printf("**b = %d\n", **b); // 1 //printf("%d\n", *(b + 1)); // compile error printf("**(b + 1) = %d\n", **(b + 1)); // 4 printf("%d\n", *(*(b + 1) + 1)); // 5 p = (int *)(&b + 1); printf("%d\n", *(p - 1)); // 9 }
int a[]; 一维数组类似于指针, 本质上和一个int类型的指针很像
int a[][];二维数组类似于指向一维数组的指针和指针的组合。指向一维数组的指针用于检索行。
#include <stdio.h> #include <string.h> void main() { const char *s = "12345"; char a[] = "12345"; a[3] = 'x'; //s[3] = 'x'; // compile error printf("%s\n", a); printf("sizeof(s) = %lu\n", sizeof(s)); // 8 printf("sizeof(a) = %lu\n", sizeof(a)); // 6 }

浙公网安备 33010602011771号