4

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

#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 the typedef is removed:

#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 whether using Cygwin or MinGW tools, or whether 32 vs 64 bit.

  • Error is eliminated if I avoid either the struct or the typedef. But error is present whenever using both typedef and struct together as shown.

  • Before posting, I examined (among others) stackoverflow.com/questions/20944784/, which advises use of const. However, error persists whether typedef is

    pointer to constant char (typedef char const * STR); constant pointer to char (typedef char * const STR); or constant pointer to constant char (typedef char const * const STR);

  • Verbatim error message: Mismatching the argument type "char" and conversion specifier "s".

  • Further testing shows that ms.str is, indeed, of the expected type char * (e.g., proper sizeof, swapping conversion specifier to "c" for char as indicated by error message results in gibberish as expected if printf'ing pointer to char as a char, etc.)

  • Changing the name of the typedef identifier (e.g., from STR to STR_TEST) results in same error. So there appears to be no conflict with definitions in standard headers.

  • 9
    Which compiler are you using? – Sourav Ghosh May 26 '17 at 06:07
  • 1
    Does it work if you use a different name for the typedef? could `STR` be #defined as something odd? – AShelly May 26 '17 at 06:16
  • Both pieces compile fine with me, so I guess it has to do with your compiler version. – Marievi May 26 '17 at 06:20
  • VS 2015 x64 works fine – Ivan Ivanov May 26 '17 at 06:22
  • GCC win x64 works fine – Ivan Ivanov May 26 '17 at 06:25
  • Ubuntu x64 GCC works fine – Ivan Ivanov May 26 '17 at 06:27
  • 1
    Ok, MAC x64, returns warning - Due to C++11 standards `projects/Test/Test/main.cpp:24:27: ISO C++11 does not allow conversion from string literal to 'STR' (aka 'char *')`. Looks like https://stackoverflow.com/questions/20944784/why-is-conversion-from-string-constant-to-char-valid-in-c-but-invalid-in-c . I suppose you use C++ compiler instead of C. – Ivan Ivanov May 26 '17 at 06:32
  • 1
    These codes have exactly the same defined behaviour. It could either be a compiler bug, or the compiler showing some message that it thinks is useful information. Show the exact compiler message you get – M.M May 26 '17 at 07:13
  • 2
    It is likely that under your compiler, there's a definition involving "`STR`" in a standard header file such as `` or ``. Your compiler should *not* be doing this, but it's a common sort of problem under poor compilers. What is the exact error message you're getting? What happens if you use, say, `STRR` instead of `STR`? – Steve Summit May 26 '17 at 10:09
  • In NetBeans IDE 8.2, I've compiled the code with gcc compiler via cygwin (64), mingw (32), and mingw (64). Same warning in all three trials. Also, changing the typedef identifer from STR to, say, STR_TEST, results in the same warning -- so it doesn't appear to be a conflict with definitions in the standard headers. Warning verbatim: <>. – Bloody Peasant May 26 '17 at 16:53
  • @IvanIvanov Before posting, I had looked at stackoverflow.com/questions/20944784/ which advises use of const. However, all three of the following typedef's lead to same warning: (1) typedef char * const STR; (2) typedef const char * STR; and (3) typedef const char * const STR;... So, I think it must be a different issue... Something about pointing to a typedef'd string through a struct is more specifically the issue. There is no error or warning if printf'ing a typedef'd pointer to character without the invervening struct. – Bloody Peasant May 26 '17 at 17:06
  • 1
    I cannot reproduce your problem on Cygwin x64. Furthermore, "Mismatching the argument type "char" and conversion specifier "s"" doesn't look like a GCC warning, is probably a Netbeans issue. See also [this similar Netbeans bug report](https://netbeans.org/bugzilla/show_bug.cgi?id=258511) – rustyx May 26 '17 at 18:51
  • 2
    @BloodyPeasant gcc does not have an error message that says "Mismatching the argument type "char" and conversion specifier "s"." , it may be NetBeans that shows the error message instead of gcc, in which case NetBeans is wrong, and also have quite poor English grammar in its error messages. – nos May 26 '17 at 19:33
  • @nos Ha, so poor grammar is the telltale sign that it's a NetBeans issue... Looking under the hood of NetBeans, it appears that you and RustyX are correct that this is just an IDE glitch. Thanks, all. – Bloody Peasant May 27 '17 at 02:01
  • Just another good example of why it is good to read [Is it a good idea to **typedef** pointers](http://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers). – David C. Rankin Jun 17 '17 at 05:01

1 Answers1

1

In the echo-chambers that are the comments for this question, it appears those participants and the OP have come to a consensus that this is the fault of NetBeans.

I cannot reproduce your problem on Cygwin x64. Furthermore, "Mismatching the argument type "char" and conversion specifier "s"" doesn't look like a GCC warning, is probably a Netbeans issue. See also this similar Netbeans bug report – RustyX May 26 at 18:51

@BloodyPeasant gcc does not have an error message that says "Mismatching the argument type "char" and conversion specifier "s"." , it may be NetBeans that shows the error message instead of gcc, in which case NetBeans is wrong, and also have quite poor English grammar in its error messages. – nos May 26 at 19:33

@nos Ha, so poor grammar is the telltale sign that it's a NetBeans issue... Looking under the hood of NetBeans, it appears that you and RustyX are correct that this is just an IDE glitch. Thanks, all. – Bloody Peasant May 27 at 2:01

... and so your question needs a correction:

Why does the compiler [NetBeans] complain about a mismatched argument type "char" and conversion specifier "s" in the following printf?

Those who wrote NetBeans are people capable of making mistakes, and this error message is the manifestation of one of those mistakes, the symptom of what we commonly call a "bug", "glitch" or "software fault".

Community
  • 1
  • 1
autistic
  • 1
  • 3
  • 35
  • 80