一个简单的指针问题
谭浩强 《c程序设计(第四版)》中的一个例子,可是我怎么也编译不过去,不知道是我的问题还是这个例子的问题。
- C/C++ code
#include <stdio.h>int main(){ int a[3][4]={{1,9,5,7},{9,11,13,15},{17,19,21,23}}; printf("%d,%d\n",a,*a); printf("%d,%d\n",a[0],&a[0][0]); printf("%d,%d\n",a[0],*(a+0)); printf("%d,%d\n",a[1],a+1); printf("%d,%d\n",&a[1][0],*(a+1)+0); printf("%d,%d\n",a[2].*(a+2)); printf("%d,%d\n",&a[2],a+2); printf("%d,%d\n",a[1][0],*(*(a+1)+0)); printf("%d,%d\n",*a[2],*(*(a+2)+0)); return 0;}下边是编译过程中遇到的错误。
/Users/apple/Documents/c/test/printf34.c: In function ‘main’:
/Users/apple/Documents/c/test/printf34.c:5: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int (*)[4]’
/Users/apple/Documents/c/test/printf34.c:5: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int *’
/Users/apple/Documents/c/test/printf34.c:5: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int (*)[4]’
/Users/apple/Documents/c/test/printf34.c:5: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int *’
/Users/apple/Documents/c/test/printf34.c:6: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
/Users/apple/Documents/c/test/printf34.c:6: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int *’
/Users/apple/Documents/c/test/printf34.c:6: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
/Users/apple/Documents/c/test/printf34.c:6: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int *’
/Users/apple/Documents/c/test/printf34.c:7: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
/Users/apple/Documents/c/test/printf34.c:7: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int *’
/Users/apple/Documents/c/test/printf34.c:7: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
/Users/apple/Documents/c/test/printf34.c:7: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int *’
/Users/apple/Documents/c/test/printf34.c:8: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
/Users/apple/Documents/c/test/printf34.c:8: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int (*)[4]’
/Users/apple/Documents/c/test/printf34.c:8: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
/Users/apple/Documents/c/test/printf34.c:8: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int (*)[4]’
/Users/apple/Documents/c/test/printf34.c:9: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
/Users/apple/Documents/c/test/printf34.c:9: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int *’
/Users/apple/Documents/c/test/printf34.c:9: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int *’
/Users/apple/Documents/c/test/printf34.c:9: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int *’
/Users/apple/Documents/c/test/printf34.c:10: error: expected identifier before ‘*’ token
/Users/apple/Documents/c/test/printf34.c:10: warning: too few arguments for format
/Users/apple/Documents/c/test/printf34.c:10: warning: too few arguments for format
/Users/apple/Documents/c/test/printf34.c:11: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int (*)[4]’
/Users/apple/Documents/c/test/printf34.c:11: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int (*)[4]’
/Users/apple/Documents/c/test/printf34.c:11: warning: format ‘%d’ expects type ‘int’, but argument 2 has type ‘int (*)[4]’
/Users/apple/Documents/c/test/printf34.c:11: warning: format ‘%d’ expects type ‘int’, but argument 3 has type ‘int (*)[4]’
[解决办法]
printf("%d,%d\n",a[2].*(a+2)); 用 ,
[解决办法]
stdlib.h