Format specifier '%p' requires 'void *' argument instead of 'char *'?

2018-11-01 21:44:26 +08:00
 codechaser
char *string1 = "foo";
printf("%p\n",string1);

printf这里总是提示Format specifier '%p' requires 'void *' argument instead of 'char *。string1 不就是个指针吗?输出也的确没问题。求教。

2278 次点击
所在节点    C
9 条回复
ysc3839
2018-11-01 21:57:04 +08:00
已经说得很明白了 requires 'void *' argument instead of 'char *'
Monad
2018-11-01 22:09:12 +08:00
https://stackoverflow.com/questions/24867814/printfp-and-casting-to-void
int* 和 void* 纯理论上来讲 这俩不一定要有相同的表示
Tyanboot
2018-11-01 22:59:24 +08:00
c 里面 void*确实是“万能指针”,这样传是不会有 warning 的。不过 c++里面是会有 warning 的。
vsomeone
2018-11-02 00:17:48 +08:00
不知道你的编译器是什么,但是 Linux 上 gcc 这句话没有报错或警告
codechaser
2018-11-02 09:17:45 +08:00
@vsomeone mingw
codechaser
2018-11-02 09:18:02 +08:00
@vsomeone ide 用的 clion
codechaser
2018-11-02 09:47:08 +08:00
@ysc3839 时很明白了,那请问一下为何需要 void * 不是 char *呢?
codechaser
2018-11-02 09:47:42 +08:00
@Monad 谢谢你!
ysc3839
2018-11-02 10:11:35 +08:00
@codechaser 这就要看相关标准了吧?我也不知道。

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/503561

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX