Embedded C Module3 Copy (2)
Embedded C Module3 Copy (2)
Example
arr[0] = 1; // Sets the first element to 1
Standard C Example
int main() {
int values[5] = {5, 10, 15, 20, 25};
int sum = 0;
for (int i = 0; i < 5; i++) {
sum += values[i];
}
printf("Sum of values: %d\n", sum);
return 0;
}
Definition
Multi-dimensional arrays are arrays of arrays.
They are used to represent data in more than one dimension, such as
matrices.
Example
multiArr[0][1] = 5; // Element at row 0, column 1 to 5
Declaration Syntax
type arrayName[size1][size2];
Embedded C Context
Such arrays can represent physical layouts in hardware, like an LED matrix,
with each element controlling the state of an LED.
Initialization Syntax
type arrayName[size1][size2] = {{val1, val2}, {...}};
Standard C Example
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
Embedded C Application
Initializing state matrices for devices like displays where each element
represents a pixel or segment state.
Accessing Elements
Use row and column indices to access elements in a multi-dimensional
array.
arrayName[row][column]
Standard C Example
int value = matrix[1][2]; // Accesses the element at second ro
Embedded C Context
For embedded systems, ensure the indices are within bounds to maintain
system stability.
Standard C Example
for(int i = 0; i < 2; i++) {
for(int j = 0; j < 3; j++) {
printf("%d ", matrix[i][j]);
}
printf("\n");
}
Embedded C Consideration
In embedded systems, nested loops are commonly used for scanning or controlling a grid of
sensors or actuators.
Embedded C Application
Matrix addition can be used in embedded systems for combining data from
multiple sensor arrays.
Embedded C Significance
Understanding memory layout is crucial in embedded systems for
optimizing data storage and access patterns.
Standard C Example
int array[5];
int *ptr = array;
printf("%p %p", ptr, ptr + 1); // Prints contiguous addresses
Embedded C Application
Directly manipulating memory addresses is common in embedded systems,
for instance when interfacing with hardware registers.
Embedded C Scenario
Searching through a data array to find a sensor reading that exceeds a
threshold could trigger an event or alert.
Module-3 Arrays and Strings 19/50
Strings in C: A Special Kind of Array
Embedded C Example
char errorMessage[20] = "Error Code: ";
Note
String initialization automatically includes the null terminator.
Embedded C Considerations
In embedded systems, functions like ‘sprintf‘ and ‘sscanf‘ are used for
formatting strings to interact with hardware or protocol messages.
Common Functions
‘strlen‘ - Get string length
‘strcpy‘ - Copy string
‘strcat‘ - Concatenate strings
‘strcmp‘ - Compare two strings
Embedded C Application
String concatenation might be used in embedded systems for creating log
messages or protocol frames.
Function Definition
void functionName(parameters) {
// Code to execute
}
Note
Function prototypes are often declared in header files, while definitions are
in source files.
Calling a Function
functionName(arguments);
Example
void turnOnLED(int ledNumber);
turnOnLED(1); // Turns on LED number 1
Embedded C Tip
Ensure that any functions that interface with hardware are called with the
correct timing and context to avoid system errors.
Parameter Passing
In C, parameters can be passed by value, where a copy of the data is
made, or by reference, using pointers, which allows the function to modify
the original data.
Embedded C Application
Functions that interact with hardware components often return status
codes, data readings, or boolean values indicating success or failure.
Embedded C Usage
Such a function could be used in an embedded system to determine the
highest sensor value, control signal, or other measurement critical to the
system’s operation.
Embedded C Consideration
Stack size is limited in embedded systems. Recursive functions or deep
function calls can lead to stack overflow.
What is Recursion?
Recursion occurs when a function calls itself to solve a problem by
breaking it down into smaller, more manageable sub-problems.
Embedded C Note
Recursive functions should be used with caution in embedded systems due
to limited stack space.
int factorial(int n) {
if (n <= 1) return 1;
return n * factorial(n - 1);
}
int main() {
int num = 5;
printf("Factorial of %d is %d", num, factorial(num));
return 0;
}
Pointer Declaration
type *pointerName;
Pointer Usage
int var = 10;
int *ptr = &var;
Embedded C Example
char *bufferPtr; // Pointer to a character buffer
Pointer Operations
Pointer arithmetic allows pointers to be incremented or decremented,
effectively moving through an array or block of memory.
Swapping Function
void swap(int *x, int *y) {
int temp = *x;
*x = *y;
*y = temp;
}
int main() {
int a = 10, b = 20;
swap(&a, &b);
printf("a: %d, b: %d", a, b); // Outputs a: 20, b: 10
}