Thomas o connor
Thomas o connor

Reputation: 13

How to set a static array in a specific memory location

I am running this c program in gcc. The below works for one variable.

#define dataBlk_tx0 ((volatile unsigned int *) 0x20000ACB)

But if I want to create an array and write to it how would I do this, this array needs to be defined before main?

#define dataBlk_tx0 ((volatile unsigned int * x[8]) 0x20000ACB)

main{

dataBlk_tx0[0] = 5;

}

Upvotes: 1

Views: 330

Answers (1)

GandhiGandhi
GandhiGandhi

Reputation: 1350

If you want to read or write directly to that address and memory, you can do it like this:

  1. Initialize dataBlk_tx0 as a pointer to the specific memory location
  2. Access that memory through the pointer.
volatile unsigned int * dataBlk_tx0 = (unsigned int *)0x20000ACB;

int main () {
    dataBlk_tx0[0] = 5;
    return 0;
}

If you want to create an array in a specific memory region (like Flash vs RAM on a micocontroller), then you'll need to look into linker scripts.

Upvotes: 2

Related Questions