Dmitry
Dmitry

Reputation: 704

C/C++ predefine array with memory values

I want to make a predefinition of some array with data from specific memory location, but it just not working for some reason.

// Location of first byte in memory
#define MEMORY   0x7800

// Take a data from memory to array which will be containing the data
const uint8_t *MemArray[3] = { (const uint8_t *)MEMORY, 
                               (const uint8_t *)MEMORY+1, 
                               (const uint8_t *)MEMORY+3 };

// Now, I want to define some another array with values upper, but it not working
const uint8_t TargetArray[7] = { 1,2,3,4, 
                                 (const uint8_t)(*MemArray[0]), 
                                 (const uint8_t)(*MemArray[1]), 
                                 (const uint8_t)(*MemArray[2]) };

Anybody know how to do it?

Compiler gets a warning and error at (const uint8_t)(*MemArray[i]) lines

Warning[Pe191]: type qualifier is meaningless on cast type D:..\Source\file.c

Error[Pe028]: expression must have a constant value D:..\Source\file.c

I'm using IAR compiler for 8051 core

Upvotes: 0

Views: 289

Answers (2)

wkz
wkz

Reputation: 2263

Are the addresses associated with a very slow device? Otherwise you can just have a const pointer point to that location and read out the values at will.

Something like this:

#define MEMORY 0x7800

const struct __attribute__ ((__packed__)){
    uint8_t foo;
    uint8_t bar;
    uint8_t __reserved;
    uint8_t baz;
} *mem = (const void *)MEMORY;

You can read the locations by mem->foo etc.

Note: The exact syntax for struct packing varies, used GCC's in this example.

Upvotes: 0

unwind
unwind

Reputation: 399813

Initializer values must be compile-time constant. Thus, they can't depend on values read out of the memory, the compiler must be able to generate the initialized array.

You need to include actual code to set this up. Just do that first in main(), before the rest of your program.

Upvotes: 3

Related Questions