Why does the compiler see a mismatch between char * and the printf "s" conversion specifier when char * typedef'd and is accessible through the structure?

Why does the compiler complain about the inconsistent argument type "char" and the conversion specifier "s" in the following printf file?

#include <stdio.h>
#include <stdlib.h>
typedef char * STR;     // causes problems in printf below
int main(void) {
    struct MyStruct {
        STR str;
    };
    struct MyStruct ms = {"some text"};
    printf("%s\n", ms.str);
    return (EXIT_SUCCESS);
}

The compiler has no complaint about the same code when deleting a typedef:

#include <stdio.h>
#include <stdlib.h>
//typedef char * STR;      // runs fine without typedef
int main(void) {
    struct MyStruct {
        char * str; //STR str;
    };
    struct MyStruct ms = {"some text"};
    printf("%s\n", ms.str);
    return (EXIT_SUCCESS);
}

Notes:

  • System = Win 64, NetBeans IDE 8.2, GCC compiler, no diff, regardless of whether the tools use Cygwin or MinGW or 32 or 64 bits.

  • The error is resolved if I avoid either a structure or a typedef. But an error is present whenever both typedef and struct are used, as shown.

  • ( ) stackoverflow.com/questions/20944784/, . , , typedef

    char (typedef char const * STR); char (typedef char * const STR); char (typedef char const * const STR);

  • Verbatim: "char" "s".

  • , ms.str - char * (, sizeof, "c" char, , , char char ..)

  • typedef (, STR STR_TEST) . , .

+4
1

-, , , OP , NetBeans.

Cygwin x64. , " " char " " s " GCC, , Netbeans. . Netbeans - RustyX 26 18:51

@BloodyPeasant gcc " " char " "s".", NetBeans, gcc, NetBeans , . - nos 26 19:33

@nos Ha, - , NetBeans... NetBeans, , RustyX , IDE. . - 27 2:01

... :

[NetBeans] "char" s printf?

, NetBeans, , , , , "", "" " ".

0

Source: https://habr.com/ru/post/1677962/


All Articles