codechaser
V2EX  ›  C

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

  •  
  •   codechaser · Nov 1, 2018 · 3234 views
    This topic created in 2947 days ago, the information mentioned may be changed or developed.
    char *string1 = "foo";
    printf("%p\n",string1);
    

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

    9 replies  •  2018-11-02 10:11:35 +08:00
    ysc3839
    ysc3839
    Nov 1, 2018
    1
    已经说得很明白了 requires 'void *' argument instead of 'char *'
    Monad
    Monad
    Nov 1, 2018❤️2
    2
    https://stackoverflow.com/questions/24867814/printfp-and-casting-to-void
    int* 和 void* 纯理论上来讲 这俩不一定要有相同的表示
    Tyanboot
    Tyanboot
    Nov 1, 2018
    3
    c 里面 void*确实是“万能指针”,这样传是不会有 warning 的。不过 c++里面是会有 warning 的。
    vsomeone
    vsomeone
    Nov 2, 2018
    4
    不知道你的编译器是什么,但是 Linux 上 gcc 这句话没有报错或警告
    codechaser
    codechaser
    OP
    Nov 2, 2018 via Android
    5
    @vsomeone mingw
    codechaser
    codechaser
    OP
    Nov 2, 2018 via Android
    6
    @vsomeone ide 用的 clion
    codechaser
    codechaser
    OP
    Nov 2, 2018
    7
    @ysc3839 时很明白了,那请问一下为何需要 void * 不是 char *呢?
    codechaser
    codechaser
    OP
    Nov 2, 2018
    8
    @Monad 谢谢你!
    ysc3839
    ysc3839
    Nov 2, 2018 via Android
    9
    @codechaser 这就要看相关标准了吧?我也不知道。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4325 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 08:33 · PVG 16:33 · LAX 00:33 · JFK 03:33
    ♥ Do have faith in what you're doing.