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.