Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

loves me some obscure error message

Name: Anonymous 2009-11-01 2:55

Ok, I've got this code (not exactly as seen here, it's much long and obviously uses real variable names etc. but I just provided the bare minimum so you can see what's going on):

void myfunc(const char *sarg)
{
        char *temp;

        temp = strtok(sarg, ".");

        /* etc.  doesn't matter what I have beyond this code */
}


I get this message:
[Warning] passing arg 1 of `strtok' discards qualifiers from pointer target type

What the hell does that mean?  I'm not too worried about it because it works fine if I just typecast sarg with (char*), but I'd like to know wtf this actually means instead of just assuming that everything will be ok as long as I typecast, since that could potentially lead to some unsafe behaviour that I can't foresee.

Name: Anonymous 2009-11-01 12:22

>>6,9
You're doing it wrong. Anons >>7,8 have it right (though 8 is just because he isn't actually using typedef). Basically the typedef doesn't exist yet until the whole struct has been parsed, so until then you can't use the typedef; just use the struct name itself (by using the struct qualifier).

typedef struct my_struct_s {
  struct my_struct_s* next;
} my_struct;


AFTER this point you can use "my_struct*", but until the typedef is over, you have to use "struct my_struct_s*". Here I'm appending _s to differentiate the struct name from its typedef, but in most compilers you can actually use the same name (it's supposed to be allowed by ANSI C, but some compilers suck).

If it's still finicky, it's because you're fucking up your header files. Try to avoid including header files in other header files as much as possible. Where you would normally include the header file, just forward declare it instead:

typedef struct my_struct_s my_struct;

Only include the header file when you actually need the definition of the struct or its related functions (you don't need the definition for pointers).

Remember, you can always tell GCC to emit the preprocessed source (-E). Then you can look through it yourself to see where your declaration and typedefs are and where you're trying to use them.

>>1
The qualifier it's talking about is 'const'. The error message is not so obscure once you understand this.

DON'T EVER EVER EVER cast away the const modifier. If you would read the documentation, you would realize why it takes a non-const parameter:

str: C string to truncate. The contents of this string are modified and broken into smaller strings (tokens).

It replaces all the delimiters it finds with \0 to give you null-terminated tokens. If you try to use a const char* you get undefined behaviour. It might work on your computer, but on someone else's it will probably halt and catch fire. Don't do it.

Instead strdup() the string. This gives you a mutable copy. Don't forget to free it later.


Sane advice? On MY /prog/? It's more likely than you think

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List