Skip to content

Instantly share code, notes, and snippets.

@k0001
Created April 19, 2012 16:53
Show Gist options
  • Save k0001/2422264 to your computer and use it in GitHub Desktop.
Save k0001/2422264 to your computer and use it in GitHub Desktop.
C const rules cheat shit
typedef struct {
int a;
const int b;
} thing_t;
int main(int argc, char *argv[])
{
thing_t t1 = { .a = 1, .b = 2 };
t1.a = 90;
// t1.b = 90; // NO!
const thing_t t2 = { .a = 3, .b = 4 };
// t2.a = 90; // NO!
// t2.b = 90; // DOUBLE NO!
thing_t * const p0 = &t1;
p0->a = 90;
// p0->b = 90; // NO!
// p0 = &t1; // NO!
const thing_t *
// p1->a = 90; // NO!
// p1->b = 90; // DOUBLE NO!
p1 = &t1;
const thing_t * const p2 = &t1;
// p2->a = 90; // NO!
// p2->b = 90; // DOUBLE NO!
// p2 = &t1; // NO!
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment