avr-gcc-list
[Top][All Lists]
Advanced

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

Re: [avr-gcc-list] String declaration query


From: Georg-Johann Lay
Subject: Re: [avr-gcc-list] String declaration query
Date: Sun, 06 Jul 2014 21:52:33 +0200
User-agent: Thunderbird 2.0.0.24 (Windows/20100228)

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




reply via email to

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