1

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;

}
2
  • 5
    Why do you need to create an array? The array is already there, it's the memory dataBlk_tx0 points to. Just use it like this: dataBlk_tx0[0], dataBlk_tx0[1] etc. Just use #define dataBlk_tx0 ((volatile unsigned int *) 0x20000ACB) (without the final ;) Commented Nov 24, 2020 at 13:47
  • You could define it as #define dataBlk_tx0 ((volatile unsigned int (*)[8]) 0x20000ACB), but then you would need to access the elements using constructions such as (*dataBlk_tx0)[0] = 5; or dataBlk_tx0[0][0] = 5;, so it would be much simpler to follow @Jabberwocky's suggestion in the comment above to avoid the need for the extra level of dereferencing/indexing. Commented Nov 24, 2020 at 13:56

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.