Module 3
Module 3
Example:
type arrayName[arraySize];
#define NUM_SENSORS 4
int sensorReadings[NUM_SENSORS]; //Array for storing sensor v
In embedded C, the size of arrays is often determined by the number of physical components, like sensors or
actuators, connected to the microcontroller.
Array Initialization Syntax
Example:
int array[5] = {1, 2, 3, 4, 5};
int firstElement = array[0]; // Access fi rst element
Example:
int sum = 0;
for (int i = 0; i < 5; i++)
{
sum += array[i];
}
Example: Summing Elements in an Array
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;
}
ex2.c
Write a program to find average marks obtained by a class of 30 students in a test.
main( )
{
int avg, sum = 0 ;
int i ;
int marks[30] ; /* array declaration */
for ( i = 0 ; i <= 29 ; i++ )
{
printf ( "\nEnter marks " ) ;
scanf ( "%d", &marks[i] ) ; /* store data in array */
}
for ( i = 0 ; i <= 29 ; i++ )
sum = sum + marks[i] ; /* read data from an array*/
avg = sum / 30 ;
printf ( "\nAverage marks = %d", avg ) ;
}
ex1.c
Multi-Dimensional Arrays--- arrays
within arrays.,
Syntax and Declaration:
type arrayName[size1][size2];
Example:
int multi Arr[3][4]; // Declares a 3x4 array
multi Arr[0][1] = 5; // Element at row 0, column 1 to 5
Initializing Multi-Dimensional Arrays
Initialization Syntax:
type arrayName[size1][size2] = {{val1, val2}, {...}};
Example:
int matrix[2][3] = {{1, 2, 3}, {4, 5, 6}};
• Use row and column indices to access elements in a multi-dimensional array.
arrayName[row][column]
int value = matrix[1][2];
// Accesses the element at second row-third column
• For embedded systems, ensure the indices are within bounds to maintain
system stability.
Nested Loops and Multi-Dimensional Arrays
• Nested loops allow iteration over rows and columns of a multi-dimensional array.
for(int i = 0; i < rows; i++) { for(int j = 0; j < columns; j++) {
// Access array elements
}
}
• Example:
for(int i = 0; i < 2; i++)
{ for(int j = 0; j < 3; j++)
{
printf("%d ", matrix[i][j]);
}
printf("\n");
}
• In embedded systems, nested loops are commonly used for scanning or controlling a grid of sensors or
actuators.
Example
• Matrix addition ------ matrix_add_sub.c
#include <stdio.h> // add the matrices
int main() { for (i = 0; i < m; i++) {
int m, n, i, j; for (j = 0; j < n; j++) {
printf("Enter the number of rows and columns of the c[i][j] = a[i][j] + b[i][j];
matrices: "); }
scanf("%d%d", &m, &n); }
int a[m][n], b[m][n], c[m][n]; // print the result
printf("Enter the elements of matrix A: \n"); printf("The sum of the two matrices is: \n");
for (i = 0; i < m; i++) { for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) { for (j = 0; j < n; j++) {
scanf("%d", &a[i][j]); printf("%d ", c[i][j]);
} }
} printf("\n");
printf("Enter the elements of matrix B: \n"); }
for (i = 0; i < m; i++) { return 0;
for (j = 0; j < n; j++) { }
scanf("%d", &b[i][j]);
}
}
Arrays in Memory: How C Stores Arrays
Example: Searching an Array
Implementing a Search
Algorithm
A linear search algorithm iterates over an array to find a value.
This is a straightforward example of how to traverse an array with
a loop.
Embedded C Scenario
Searching through a data array to find a sensor reading that exceeds
a threshold could trigger an event or alert.
Strings in C: A Special Kind of Array
19/103
Reading and Writing Strings
Embedded C Considerations
In embedded systems, functions like ‘sprintf‘ and ‘sscanf‘ are used
for formatting strings to interact with hardware or protocol
messages.
20/103
Programs on Strings ----- gets()
#include<stdio.h>
void main ()
{
char s[30];
printf("Enter the string? ");
gets(s);
printf("You entered %s",s);
}
#include<stdio.h>
void main()
{
char str[20];
printf("Enter the string? ");
fgets(str, 20, stdin);
printf("%s", str);
}
Programs on Strings ----- puts()
#include<stdio.h>
#include <string.h>
int main(){
char name[50];
printf("Enter your name: ");
gets(name); //reads string from user
printf("Your name is: ");
puts(name); //displays string
return 0;
}
String Functions
No. Function Description
1) strlen(string_name) returns the length of string name.
4) strcmp(first_string, second_string) compares the first string with second string. If both
strings are same, it returns 0.
#include<stdio.h> str3.c
#include <string.h>
int main(){
char ch[10]={'w', 'e', 'l', 'c', 'o', 'm', 'e', '\0'};
printf("Length of string is: %d",strlen(ch));
return 0;
}
2. Copy String: strcpy()
#include<stdio.h>
#include <string.h> str4.c
int main(){
char ch[20]={'j', 'a', 'v', 'a', 't', 'p', 'o', 'i', 'n', 't', '\0'};
char ch2[20];
strcpy(ch2,ch);
printf("Value of second string is: %s",ch2);
return 0;
}
3. String Concatenation: strcat() str5.c
#include<stdio.h>
#include <string.h>
int main(){
char ch[10]={'h', 'e', 'l', 'l', 'o', '\0'};
char ch2[10]={'c', '\0'};
strcat(ch,ch2);
printf("Value of first string is: %s",ch);
return 0;
}
4. Compare String: strcmp()
str9.c
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "abcd", str2[] = "abCd", str3[] = "abcd";
int result;
// comparing strings str1 and str2
result = strcmp(str1, str2);
printf("strcmp(str1, str2) = %d\n", result);
// comparing strings str1 and str3
result = strcmp(str1, str3);
printf("strcmp(str1, str3) = %d\n", result);
return 0;
}
5. Reverse String: strrev() str7.c
#include<stdio.h>
#include <string.h>
int main(){
char str[20];
printf("Enter string: ");
gets(str);//reads string from console
printf("String is: %s",str);
printf("\nReverse String is: %s",strrev(str));
return 0;
}
String Palindrome str8.c
int main() {
printf("%p", &age);
printf("\n%p", ptr);
return 0;
}
C Pointers
• Pointers (pointer variables) are special variables that are used to store addresses rather
than values.
Pointer Syntax
• int* p;
• int *p1;
• int * p2;
• int* p1, p2; //Here, we have declared a pointer p1 and a normal variable p2.
• Assigning addresses to Pointers
• int* pc, c;
• c = 5;
• pc = &c;
• Here, 5 is assigned to the c variable. And, the address of c is assigned to the pc pointer.
Get Value of Thing Pointed by Pointers
• To get the value of the thing pointed by the pointers, we use the * operator.
• For example:
• int* pc, c;
• c = 5;
• pc = &c;
• printf("%d", *pc); // Output: 5
• Here, the address of c is assigned to the pc pointer. To get the value stored
in that address, we used *pc.
Changing Value Pointed by Pointers
Let's take an example.
int* pc, c;
c = 5;
pc = &c;
c = 1;
printf("%d", c); // Output: 1
printf("%d", *pc); // Ouptut: 1
• We have assigned the address of c to the pc pointer.
• Then, we changed the value of c to 1.
• Since pc and the address of c is the same, *pc gives us 1.
int* pc, c;
c = 5;
pc = &c;
*pc = 1;
printf("%d", *pc); // Ouptut: 1
printf("%d", c); // Output: 1
Initially, the address of c is assigned to the pc pointer using pc = &c;. Since c is 5, *pc gives us 5.
Then, the address of d is assigned to the pc pointer using pc = &d;. Since d is -15, *pc gives us -15.
#include <stdio.h> *pc = 2;
int main() printf("Address of c: %p\n", &c);
{ printf("Value of c: %d\n\n", c);
int* pc, c; return 0;
}
c = 22;
Address of c: 2686784
printf("Address of c: %p\n", &c);
Value of c: 22
printf("Value of c: %d\n\n", c);
1. int c, *pc;
// pc is address but c is not
2. pc = c; // Error
// &c is address but *pc is not
3. *pc = &c; // Error
// both &c and pc are addresses
4. pc = &c; // Not an error
// both c and *pc are values
5. *pc = c; // Not an error
Arrays & Pointers:
An array is a block of sequential data.
#include <stdio.h> From the above example, it is clear that &x[0] is equivalent
int main() { to x. And, x[0] is equivalent to *x.
int x[4]; Similarly,
int i;
&x[1] is equivalent to x+1 and x[1] is equivalent to *(x+1).
for(i = 0; i < 4; ++i) { &x[2] is equivalent to x+2 and x[2] is equivalent to *(x+2).
printf("&x[%d] = %p\n", i, &x[i]);
...
}
Basically, &x[i] is equivalent to x+i and x[i] is equivalent
printf("Address of array x: %p", x); to *(x+i).
return 0;
}
Example 1: Pointers and Arrays Here, we have declared an array x of
#include <stdio.h> 6 elements. To access elements of the
int main() { array, we have used pointers.
int i, x[6], sum = 0;
printf("Enter 6 numbers: ");
printf("%d", *p);
// 11
return 0;
}