Lab1 Cce Adv C
Lab1 Cce Adv C
Mansoura university
Computer Control Engineering department
Embedded Systems
Lab (1)
Advanced C programming
2
what’s embedded system.
4
Course Objectives
• Understanding embedded system software Software
limitations.
• Adapt C language to be used in embedded
context.
• Embedded C techniques.
• Writing peripherals drivers.
• Learn basic embedded software architecture.
5
Course overview Embedded
Concept
Assignments
micro projects for each lab.
8
Lab ( 1 )
9
Lab - Outlines
• Pointers.
• Pointer arithmetic.
• Null pointer.
• Void pointer.
• Pass by reference:
• Function overhead.
• Status-return 10
C Pointers
❑ C is very powerful allowing its user to directly access memory address
using pointers.
❑ Pointers are a data type that store the memory locations.
❑ Pointer can indirectly access value unlike, variables name which access the
value directly.
C Pointers
int y = 5 ;
Int main(){
char * c;
char y;
printf(“%d \n%d”,sizeof(c),sizeof(y)); OUTPUT:
return 0; 4
} 1
Size of pointer depend on computer Really?
Arch.
C Pointers
Pointers make C more powerful allowing a wide variety
of tasks to be accomplished. Pointers enable
programmers to:
• Change values of actual arguments passed to function “call
by reference”.
• More concisely and efficiently deal with arrays.
• Represent strings in a different manner.
• Call functions from different levels.
• manipulate memory specially those is dynamically allocated
• Effectively represent sophisticated data structures such
as linked lists, queues, stacks and trees.
Pointers and Arrays
The array name is self is a constant pointer to the
first element in the array?
Int a[ ] = {1, 2, 3, 4};
Int * ptr;
Ptr = a;
Printf(“ %d “ , *ptr); OUTPUT:
Ptr = &a[0]; 1
Printf(“ %d “ , *ptr); 1
Ptr = &a[3]; 4
Printf(“ %d “ , *ptr);
Pointers and Arrays
The array name it self is a constant pointer to the
first element in the array?
Example :
Int v [ ] = {1,2,3,4,5}; //first element is at 0x3000
Int * vptr = v;
Pointers Arithmetic
Example :
Int v [ ] = {1,2,3,4,5}; //first element is at 0x3000
Int * vptr = v;
vptr += 2; // ???
Pointers Arithmetic
Example :
Int v [ ] = {1,2,3,4,5}; //first element is at 0x3000
Int * vptr = v;
vptr += 2; // ???
Note
When adding value to pointer we don’t add ( 1 , 2 or 3 …n)
but we actually add a memory location.
New_add = old_add + ( n * sizeof (pointing data type) )
Pointers Arithmetic
Example :
#include <stdio.h> arr[0] = 5, arr[1] = 2, arr[2] = 3
int main(){ arr[0] = 5, arr[1] = 10, arr[2] = 3
int arr[] = {1, 2, 3}; 0[arr] = 5, 1[arr] = 10, 2[arr] = 15
*arr= 5;
printf("arr[0] = %d, arr[1] = %d, arr[2] = %d\n",arr[0], arr[1], arr[2]);
*(arr+ 1) = 10;
printf("arr[0] = %d, arr[1] = %d, arr[2] = %d\n",arr[0], arr[1], arr[2]);
2[arr] = 15;
printf("0[arr] = %d, 1[arr] = %d, 2[arr] = %d\n",0[arr], 1[arr], 2[arr]);
return 0;
}
Pointers Arithmetic
Example :
#include <stdio.h> 1 2 3 000000000061FE0C 1
Int main(){ -1 2 3 000000000061FE10 2
Int arr[] = {1,2,3}; -1 2 -2 000000000061FE14 -2
int*ptr= arr; -1 2 -1 000000000061FE14 -1
printf("%d \t %d \t %d \t %p \t %d\n",arr[0],arr[1],arr[2],ptr,*ptr);
*ptr++=-1;
printf("%d \t %d \t %d \t %p \t %d\n",arr[0],arr[1],arr[2],ptr,*ptr);
*++ptr=-2;
printf("%d \t %d \t %d \t %p \t %d\n",arr[0],arr[1],arr[2],ptr,*ptr);
++(*ptr);
printf("%d \t %d \t %d \t %p \t %d\n",arr[0],arr[1],arr[2],ptr,*ptr);
return 0;}
Pointer to char
Access a chunk of memory
Int main()
{
int a = 0xFFAACCDD; 0xDD
unsigned char * ptr = (char *)
&a;
0xCC
0xAA
printf("0x%x \n",*(ptr));
printf("0x%x \n",*(ptr+1));
printf("0x%x \n",*(ptr+2));
}
Side note:
This example assume little-Indian (search for it :D).
Assignment (1)
This assignment aims to make you remember C basic syntax
37
Assignment (1)
Implement an X,O game with a grid 3 x 3, the X and O players take turn to play.
(X) will always start first
your program should do the following: 1 2 3
• display a matrix as POINTED to the user 4 5 6
• promote to user which turn ( X or Y), then the user will input the
index in which he wants to play in. 7 8 9
• The program should update the matrix and print it again to the next
player to select where to play
⚬ for example x will enter (5) then the program should display the
1 2 3
following 4 X 6
• The program should check if any player wins, if so should print when
player won and end the game. if it is a draw the program should
7 8 9
start another grid until someone win
Assignment (1)
• The winning condition for either X or O is like the following
• any horizontal row contains (x or O)
• any vertical column contains(x,o)
• the two diagonal of the matrix
X
X X
X X X
• you code must have
⚬ char check_board_for_winner()-> the function takes the 3x3 2D array and check if X
wins return ‘x’ or O wins return ‘O’ other returns (255) indicating draw. you need to
search how give 2D array as argument to a function.
Assignment (1)
How to upload your answer:
41