reassigning value of a pointer

F

Flash Gordon

The above assignment loses the const qualifier.

No it doesn't. One of the quirks of the C language is that it is *not*
const qualified. Modifying string literals is undefined behaviour
(forbidden) because the standard explicitly states that it is and for no
other reason.
> In case you
are using gcc, -Wwrite-strings will emit warnings. Use arrays if
you want to modify content.

This is correct.
#include <stdio.h>
int main (void)
{
char *string = "testing";
printf("%s\n", string);
return 0;
}

There is nothing wrong with the above code. The standard does not
require a diagnostic and it does not invoke undefined behaviour.
gcc: aliased to gcc -Wall -W -ansi -pedantic


str.c: In function `main':
str.c:4: warning: initialization discards qualifiers from pointer
target type

I agree that the warning is useful. Just not required by the standard
and not provided by all compilers.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,920
Members
47,462
Latest member
ChanaLipsc

Latest Threads

Top