Reputation: 567
In the C SDL project I am working on, I typedef
ed char *
to str
for readability.
Now when I do:
const str title = SDL_GetWindowTitle(win);
where SDL_GetWindowTitle
returns a const char *
, I get:
warning: return discards ‘const’ qualifier from pointer target type [-Wdiscarded-qualifiers]
The warning is removeed when I change the type to char *
:
const char *title = SDL_GetWindowTitle(win);
A typedef is merely an alias to a type, right? So declaring a variable as str
or char *
should be equivalent, why do I get that warning? Or is there something I missed...?
I am using GCC on the CLI, so it's not an IDE's fault.
Thx in advance!
Upvotes: 2
Views: 2808
Reputation: 33694
typedef
s are not macro substitution, so in your case
const str
const char *
are different types. The former is actually equivalent to:
char *const
This is a const
value of char *
type, so it points to a mutable string. In your example, you cannot modify title
, but you could modify *title
through that pointer (if it actually points to non-const
memory, which depends on what SDL_GetWindowTitle
does).
You would have to add a separate typedef
for const char *
to fix this.
Upvotes: 5