0
0
Power-electronicsHow-ToBeginner · 4 min read

How to Use Pointers in Embedded C: Syntax and Examples

In embedded C, use pointers to store memory addresses, allowing direct access to hardware registers or variables. Declare a pointer with type *name, assign it an address using &, and access the value via * dereferencing.
📐

Syntax

In embedded C, a pointer is declared by specifying the data type it points to, followed by an asterisk * and the pointer name. You assign it the address of a variable using the address-of operator &. To access or modify the value at that address, use the dereference operator *.

  • Declaration: type *pointerName;
  • Assign address: pointerName = &variable;
  • Dereference: *pointerName to get or set the value
c
int *ptr; // Declare a pointer to int
int var = 10;
ptr = &var; // Assign address of var to ptr
int value = *ptr; // Dereference ptr to get var's value
💻

Example

This example shows how to use pointers to read and modify a variable's value in embedded C. It also demonstrates how pointers can be used to access hardware registers by simulating a memory address.

c
#include <stdio.h>

int main() {
    int sensorValue = 100; // Simulated sensor data
    int *ptrSensor = &sensorValue; // Pointer to sensorValue

    printf("Original sensor value: %d\n", *ptrSensor);

    // Modify sensor value using pointer
    *ptrSensor = 200;

    printf("Modified sensor value: %d\n", sensorValue);

    // Simulate hardware register access
    volatile unsigned char *port = (volatile unsigned char *)0x40021000; // Example address
    // Normally, you would write or read hardware registers like this:
    // *port = 0xFF; // Set port bits

    return 0;
}
Output
Original sensor value: 100 Modified sensor value: 200
⚠️

Common Pitfalls

Common mistakes when using pointers in embedded C include:

  • Using uninitialized pointers which can cause crashes or unpredictable behavior.
  • Dereferencing NULL or invalid pointers leading to memory faults.
  • Confusing the pointer itself with the value it points to.
  • Forgetting to use volatile when accessing hardware registers, which can cause the compiler to optimize away necessary reads or writes.

Always initialize pointers before use and use volatile for hardware addresses.

c
#include <stdio.h>

int main() {
    int *ptr; // Uninitialized pointer
    // *ptr = 10; // WRONG: dereferencing uninitialized pointer causes crash

    int var = 5;
    ptr = &var; // Correct initialization
    *ptr = 10; // Safe to modify var through pointer

    printf("Value of var: %d\n", var);
    return 0;
}
Output
Value of var: 10
📊

Quick Reference

Keep these tips in mind when using pointers in embedded C:

  • Declare pointers with the correct data type.
  • Always initialize pointers before dereferencing.
  • Use & to get a variable's address.
  • Use * to access or modify the value at the pointer's address.
  • Use volatile for pointers to hardware registers.
  • Be careful with pointer arithmetic and avoid invalid memory access.

Key Takeaways

Pointers store memory addresses and allow direct access to variables or hardware in embedded C.
Always initialize pointers before using them to avoid crashes or undefined behavior.
Use the dereference operator * to read or write the value a pointer points to.
Mark pointers to hardware registers as volatile to prevent compiler optimizations.
Be cautious with pointer arithmetic and never dereference NULL or invalid pointers.