使用gcc的警告資訊間接知道變數的型別
#include <stdio.h> #include <stdlib.h> #include <stddef.h> #include <string.h> int main() { int a[2][10]; printf("%d\n", a[0]); printf("%d\n", a); printf("%d\n", a + 1); exit(0); }
[root@localhost transform]# gcc -o test -Wall -g test.c test.c: In function ‘main’: test.c:14: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’ test.c:15: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int (*)[10]’ test.c:16: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int (*)[10]’
gcc 的選項中加入 -Wall 即可