代碼:
1 /************************************************************************* 2 > File Name: ptr_variable.c 3 > Author: Mr.Yang 4 > Purpose:演示指向變量的指針 5 > Created Time: 2017年06月03日 星期六 08時47分33秒 6 ************************************************************************/ 7 8 #include <stdio.h> 9 #include <stdlib.h> 10 11 int main(void) 12 { 13 int a = 1000; 14 printf("a的地址為:%d\n",&a); 15 16 int *p; 17 puts(p); 18 printf("指針p的地址為:%d\n",&p); 19 20 return 0; 21 }
錯誤完整描述:
1 ptr_variable.c: In function ‘main’: 2 ptr_variable.c:17:7: warning: passing argument 1 of ‘puts’ from incompatible pointer type 3 puts(p); 4 ^ 5 In file included from ptr_variable.c:8:0: 6 /usr/include/stdio.h:695:12: note: expected ‘const char *’ but argument is of type ‘int *’ 7 extern int puts (const char *__s); 8 ^
由描述可知,puts需要const char *但是參數給了 int *,通過查詢puts函數的用法(如下:附)可知,里面是一個字符串參數,不是int型參數,所以上面的代碼可把int *p改為char *p即可編譯通過。
附:puts()函數用於將一字符串放到標准輸出流(stdout)中,並在最后增加換行符 '\n',其函數原型如下:
int puts(char *string);