[Assorted-commits] SF.net SVN: assorted: [440] sandbox/trunk/src/c/consts.c
Brought to you by:
yangzhang
From: <yan...@us...> - 2008-02-15 02:44:59
|
Revision: 440 http://assorted.svn.sourceforge.net/assorted/?rev=440&view=rev Author: yangzhang Date: 2008-02-14 18:45:03 -0800 (Thu, 14 Feb 2008) Log Message: ----------- added const test Added Paths: ----------- sandbox/trunk/src/c/consts.c Added: sandbox/trunk/src/c/consts.c =================================================================== --- sandbox/trunk/src/c/consts.c (rev 0) +++ sandbox/trunk/src/c/consts.c 2008-02-15 02:45:03 UTC (rev 440) @@ -0,0 +1,66 @@ +// Exploring const-ness in C. It's actually the same in C++ (modulo C++-only +// concepts like classes). Obvious results. + +struct S +{ + int x; + char *s; + const char *c; +}; + +int +main() +{ + { + const char* s = "hello world"; + + // Compile error on this. + // s[0] = 'H'; + } + { + const struct S *p = 0; + + // Compile error on this. + // p->x = 0; + + // No compile error on this. + p->s[0] = '\0'; + + // Compile error on this. + // p->c[0] = '\0'; + + // No compile error on this. + p = 0; + } + { + struct S * const p = 0; + + // No compile error on this. + p->x = 0; + + // No compile error on this. + p->s[0] = '\0'; + + // Compile error on this. + // p->c[0] = '\0'; + + // Compile error on this. + // p = 0; + } + { + const struct S * const p = 0; + + // Compile error on this. + // p->x = 0; + + // No compile error on this. + p->s[0] = '\0'; + + // Compile error on this. + // p->c[0] = '\0'; + + // Compile error on this. + // p = 0; + } + return 0; +} This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |