Re: String declaration query

Georg-Johann Lay <[email protected]>
Newsgroups gmane.comp.hardware.avr.gcc
Message-ID <[email protected]>
Royce Pereira schrieb:
> Hi,
> 
> What is the differenence between:
> 
> __flash const char myString[] = "Hello There!" ;

myString is a const array in flash that's initialized with "Hello There!".

> and
> 
> __flash const char *myString = "Hello There!" ;

myString is a non-const pointer in RAM that holds a const pointer to 
flash, which is initialized with the address of string literal "Hello 
There!".


> AFAIK, both should be same, but the 2nd gives the error:

No, they are not the same.  For example, you can assign a value to the 
second myString, but not to the first even if it's not const.

The second resides in RAM whereas the first is located in flash, etc.

> " ... initializer element is not computable at load time"

The address of string literal "Hello There!" is an address to the 
generic address space which cannot be turned into an address to a 
different address space at compile time.

IMO this is a shortcoming in the Embedded-C paper, because myString 
holds the only reference to "Hello There!" and it cannot be accessed 
otherwise.  However, in C the left side of an assignment has no effect 
on the right side, which applies to Embedded-C, too (because Embedded-C 
does not specify it).

If you want to locate the literal in a different address space, you can 
initialize myString with the address of a matching compound literal:

__flash const char *myString = (const __flash char[]) { "Hello There!" };

This only works if myString has file scope.

If the compound literal is more complicated, it works similar:

typedef struct
{
     int a;
     const __memx char *c;
} ac_t;

const __flash ac_t *ac = & (const __flash ac_t)
     {
         23,
         (const __memx char[]) { "Hello There!" }
     };



Johann
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.