With my compiler (Apple llvm-gg-4.2) this code:
void fun1(const char *s)
{
char* t = s+1;
}
void fun2(char *s)
{
char* t = s+1;
}
int main(void)
{
char* a;
fun1(a);
fun2(a);
}
gives this warning:
junk.c:3: warning: initialization discards qualifiers from pointer target type
on fun1 but not on fun2. Why?
fun1 is taking const char* and is being assigned to char*
Whereas fun2 is taking a char* and being assigned to char* which is fine.
If you are assigning a constant pointer to a non-const pointer, this means you can modify the const pointer by using the const pointerIn this case, inside fun1 if you do
t[0] = 'a'its not legal because you are modifying const memory, which is why compiler warns you