When I compile this code,
#include <stdio.h>
int *foo();
int main()
{
*foo()++;
return 0;
}
int *foo()
{
static int bar;
return &bar;
}
Clang shows me an error:
static2.c:7:8: error: expression is not assignable
Why it's illegal? I suppose bar have static storage duration, so its lifetime is the entire execution of the program. Although bar itself isn't visible to main(), a pointer should be able to modify it.
This version of foo() doesn't work too, and Clang gives me the same error:
int *foo()
{
static int bar;
static int* ptr = &bar;
return ptr;
}