joH1
joH1

Reputation: 567

why does GCC give me this `-Wdiscarded-qualifiers` warning?

In the C SDL project I am working on, I typedefed 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

Answers (1)

Florian Weimer
Florian Weimer

Reputation: 33694

typedefs 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

Related Questions