TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: Roy McNeill
from: Paul Edwards
date: 1995-09-04 23:51:20
subject: string constants

ac> Note that the following code when compiled with emx/gcc will cause an
ac> exception error when executed, due to this ANSI/ISO C rule.

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

I guess what you want is something like "const char *str",
presumably they allow normal assignment for compatibility
with the trillions of lines of code already in existance.

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


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

RM>  int i = 1;

That's a very good example.  Modifying the literal "Hello world"
is the equivalent of modifying the "1" in "i = 1".  Since the
compiler would probably generated "MOV EAX, 1" or somesuch for
the latter, I'd like to see you try and modify the "1" to 
something else.  BFN.  Paul.
@EOT:

---
* Origin: X (3:711/934.9)

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™.