[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [avr-gcc-list] Bizarre string problem

From: Joerg Wunsch
Subject: Re: [avr-gcc-list] Bizarre string problem
Date: Thu, 9 Sep 2004 14:22:29 +0200 (MET DST)

Richard Urwin <address@hidden> wrote:

>> char t[2] = "T";

> I don't remember that being standard C.

It is.  It's more commonly written as

char t[] = "T";

though.  For example, the PSTR() macro does exactly something like
that internally.  (That's also the reason why PSTR() doesn't aggregate
identical strings.)

Technically, it creates a variable that is initialized with 'T', and
'\0', but remains a variable in all respects (i.e. you're free to
modify both elements of t[]).  In contrast,

char *t = "T";

declares a pointer to the (anonymous) array containing 'T', followed
by '\0', but the compiler is allowed to place the anonymous array into
read-only memory, and it is allowed to aggregate these anonymous
arrays in case more of them appear in the same translation unit.  You
are not allowed to modify the object t is pointing to, i.e. you gotta
handle it as if it had been declared to be

const char *t = "T";

J"org Wunsch                                           Unix support engineer
Wir stellen aus! Auf der SYSTEMS 2004  vom 18.-22. Oktober in München
Halle B 3, Stand 320-206 (Partner-Stand von Sun Microsystems)
Halle B 2, Stand 605 "Die Musterfirma"

reply via email to

[Prev in Thread] Current Thread [Next in Thread]