TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: Roy McNeill
from: andrew clarke
date: 1995-09-08 10:27:48
subject: string constants

ac>> Hi there.  Why does emx/gcc disallow the modification of string 
 ac>> constants? Or more to the point - why does ANSI/ISO C disallow 
 ac>> this?  Tnx.

 > Hmm.. if the standard says so, shouldn't the compiler have been able to 
 > detect the problem by implicitly declaring str as constant or something 
 > equivalent?

Backward compatibility.  There may be a compiler with an option to do just
that, who knows.  I doubt it though.  gcc/emx is the only compiler I've
come across that enforce the rule, but I'm not aware of any option to tell
it to either enfore the rule at compile-time or not enfore the rule at
run-time.


 ac>> int main(void)
 ac>> {
 ac>>     char *str = "Hello world";
 ac>>     *(str + 5) = '\0';
 ac>>     printf("str: `%s'\n", str);
 ac>>     return 0;
 ac>> }


 > Very interesting. Emphasises the difference between program space and 
 > data space, I spose, cos the "Hello world" in the above
code lives in 
 > the program, not the data. But shouldn't the same rule apply to

 >  int i = 1;

 > ?

It does. :-)  You can't modify the 1.  Makes perfect sense now.  I'm a bit
suprised I haven't come across said rule before - normally I don't have to
go searching for this stuff. 

andrew

--- Msgedsq/2 3.20
* Origin: Blizzard of Ozz, Melbourne, Australia (3:633/267.1{at}fidonet)
SEEN-BY: 50/99 620/243 623/630 632/348 998 633/154 252 260 267 371 373
SEEN-BY: 634/384 635/301 502 503 544 639/100 711/401 409 410 430 510 807 808
SEEN-BY: 711/809 932 934 712/515 713/888 714/906 800/1 7877/2809
@PATH: 633/267 252 371 635/503 50/99 711/808 809 934

SOURCE: echomail via fidonet.ozzmosis.com

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.