Vybhav Jayaraman
Vybhav Jayaraman

Reputation: 9

How to point a structure to a particluar address

I am trying to point a structure to a particular memory location , can anyone please tell me how it can be done in c programming . I have defined a macro which holds the starting address of the memory location.

#define START_ADDR 0xF6000 //starting address

typedef struct {
     uint32_t checksum;
     uint16_t index[len];
} block;

I changed the memory allocation of block using the below statement:

block *value = (block*) START__ADDR;

I verified the change in memory allocation as well and no issue with it.I am working on FLASH memory and i have verified that writing operation can be done on the particular memory location which i have mentioned. Now i am trying to update the value of checksum using

value->checksum=0xa5a5a5a5;

But the value of checksum is 0x00000000 and not getting updated to 0xa5a5a5a5. Can anyone please tell me how can i change the value of checksum.

Thanks in advance.

Regards Vybhav

Upvotes: 0

Views: 88

Answers (2)

A.K.
A.K.

Reputation: 989

You need to cast the address to a pointer to a struct block object. Also you need an instance of a struct block object to assign the casted pointer.

See this example:

struct block * volatile block_pointer = (struct block*)START_ADDR;

Upvotes: 0

Jose
Jose

Reputation: 3460

Considering that you do know what you are setting or landing there (and you have permissions to do that), two main ways are:

a) Derefering a pointer which points to the address:

#define ADDR 0xXXXXXX
unsigned int volatile * const var = (unsigned int *) ADDR;

Your specific case:

typedef struct {
    uint32_t checksum;
    uint16_t index[len];
} block_t;

unsigned block_t volatile * const block = (block_t *) ADDR;
block->checksum = 0;

b) Through the linker:

file.c:

block_t __attribute__((section(".theSection"))) block;
block.checksum = 0;

linker.ld:

SECTIONS
{
    .theSegment 0xXXXXXX : {KEEP(*(.theSection))}
}

Note that I have renamed the type block to block_t.

Upvotes: 1

Related Questions