Question.6
A developer computes the number of elements between two pointers:
int arr[6] = {10, 20, 30, 40, 50, 60};
int *start = &arr[1];
int *end = &arr[4];
int count = end - start;What is the value of count?
A pointer is a variable that stores the memory address of another variable.
int x = 10;
int *ptr = &x; // ptr holds address of xPointers are core to C programming — especially in firmware, where direct access to memory, registers, and hardware resources is essential.
int x = 25;
int *p = &x;
printf("%d", *p); // dereferencing → prints 25If x is at 0x2000, and int = 4 bytes:
| Variable | Address | Value |
|---|---|---|
| x | 0x2000 | 25 |
| p | 0x3000 | 0x2000 |
| Operation | Description |
|---|---|
| int *p | Declare pointer to int |
| p = &x | Assign address of x to p |
| *p = 5 | Modify value pointed to by p |
| p++ | Move to next int (e.g., next array cell) |
| *(arr + i) | Access array like pointer |
int arr[3] = {10, 20, 30};
int *p = arr;
printf("%d", *(p + 2)); // prints 30
In C, arr[i] is equivalent to *(arr + i)
int *ptr = NULL;
int x = 10;
int *p = &x;
int **pp = &p;Used when you want to modify the original pointer itself (e.g., inside a function).
const int *p1; // pointer to constant data (can’t modify *p1)
int *const p2; // constant pointer (can’t change p2)
const int *const p3; // const pointer to const dataUseful when:
A void * is a generic pointer — it can point to any type but must be cast before dereferencing.
void *ptr;
int x = 10;
ptr = &x;
printf("%d", *(int *)ptr); // cast requiredCommon in:
You can pass a pointer to allow a function to modify variables.
void update(int *p) {
*p = 99;
}
int main() {
int val = 10;
update(&val);
// val now = 99
}Used for:
Function pointers let you store and call functions dynamically.
void blink() { printf("LED Blink\n"); }
void (*func_ptr)() = blink;
func_ptr(); // calls blinkExample
#include <stdio.h>
// Normal function
int add(int a, int b) {
return a + b;
}
int main() {
// Declare a function pointer
int (*fp)(int, int) = add;
// Call function using pointer
int result = fp(5, 3);
printf("%d\n", result); // Output: 8
return 0;
}Used in:
Pointer arithmetic means performing arithmetic operations like +, -, ++, or -- on pointers, to move them through memory.
int arr[3] = {10, 20, 30};
int *p = arr;
p++; // now p points to arr[1]In embedded systems, pointer arithmetic is widely used to:
When you do p + 1, it doesn’t move 1 byte — it moves to the next element based on the pointer’s type:
| Pointer Type | Bytes Jumped by p + 1 |
|---|---|
| int * | 4 bytes (on 32-bit MCU) |
| char * | 1 byte |
| double * | 8 bytes |
So:
p++ means: go to the next element of type (not next byte)int nums[3] = {1, 2, 3};
char bytes[3] = {'a', 'b', 'c'};
int *iptr = nums;
char *cptr = bytes;
iptr++; // jumps 4 bytes → now points to nums[1]
cptr++; // jumps 1 byte → now points to bytes[1]int arr[5] = {5, 10, 15, 20, 25};
int *p = arr;
printf("%d", *(p + 3)); // prints 20
Same as arr[3] — this works with any type.uint8_t data[4] = {1, 2, 3, 4};
uint8_t *ptr = data;
for (int i = 0; i < 4; i++) {
printf("%d ", *(ptr + i)); // pointer arithmetic
}
Pointers are essential in:
You often use uint8_t *ptr = (uint8_t *)base_addr for hardware access.