How to Code Pointers to Memory in Embedded C

Embedded C for microcontrollers



In this article, we go over how to code pointers to the different types of memory that can be found in a microcontroller.

Pointers are variables that contain the address or location of a variable, constant, function or data object.

So, essentially, pointers represent the addresses of data objects (where the data objects are located).

Since addresses are in memory (without memory, there could be no addresses), the pointers can point to memory addresses in all of its 3 different types of memories.

In a typical microcontroller such as an AVR, there are 3 different types of memories. There is SRAM memory, FLASH memory, and EEPROM memory. To learn more about these different types of memories, see Micrcontroller Memory Types.

We can create pointers that point to each of these 3 types of memory with C for embedded applications.

How to Code Pointers to Memory in C

To create pointers to the different memories (SRAM, FLASH, and EEPROM) with C, we first declare what type of data type the data will be (such as int, char, etc), then we place either nothing for SRAM, flash for FLASH, or eeprom for EEPROM, then put the variable name of the data and then the equals assignment with the value of the data next.

This is how it would look:

/*Pointers to a string that is located in SRAM*/
char *ptr_to_ram = "This string is placed in SRAM";

/*Pointer to a string that is located in FLASH*/
char flash *ptr_to_flash= "This string is placed in FLASH";

/*Pointer to a string that is located in EEPROM*/
char eeprom *ptr_to_eeprom= "This string is placed in EEPROM";

Even though the pointers can point to FLASH and EEPROM memory areas, the pointers themselves are always stored in SRAM. The proper code keywords must be used so that the compile can generate the proper code for accessing the desired region.

HTML Comment Box is loading comments...