🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #1415

initialized 'global' (extern) char arrays

#1415
From: Jim Lynch
Date: Thu, 07 Jan 1993 03:24
40 lines
1594 bytes
I am given to understand that the storage allocated by

char *x = "abcde" ;

is not writable. In terms of whatever standard exists, is it exactly the same as

const char *x = "abcde" ;  // ?

The reason I ask is that I used such a construct in gcc-2.3.2 (yes, I plan to
upgrade) as follows:

char *x = "some string" ;

main()
{
    something(x[0]) ;
}

where the prototype for something looks like

void something(char &z) ;

and modifies that which is referred to by z.

So when something() got around to modifying, the system dumped core, claiming
a segmentation fault. Shouldn't the compiler have warned me?

Also, some internal issues:

Assuming that x is a const char *, does the C compiler see the argument to
something() in main() as needing a type conversion from const char to plain
ol' char? If so, shouldn't the compiler warn about this (possibly probematic)
kind of conversion?

Someone told me that even though the ANSI standard says that x is unwritable,
the compiler won't implement this by making it a const. My feeling is that this
is a mistake: unwritable should imply const (although my assumption was the
other way around: unwritable is implemented through using const.)

-Jim
Technical Details
Message-ID: <1993Jan7.032458.21941@netcom.com>
Path: arc1-ca.pugleaf.net!sparky!uunet!gumby!wupost!csus.edu!netcom.com!jimlynch

Show Full Headers
Complete Article Headers
Newsgroups: comp.std.c++
Path: arc1-ca.pugleaf.net!sparky!uunet!gumby!wupost!csus.edu!netcom.com!jimlynch
From: jimlynch@netcom.com (Jim Lynch)
Subject: initialized 'global' (extern) char arrays
Message-ID: <1993Jan7.032458.21941@netcom.com>
Organization: Netcom Online Communications Services (408-241-9760 login: guest)
Date: Thu, 7 Jan 1993 03:24:58 GMT
Lines: 41