Question.2
An array of int is stored in memory starting at address 0x2000. On this platform, sizeof(int) = 4 bytes.
int sensor_data[6] = {10, 20, 30, 40, 50, 60};| Index | Address | Value |
|---|---|---|
| 0 | 0x2000 | 10 |
| 1 | 0x2004 | 20 |
| 2 | 0x2008 | 30 |
| 3 | 0x200C | 40 |
| 4 | 0x2010 | 50 |
| 5 | 0x2014 | 60 |
What is the address of sensor_data[4]?
An array in C is a contiguous block of memory that stores multiple elements of the same data type. It allows you to:
Array Declaration
int numbers[10]; // Array of 10 integers
uint8_t buffer[32]; // 32-byte buffer (commonly used in firmware)
Declares a fixed-size array — memory is allocated statically.
Array Initialization
int values[4] = {10, 20, 30, 40}; // full initialization
int empty[4] = {0}; // all elements = 0
char message[] = "Hi"; // string-style initIf you skip the size (like message[ ]) , the compiler counts the size automatically.
Accessing Array Elements
int arr[3] = {5, 10, 15};
printf("%d", arr[1]); // prints 10
arr[2] = 20; // modify element at index 2The index starts from 0. Always ensure you stay within 0 to n-1.
Array Size with sizeof()
int arr[5];
int total_bytes = sizeof(arr); // e.g., 20 if int = 4 bytes
int element_bytes = sizeof(arr[0]); // gives size of int i.e. 4 bytes
int element_count = sizeof(arr) / sizeof(arr[0]); // gives 5This only works within the same scope where the array is declared (not when passed to a function).
int arr[4] = {10, 20, 30, 40};
Assuming the starting address is 0x2000, the memory looks like:
| Address | Value |
|---|---|
| 0x2000 | 10 |
| 0x2004 | 20 |
| 0x2008 | 30 |
| 0x200C | 40 |
Each int = 4 bytes (on most systems).
In embedded systems, arrays are used for: