TIP: Click on subject to list as thread! ANSI
echo: aust_c_here
to: All
from: andrew clarke
date: 1995-09-02 13:17:20
subject: string constants

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

Note that the following code when compiled with emx/gcc will cause an
exception error when executed, due to this ANSI/ISO C rule.  When compiled
with Borland C++ for DOS 3.1, Microsoft QuickC 2.5 or Borland C++ for OS/2
1.0, the following code executes without error.  CLint 1.41 also passed the
following code with flying colors.


#include 

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


The following code is a simple work-around for the rule:

#include 
#include 
#include 

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


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