codechaser
V2EX  ›  C

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

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