puts函數出現warning: passing argument 1 of ‘puts’ from incompatible pointer type(警告:從不兼容的指針類型傳遞“puts”的參數1)


代碼:

 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);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM