SlideShare uma empresa Scribd logo
1 de 111
Baixar para ler offline
C_Programming
Part 8
ENG. KEROLES SHENOUDA
1
Access Wrong Bit on the Register
one of the most famous issue for
the embedded programmer
2
Functionality
issue
Bit-Fields
THINK FIRST THEN ANSWER
HOW TO USE STRUCTURE ON ACCESS
SPECIFIC BIT ON THE REGISTERS ?
3
Bit-Fields
 Unlike some other computer languages, C has a built-in
feature, called a bit-field, that allows you to
access a single bit. Bit-fields can be useful for a number of
reasons, such as:
 If storage is limited, you can store several Boolean (true/false)
variables in one byte.
 Certain devices transmit status information encoded into one or more
bits within a byte
 Certain encryption routines need to access the bits within a byte.
4
Bit-Fields
 A bit-field must be a member of a structure or union. It defines how
long, in bits, the field is to be.
The general form of a bit-field definition is
type name: length;
 type is the type of the bit-field, and length is the number of bits in
the field. The type of a bitfield must be int, signed, or unsigned.
(C99 also allows a bit-field to be of type _Bool.)
 Bit-fields are frequently used when analyzing input from a hardware
device.
5
Bit-Fields
For example, the status port of a serial communications adapter might
return a status byte organized like this:
Bit Meaning When Set
0 Change in clear-to-send line
1 Change in data-set-ready
2 Trailing edge detected
3 Change in receive line
4 Clear-to-send
5 Data-set-ready
6 Telephone ringing
7 Received signal
6
struct status_type {
unsigned char delta_cts:1;
unsigned char delta_dsr:1;
unsigned char tr_edge:1;
unsigned char delta_rec:1;
unsigned char cts:1;
unsigned char dsr:1;
unsigned char ring:1;
unsigned char rec_line:1;
} status;
Bit-Fields
 You might use statements like the ones
shown here to enable a program to
determine when it can
send or receive data:
7
status = get_port_status();
if(status.cts) printf("clear to send");
if(status.dsr) printf("data ready");
Bit Meaning When Set
0 Change in clear-to-send line
1 Change in data-set-ready
2 Trailing edge detected
3 Change in receive line
4 Clear-to-send
5 Data-set-ready
6 Telephone ringing
7 Received signal
struct status_type {
unsigned char delta_cts:1;
unsigned char delta_dsr:1;
unsigned char tr_edge:1;
unsigned char delta_rec:1;
unsigned char cts:1;
unsigned char dsr:1;
unsigned char ring:1;
unsigned char rec_line:1;
} status;
What is the Output ? 8
What is the Output ? 9
Bit-Fields 10
struct status_type {
unsigned char delta_cts:1;
unsigned char delta_dsr:1;
unsigned char tr_edge:1;
unsigned char delta_rec:1;
unsigned char cts:1;
unsigned char dsr:1;
unsigned char ring:1;
unsigned char rec_line:1;
} status;
status
7 6 5 4 3 2 1 0
delta_cts
delta_dsr
tr_edge
delta_rec
cts
dsr
rec_line
Bit-Fields
 You do not have to name
each bit-field.
This makes it easy to reach the
bit you want, bypassing unused
ones.
 For example, if you only care
about the cts and dsr bits, you
could declare the
status_type structure like this:
 Also, notice that the bits after
dsr do not need to be specified if
they are not used.
11
status
7 6 5 4 3 2 1 0
cts
dsr
Bit-Fields
 It is valid to mix normal structure members with bit-fields. For
example
12
What is the Output ?
Bit-Fields
 It is valid to mix normal structure members with bit-fields. For
example
13
What is the Output ?
Bit-Fields
Bit-fields have certain restrictions
 you cannot take the address of a bit-field.
Bit-fields cannot be arrayed.
 You cannot know, from machine to machine, whether
the fields will run from right to left or from left to right;
have some machine dependencies
14
Pointers
15
What Are Pointers?
 A pointer is a variable that holds a memory
address.
 This address is the location of another object
(typically another variable) in memory.
For example, if one variable contains the address of another
variable, the first variable is said to point to
the second.
16
Why do we need Pointer?
 Simply because it’s there!
 It is used in some circumstances in C
Remember this?
scanf(“%d”, &i);
Pointers
Pointer Variables
 If a variable is going to be a
pointer, it must be declared
as such. A pointer
declaration consists of a
base type, an *, and the
variable name. The general
form for declaring a pointer
variable is
type *name;
18
Pointers
Pointer Variables
19
Pointers
Pointer Variables
20
Pointers Pointer Variables 21
Pointers
Pointer Variables
22
23
What is the Output ?
Pointers
Pointer Variables
24
Pointer Arithmetic
 There are only two arithmetic operations
that you can use on pointers:
addition and subtraction.
25
26
rule govern pointer arithmetic.
All pointer arithmetic is relative to its base type
(assume 2-byte integers)
27
Pointer to Array 28
29
LAB Average of Weights
 It is required to calculate the summation weight of 5
boxes. The user should enter the boxes
30
31
32
Pointers and Arrays
 There is a close relationship between pointers and arrays.
 Consider this program fragment:
char str[80], *p1;
p1 = str;
Here, p1 has been set to the address of the first array element in str.
 To access the fifth element in str, you could write
str[4]
or
*(p1+4)
33
Pointer to
Structure
34
->
Pointer to
Structure
35
Pointers AND Functions
 Pointers are used efficiently with functions. Using pointers
provides two main features:
Fast data transfer, because only pointers are transferred.
Pointers allow the definition of several outputs for the same
function.
36
Fast Data
Transfer
Using
Pointers
37
Fast Data
Transfer
Using
Pointers
38
Passing
Arrays and
Pointers to
Functions
Normal
Array
Passing
39
Passing
Arrays and
Pointers to
Functions
Array
Passing with
Pointers
40
Method 2 is completely equivelent to
Method 1 which means that:
41
Finally we can summarize function
parameters types
 1. Input Parameters (Calling by Value)
The parameter values is completely transmitted to the function. This
gives the
function the ability to read the transmitted data only.
2. Input/Output Parameters (Refrence or Pointer)
The parameter pointer (refernce) is transmitted only. This gives the
function the
ability to read from and write to the original parameters.
3. Output Parameters (Return Value)
The return data of the function is assumed as an output parameter. Normally C
does
not provide other Output parameters except the return value.
42
The sort function contains the two types
of parameters.
43
Pointer with Unknown Type (void*)
 Programmer can define general
pointer without specifying a linked
data type.
 This type of pointers called (void
pointer).
 void pointer can not be used
normally to manipulated data, it
is required to type cast each
data access operation.
44
Example: Universal Compare with void Pointers
45
Example: Universal Compare with void Pointers
46
The function prototype is:
 int Compare(void* value1, void* value2, int type)
which means it takes any two pointers with uncknown
type, the third parameter informs the
type of the submitted values (1 means integer, 2
means double).
47
Pointer Conversions
void * pointers
 In C, it is permissible to assign a void * pointer to any other type of pointer. It
is also permissible to assign any other type of pointer to a void * pointer. A
void * pointer is called a generic pointer.
 The void * pointer is used to specify a pointer whose base type is unknown.
 The void * type allows a function to specify a parameter that is capable of
receiving any type of pointer argument without reporting a type mismatch.
 It is also used to refer to raw memory (such as that returned by the
malloc( ) function described later in this chapter)
48
Multiple Indirection
Pointer to Pointer
 You can have a pointer point to another pointer
that points to the target value.
 This situation is called
multiple indirection, or pointers to pointers
49
Using Pointer to Pointer 50
Using Pointer to Pointer 51
NULL and Unassigned Pointers
 If the pointer is unassigned it will contain an invalid address, it is unsafe
to use an unassigned pointer, normally the program will crash. Fllowing
program will crash because
the (pX) pointer is not pointed to a valid address, it contain a memory
gurbage
52
NULL and Unassigned Pointers
 To avoid using unassigned pointers, all pointers must hold a valid address,
if not it must hold
a zero value. Sometimes zero value called (NULL). Above program may be
fixed as shown
bellow:
53
Labs
54
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data Table
Name Type Description Value
i int integer variable 5
j int integer variable 10
An Illustration
int i = 5, j = 10;
int *ptr; /* declare a pointer-to-integer variable */
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data Table
Name Type Description Value
i int integer variable 5
j int integer variable 10
ptr int * integer pointer variable
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr; /* declare a pointer-to-pointer-to-integer variable */
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data Table
Name Type Description Value
i int integer variable 5
j int integer variable 10
ptr int * integer pointer variable
pptr int ** integer pointer pointer variable
Double
Indirection
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i; /* store address-of i to ptr */
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data Table
Name Type Description Value
i int integer variable 5
j int integer variable 10
ptr int * integer pointer variable address of i
pptr int ** integer pointer pointer variable
*ptr int de-reference of ptr 5
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr; /* store address-of ptr to pptr */
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data Table
Name Type Description Value
i int integer variable 5
j int integer variable 10
ptr int * integer pointer variable address of i
pptr int ** integer pointer pointer variable address of ptr
*pptr int * de-reference of pptr value of ptr
(address of i)
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data Table
Name Type Description Value
i int integer variable 3
j int integer variable 10
ptr int * integer pointer variable address of i
pptr int ** integer pointer pointer variable address of ptr
*ptr int de-reference of ptr 3
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data Table
Name Type Description Value
i int integer variable 7
j int integer variable 10
ptr int * integer pointer variable address of i
pptr int ** integer pointer pointer variable address of ptr
**pptr int de-reference of de-reference of
pptr
7
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data Table
Name Type Description Value
i int integer variable 7
j int integer variable 10
ptr int * integer pointer variable address of j
pptr int ** integer pointer pointer variable address of ptr
*ptr int de-reference of ptr 10
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data Table
Name Type Description Value
i int integer variable 7
j int integer variable 9
ptr int * integer pointer variable address of j
pptr int ** integer pointer pointer variable address of ptr
**pptr int de-reference of de-reference of
pptr
9
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data Table
Name Type Description Value
i int integer variable 7
j int integer variable 9
ptr int * integer pointer variable address of i
pptr int ** integer pointer pointer variable address of ptr
*pptr int * de-reference of pptr value of ptr
(address of i)
An Illustration
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3;
**pptr = 7;
ptr = &j;
**pptr = 9;
*pptr = &i;
*ptr = -2;
Data Table
Name Type Description Value
i int integer variable -2
j int integer variable 9
ptr int * integer pointer variable address of i
pptr int ** integer pointer pointer variable address of ptr
*ptr int de-reference of ptr -2
Pointer Arithmetic
 What’s ptr + 1?
 The next memory location!
 What’s ptr - 1?
 The previous memory location!
 What’s ptr * 2 and ptr / 2?
 Invalid operations!!!
Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data Table
Name Type Description Value
a[0] float float array element (variable) ?
a[1] float float array element (variable) ?
a[2] float float array element (variable) ?
a[3] float float array element (variable) ?
ptr float * float pointer variable
*ptr float de-reference of float pointer
variable
?
Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data Table
Name Type Description Value
a[0] float float array element (variable) ?
a[1] float float array element (variable) ?
a[2] float float array element (variable) ?
a[3] float float array element (variable) ?
ptr float * float pointer variable address of a[2]
*ptr float de-reference of float pointer
variable
?
Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data Table
Name Type Description Value
a[0] float float array element (variable) ?
a[1] float float array element (variable) ?
a[2] float float array element (variable) 3.14
a[3] float float array element (variable) ?
ptr float * float pointer variable address of a[2]
*ptr float de-reference of float pointer
variable
3.14
Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data Table
Name Type Description Value
a[0] float float array element (variable) ?
a[1] float float array element (variable) ?
a[2] float float array element (variable) 3.14
a[3] float float array element (variable) ?
ptr float * float pointer variable address of a[3]
*ptr float de-reference of float pointer
variable
?
Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data Table
Name Type Description Value
a[0] float float array element (variable) ?
a[1] float float array element (variable) ?
a[2] float float array element (variable) 3.14
a[3] float float array element (variable) 9.0
ptr float * float pointer variable address of a[3]
*ptr float de-reference of float pointer
variable
9.0
Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data Table
Name Type Description Value
a[0] float float array element (variable) ?
a[1] float float array element (variable) ?
a[2] float float array element (variable) 3.14
a[3] float float array element (variable) 9.0
ptr float * float pointer variable address of a[0]
*ptr float de-reference of float pointer
variable
?
Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data Table
Name Type Description Value
a[0] float float array element (variable) 6.0
a[1] float float array element (variable) ?
a[2] float float array element (variable) 3.14
a[3] float float array element (variable) 9.0
ptr float * float pointer variable address of a[0]
*ptr float de-reference of float pointer
variable
6.0
Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data Table
Name Type Description Value
a[0] float float array element (variable) 6.0
a[1] float float array element (variable) ?
a[2] float float array element (variable) 3.14
a[3] float float array element (variable) 9.0
ptr float * float pointer variable address of a[2]
*ptr float de-reference of float pointer
variable
3.14
Pointer Arithmetic and Array
float a[4];
float *ptr;
ptr = &(a[2]);
*ptr = 3.14;
ptr++;
*ptr = 9.0;
ptr = ptr - 3;
*ptr = 6.0;
ptr += 2;
*ptr = 7.0;
Data Table
Name Type Description Value
a[0] float float array element (variable) 6.0
a[1] float float array element (variable) ?
a[2] float float array element (variable) 7.0
a[3] float float array element (variable) 9.0
ptr float * float pointer variable address of a[2]
*ptr float de-reference of float pointer
variable
7.0
Pointer to Function
 powerful feature of C is the function pointer.
 A function has a physical location in memory that can be assigned to a pointer.
 This address is the entry point of the function
and it is the address used when the function is called.
 Once a pointer points to a function, the
function can be called through that pointer.
 Function pointers also allow functions to be passed as arguments to other
functions
76
77
78
79
Pointers Tricks
80
How to Read C complex pointer
double to integer
Modularity
Pointer with Constant
How to Read C complex pointer
expression
Operator Precedence Associative
(),[] 1 Left to Right
*,Identifier 2 Right to Left
Data Type 3 –
81
Before We Learn How to Read Complex Array we should
first know precedence and associative .
•Priority : Operator precedence describes the order in
which C reads expressions
•order : Order operators of equal precedence in an
expression are applied
Before Reading Article You Should know Precedence
and Associative Table
How to Read C complex pointer
expression
82
How to Read C complex pointer
expression
 char (* ptr)[5]
Step 1 :
•Observe Above Precedence Table
•[] and () have Same Priority
•Using Associativity , Highest Priority Element is decided
•Associativity is from Left to Right First Priority is Given to “()”
83
How to Read C complex pointer
expression
84
Step 2 :
•Between Pair of Brackets again we have to decide which one has highest priority ? ‘*’ or ‘ptr’ ?
•* and Identifier ptr have Same Priority
•Associativity is from Right to Left First Priority is Given to “ptr”
How to Read C complex pointer
expression
85
Read it as –
ptr is pointer to a one dimensional array having
size five which can store data of type char
How to Read C complex pointer expression
examples
86
How to Read C complex pointer expression
examples
87
How to Read C complex pointer expression
examples
88
How to Read C complex pointer expression
examples
89
How to Read C complex pointer expression
examples
 Test Your self
90
Pointers Tricks
91
How to Read C complex pointer
Pointers on Embedded C
Modularity
Pointer with Constant
Pointers on Embedded C 92
93What is
the Output ?
94What is
the Output ?
Pointers Tricks
95
How to Read C complex pointer
Pointers on Embedded C
Modularity
Pointer with Constant
Modularity 96
For example “UART” (send data)
 Send_data(u16 data)
 {
 USART_Init ();
 USART_Trans_Int_Enable();
 USART_Trans_Enable();
 USART_Transmit(u16 data);
 }
97
USART_Init (){…}
USART_Trans_Int_Enable (){…}USART_Trans_Enable (){…}
USART_Transmit(u16 data);
{…}
UART
For example “UART” (send data)
 Send_data(u16 data)
 {
 USART_Init ();
 USART_Trans_Int_Enable();
 USART_Trans_Enable();
 USART_Transmit(u16 data);
 }
98
USART_Init (){…}
USART_Trans_Int_Enable (){…}USART_Trans_Enable (){…}
USART_Transmit(u16 data);
{…}
UARTSend dataByte Byte
Once the UART
sent the Data,
The TX interrupt
will insert and
the CPU will call
ISR(USART_TXC_vect)
{ …….. }
For example “UART” (send data)
 Send_data(u16 data)
 {
 USART_Init ();
 USART_Trans_Int_Enable();
 USART_Trans_Enable();
 USART_Transmit(u16 data);
 }
99
USART_Init (){…}
USART_Trans_Int_Enable (){…}USART_Trans_Enable (){…}
USART_Transmit(u16 data);
{…}
UARTSend dataByte Byte
Once the UART
sent the Data,
The TX interrupt
will insert and
the CPU will call
ISR(USART_TXC_vect)
{ …….. }
100
void (*Ptr_To_Trans_Int) (void);
extern void USART_callback_Trans_Int(void (*Ptr_to_Func)(void))
{
Ptr_To_Trans_Int = Ptr_to_Func;
}
USART_Init (){…}
ISR(USART_TXC_vect)
{ …….. }
ISR(USART_TXC_vect)
{
(*Ptr_To_Trans_Int)();
}
UARTSend dataByte Byte
Pointers Tricks
101
How to Read C complex pointer
Pointers on Embedded C
Modularity
Pointer with Constant
Variable Quantifiers - const
 const is used with a datatype declaration or definition to specify an
unchanging value
 const objects may not be changed
102
Variable Quantifiers - const
 const is used with a datatype declaration or definition to specify an
unchanging value
 const objects may not be changed
103
Constant with pointer 104
Constant with pointer 105
Complete the table yes/no … 106
107
int* ptr = &a;
int* const ptr = &a;
int const *ptr = &a;
const int *ptr = &a;
Complete the table yes/no … 108
Try Now
Complete the table yes/no … 109
110
References 111
 C The Complete Reference 4th Ed Herbert Schildt
 A Tutorial on Data Representation
 std::printf, std::fprintf, std::sprintf, std::snprintf…..
 C Programming for Engineers, Dr. Mohamed Sobh
 C programming expert.
 fresh2refresh.com/c-programming
 C programming Interview questions and answers
 C – Preprocessor directives
 Constant Pointers and Pointers to Constant A Subtle Difference in C
Programming

Mais conteúdo relacionado

Mais procurados

Automotive embedded systems part3 v1
Automotive embedded systems part3 v1Automotive embedded systems part3 v1
Automotive embedded systems part3 v1Keroles karam khalil
 
Automotive embedded systems part4 v1
Automotive embedded systems part4 v1Automotive embedded systems part4 v1
Automotive embedded systems part4 v1Keroles karam khalil
 
Automotive embedded systems part2 v1
Automotive embedded systems part2 v1Automotive embedded systems part2 v1
Automotive embedded systems part2 v1Keroles karam khalil
 
Automotive embedded systems part8 v1
Automotive embedded systems part8 v1Automotive embedded systems part8 v1
Automotive embedded systems part8 v1Keroles karam khalil
 
Automotive embedded systems part1 v1
Automotive embedded systems part1 v1Automotive embedded systems part1 v1
Automotive embedded systems part1 v1Keroles karam khalil
 
Automotive embedded systems part6 v2
Automotive embedded systems part6 v2Automotive embedded systems part6 v2
Automotive embedded systems part6 v2Keroles karam khalil
 
En.dm00024550 stm8 s003f3p6
En.dm00024550   stm8 s003f3p6En.dm00024550   stm8 s003f3p6
En.dm00024550 stm8 s003f3p6betodias29
 
PCIe BUS: A State-of-the-Art-Review
PCIe BUS: A State-of-the-Art-ReviewPCIe BUS: A State-of-the-Art-Review
PCIe BUS: A State-of-the-Art-ReviewIOSRJVSP
 
01 GPIO||General Purpose Input Output.2016
01 GPIO||General Purpose Input Output.201601 GPIO||General Purpose Input Output.2016
01 GPIO||General Purpose Input Output.2016Mohamed Fawzy
 
Automotive embedded systems part6 v1
Automotive embedded systems part6 v1Automotive embedded systems part6 v1
Automotive embedded systems part6 v1Keroles karam khalil
 
Embedded System Programming on ARM Cortex M3 and M4 Course
Embedded System Programming on ARM Cortex M3 and M4 CourseEmbedded System Programming on ARM Cortex M3 and M4 Course
Embedded System Programming on ARM Cortex M3 and M4 CourseFastBit Embedded Brain Academy
 

Mais procurados (20)

Automotive embedded systems part3 v1
Automotive embedded systems part3 v1Automotive embedded systems part3 v1
Automotive embedded systems part3 v1
 
Microcontroller part 1
Microcontroller part 1Microcontroller part 1
Microcontroller part 1
 
Embedded C - Lecture 1
Embedded C - Lecture 1Embedded C - Lecture 1
Embedded C - Lecture 1
 
Automotive embedded systems part4 v1
Automotive embedded systems part4 v1Automotive embedded systems part4 v1
Automotive embedded systems part4 v1
 
Automotive embedded systems part2 v1
Automotive embedded systems part2 v1Automotive embedded systems part2 v1
Automotive embedded systems part2 v1
 
Embedded C - Day 1
Embedded C - Day 1Embedded C - Day 1
Embedded C - Day 1
 
Embedded C - Day 2
Embedded C - Day 2Embedded C - Day 2
Embedded C - Day 2
 
Misra c
Misra cMisra c
Misra c
 
Automotive embedded systems part8 v1
Automotive embedded systems part8 v1Automotive embedded systems part8 v1
Automotive embedded systems part8 v1
 
Automotive embedded systems part1 v1
Automotive embedded systems part1 v1Automotive embedded systems part1 v1
Automotive embedded systems part1 v1
 
C Programming - Refresher - Part III
C Programming - Refresher - Part IIIC Programming - Refresher - Part III
C Programming - Refresher - Part III
 
Automotive embedded systems part6 v2
Automotive embedded systems part6 v2Automotive embedded systems part6 v2
Automotive embedded systems part6 v2
 
En.dm00024550 stm8 s003f3p6
En.dm00024550   stm8 s003f3p6En.dm00024550   stm8 s003f3p6
En.dm00024550 stm8 s003f3p6
 
Misra c rules
Misra c rulesMisra c rules
Misra c rules
 
PCIe BUS: A State-of-the-Art-Review
PCIe BUS: A State-of-the-Art-ReviewPCIe BUS: A State-of-the-Art-Review
PCIe BUS: A State-of-the-Art-Review
 
01 GPIO||General Purpose Input Output.2016
01 GPIO||General Purpose Input Output.201601 GPIO||General Purpose Input Output.2016
01 GPIO||General Purpose Input Output.2016
 
Automotive embedded systems part6 v1
Automotive embedded systems part6 v1Automotive embedded systems part6 v1
Automotive embedded systems part6 v1
 
Embedded C - Lecture 2
Embedded C - Lecture 2Embedded C - Lecture 2
Embedded C - Lecture 2
 
Automative basics v3
Automative basics v3Automative basics v3
Automative basics v3
 
Embedded System Programming on ARM Cortex M3 and M4 Course
Embedded System Programming on ARM Cortex M3 and M4 CourseEmbedded System Programming on ARM Cortex M3 and M4 Course
Embedded System Programming on ARM Cortex M3 and M4 Course
 

Destaque (13)

C programming session9 -
C programming  session9 -C programming  session9 -
C programming session9 -
 
C programming session10
C programming  session10C programming  session10
C programming session10
 
Homework 2
Homework 2Homework 2
Homework 2
 
C programming part2
C programming part2C programming part2
C programming part2
 
Microcontroller part 3
Microcontroller part 3Microcontroller part 3
Microcontroller part 3
 
Microcontroller part 9_v1
Microcontroller part 9_v1Microcontroller part 9_v1
Microcontroller part 9_v1
 
Microcontroller part 1
Microcontroller part 1Microcontroller part 1
Microcontroller part 1
 
Microcontroller part 7_v1
Microcontroller part 7_v1Microcontroller part 7_v1
Microcontroller part 7_v1
 
Microcontroller part 6_v1
Microcontroller part 6_v1Microcontroller part 6_v1
Microcontroller part 6_v1
 
Microcontroller part 4
Microcontroller part 4Microcontroller part 4
Microcontroller part 4
 
Microcontroller part 8_v1
Microcontroller part 8_v1Microcontroller part 8_v1
Microcontroller part 8_v1
 
Microcontroller part 2
Microcontroller part 2Microcontroller part 2
Microcontroller part 2
 
C programming session3
C programming  session3C programming  session3
C programming session3
 

Semelhante a C programming session8

Types of pointer in C
Types of pointer in CTypes of pointer in C
Types of pointer in Crgnikate
 
Advanced C programming
Advanced C programmingAdvanced C programming
Advanced C programmingClaus Wu
 
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.pptbtech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.pptchintuyadav19
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programmingnmahi96
 
0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdf0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdfssusere19c741
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingRai University
 
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handlingMca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handlingRai University
 
Btech 1 pic u-5 pointer, structure ,union and intro to file handling
Btech 1 pic u-5 pointer, structure ,union and intro to file handlingBtech 1 pic u-5 pointer, structure ,union and intro to file handling
Btech 1 pic u-5 pointer, structure ,union and intro to file handlingRai University
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingRai University
 
Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++Gamindu Udayanga
 
Pointers in C PPT | Poinetrs | Pointers Concept
Pointers in C PPT | Poinetrs | Pointers ConceptPointers in C PPT | Poinetrs | Pointers Concept
Pointers in C PPT | Poinetrs | Pointers Conceptsankalpkumarsahoo174
 
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handlingRai University
 
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handlingBsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handlingRai University
 
Chapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfChapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfTamiratDejene1
 

Semelhante a C programming session8 (20)

Pointers in C
Pointers in CPointers in C
Pointers in C
 
Advanced pointers
Advanced pointersAdvanced pointers
Advanced pointers
 
Pointer.pptx
Pointer.pptxPointer.pptx
Pointer.pptx
 
Types of pointer
Types of pointerTypes of pointer
Types of pointer
 
Types of pointer in C
Types of pointer in CTypes of pointer in C
Types of pointer in C
 
Advanced C programming
Advanced C programmingAdvanced C programming
Advanced C programming
 
COM1407: Working with Pointers
COM1407: Working with PointersCOM1407: Working with Pointers
COM1407: Working with Pointers
 
Pointers in C
Pointers in CPointers in C
Pointers in C
 
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.pptbtech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
btech-1picu-5pointerstructureunionandintrotofilehandling-150122010700-conver.ppt
 
Pointers-Computer programming
Pointers-Computer programmingPointers-Computer programming
Pointers-Computer programming
 
0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdf0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdf
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
 
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handlingMca 1 pic u-5 pointer, structure ,union and intro to file handling
Mca 1 pic u-5 pointer, structure ,union and intro to file handling
 
Btech 1 pic u-5 pointer, structure ,union and intro to file handling
Btech 1 pic u-5 pointer, structure ,union and intro to file handlingBtech 1 pic u-5 pointer, structure ,union and intro to file handling
Btech 1 pic u-5 pointer, structure ,union and intro to file handling
 
pointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handlingpointer, structure ,union and intro to file handling
pointer, structure ,union and intro to file handling
 
Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++Pointers Refrences & dynamic memory allocation in C++
Pointers Refrences & dynamic memory allocation in C++
 
Pointers in C PPT | Poinetrs | Pointers Concept
Pointers in C PPT | Poinetrs | Pointers ConceptPointers in C PPT | Poinetrs | Pointers Concept
Pointers in C PPT | Poinetrs | Pointers Concept
 
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handlingDiploma ii  cfpc- u-5.1 pointer, structure ,union and intro to file handling
Diploma ii cfpc- u-5.1 pointer, structure ,union and intro to file handling
 
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handlingBsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
Bsc cs 1 pic u-5 pointer, structure ,union and intro to file handling
 
Chapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdfChapter 5 (Part I) - Pointers.pdf
Chapter 5 (Part I) - Pointers.pdf
 

Mais de Keroles karam khalil (18)

C basics quiz part 1_solution
C basics quiz part 1_solutionC basics quiz part 1_solution
C basics quiz part 1_solution
 
Autosar Basics hand book_v1
Autosar Basics  hand book_v1Autosar Basics  hand book_v1
Autosar Basics hand book_v1
 
Automotive embedded systems part5 v2
Automotive embedded systems part5 v2Automotive embedded systems part5 v2
Automotive embedded systems part5 v2
 
Automotive embedded systems part7 v1
Automotive embedded systems part7 v1Automotive embedded systems part7 v1
Automotive embedded systems part7 v1
 
Automotive embedded systems part5 v1
Automotive embedded systems part5 v1Automotive embedded systems part5 v1
Automotive embedded systems part5 v1
 
Quiz 9
Quiz 9Quiz 9
Quiz 9
 
Quiz 10
Quiz 10Quiz 10
Quiz 10
 
Homework 6
Homework 6Homework 6
Homework 6
 
Homework 5 solution
Homework 5 solutionHomework 5 solution
Homework 5 solution
 
Notes part7
Notes part7Notes part7
Notes part7
 
Homework 5
Homework 5Homework 5
Homework 5
 
Notes part6
Notes part6Notes part6
Notes part6
 
Homework 4 solution
Homework 4 solutionHomework 4 solution
Homework 4 solution
 
Notes part5
Notes part5Notes part5
Notes part5
 
Homework 4
Homework 4Homework 4
Homework 4
 
Homework 3 solution
Homework 3 solutionHomework 3 solution
Homework 3 solution
 
C programming session5
C programming  session5C programming  session5
C programming session5
 
Session 5-exersice
Session 5-exersiceSession 5-exersice
Session 5-exersice
 

Último

On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeThiyagu K
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIShubhangi Sonawane
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 

Último (20)

On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 

C programming session8

  • 2. Access Wrong Bit on the Register one of the most famous issue for the embedded programmer 2 Functionality issue
  • 3. Bit-Fields THINK FIRST THEN ANSWER HOW TO USE STRUCTURE ON ACCESS SPECIFIC BIT ON THE REGISTERS ? 3
  • 4. Bit-Fields  Unlike some other computer languages, C has a built-in feature, called a bit-field, that allows you to access a single bit. Bit-fields can be useful for a number of reasons, such as:  If storage is limited, you can store several Boolean (true/false) variables in one byte.  Certain devices transmit status information encoded into one or more bits within a byte  Certain encryption routines need to access the bits within a byte. 4
  • 5. Bit-Fields  A bit-field must be a member of a structure or union. It defines how long, in bits, the field is to be. The general form of a bit-field definition is type name: length;  type is the type of the bit-field, and length is the number of bits in the field. The type of a bitfield must be int, signed, or unsigned. (C99 also allows a bit-field to be of type _Bool.)  Bit-fields are frequently used when analyzing input from a hardware device. 5
  • 6. Bit-Fields For example, the status port of a serial communications adapter might return a status byte organized like this: Bit Meaning When Set 0 Change in clear-to-send line 1 Change in data-set-ready 2 Trailing edge detected 3 Change in receive line 4 Clear-to-send 5 Data-set-ready 6 Telephone ringing 7 Received signal 6 struct status_type { unsigned char delta_cts:1; unsigned char delta_dsr:1; unsigned char tr_edge:1; unsigned char delta_rec:1; unsigned char cts:1; unsigned char dsr:1; unsigned char ring:1; unsigned char rec_line:1; } status;
  • 7. Bit-Fields  You might use statements like the ones shown here to enable a program to determine when it can send or receive data: 7 status = get_port_status(); if(status.cts) printf("clear to send"); if(status.dsr) printf("data ready"); Bit Meaning When Set 0 Change in clear-to-send line 1 Change in data-set-ready 2 Trailing edge detected 3 Change in receive line 4 Clear-to-send 5 Data-set-ready 6 Telephone ringing 7 Received signal struct status_type { unsigned char delta_cts:1; unsigned char delta_dsr:1; unsigned char tr_edge:1; unsigned char delta_rec:1; unsigned char cts:1; unsigned char dsr:1; unsigned char ring:1; unsigned char rec_line:1; } status;
  • 8. What is the Output ? 8
  • 9. What is the Output ? 9
  • 10. Bit-Fields 10 struct status_type { unsigned char delta_cts:1; unsigned char delta_dsr:1; unsigned char tr_edge:1; unsigned char delta_rec:1; unsigned char cts:1; unsigned char dsr:1; unsigned char ring:1; unsigned char rec_line:1; } status; status 7 6 5 4 3 2 1 0 delta_cts delta_dsr tr_edge delta_rec cts dsr rec_line
  • 11. Bit-Fields  You do not have to name each bit-field. This makes it easy to reach the bit you want, bypassing unused ones.  For example, if you only care about the cts and dsr bits, you could declare the status_type structure like this:  Also, notice that the bits after dsr do not need to be specified if they are not used. 11 status 7 6 5 4 3 2 1 0 cts dsr
  • 12. Bit-Fields  It is valid to mix normal structure members with bit-fields. For example 12 What is the Output ?
  • 13. Bit-Fields  It is valid to mix normal structure members with bit-fields. For example 13 What is the Output ?
  • 14. Bit-Fields Bit-fields have certain restrictions  you cannot take the address of a bit-field. Bit-fields cannot be arrayed.  You cannot know, from machine to machine, whether the fields will run from right to left or from left to right; have some machine dependencies 14
  • 16. What Are Pointers?  A pointer is a variable that holds a memory address.  This address is the location of another object (typically another variable) in memory. For example, if one variable contains the address of another variable, the first variable is said to point to the second. 16
  • 17. Why do we need Pointer?  Simply because it’s there!  It is used in some circumstances in C Remember this? scanf(“%d”, &i);
  • 18. Pointers Pointer Variables  If a variable is going to be a pointer, it must be declared as such. A pointer declaration consists of a base type, an *, and the variable name. The general form for declaring a pointer variable is type *name; 18
  • 23. 23 What is the Output ?
  • 25. Pointer Arithmetic  There are only two arithmetic operations that you can use on pointers: addition and subtraction. 25
  • 26. 26 rule govern pointer arithmetic.
  • 27. All pointer arithmetic is relative to its base type (assume 2-byte integers) 27
  • 29. 29
  • 30. LAB Average of Weights  It is required to calculate the summation weight of 5 boxes. The user should enter the boxes 30
  • 31. 31
  • 32. 32
  • 33. Pointers and Arrays  There is a close relationship between pointers and arrays.  Consider this program fragment: char str[80], *p1; p1 = str; Here, p1 has been set to the address of the first array element in str.  To access the fifth element in str, you could write str[4] or *(p1+4) 33
  • 36. Pointers AND Functions  Pointers are used efficiently with functions. Using pointers provides two main features: Fast data transfer, because only pointers are transferred. Pointers allow the definition of several outputs for the same function. 36
  • 41. Method 2 is completely equivelent to Method 1 which means that: 41
  • 42. Finally we can summarize function parameters types  1. Input Parameters (Calling by Value) The parameter values is completely transmitted to the function. This gives the function the ability to read the transmitted data only. 2. Input/Output Parameters (Refrence or Pointer) The parameter pointer (refernce) is transmitted only. This gives the function the ability to read from and write to the original parameters. 3. Output Parameters (Return Value) The return data of the function is assumed as an output parameter. Normally C does not provide other Output parameters except the return value. 42
  • 43. The sort function contains the two types of parameters. 43
  • 44. Pointer with Unknown Type (void*)  Programmer can define general pointer without specifying a linked data type.  This type of pointers called (void pointer).  void pointer can not be used normally to manipulated data, it is required to type cast each data access operation. 44
  • 45. Example: Universal Compare with void Pointers 45
  • 46. Example: Universal Compare with void Pointers 46
  • 47. The function prototype is:  int Compare(void* value1, void* value2, int type) which means it takes any two pointers with uncknown type, the third parameter informs the type of the submitted values (1 means integer, 2 means double). 47
  • 48. Pointer Conversions void * pointers  In C, it is permissible to assign a void * pointer to any other type of pointer. It is also permissible to assign any other type of pointer to a void * pointer. A void * pointer is called a generic pointer.  The void * pointer is used to specify a pointer whose base type is unknown.  The void * type allows a function to specify a parameter that is capable of receiving any type of pointer argument without reporting a type mismatch.  It is also used to refer to raw memory (such as that returned by the malloc( ) function described later in this chapter) 48
  • 49. Multiple Indirection Pointer to Pointer  You can have a pointer point to another pointer that points to the target value.  This situation is called multiple indirection, or pointers to pointers 49
  • 50. Using Pointer to Pointer 50
  • 51. Using Pointer to Pointer 51
  • 52. NULL and Unassigned Pointers  If the pointer is unassigned it will contain an invalid address, it is unsafe to use an unassigned pointer, normally the program will crash. Fllowing program will crash because the (pX) pointer is not pointed to a valid address, it contain a memory gurbage 52
  • 53. NULL and Unassigned Pointers  To avoid using unassigned pointers, all pointers must hold a valid address, if not it must hold a zero value. Sometimes zero value called (NULL). Above program may be fixed as shown bellow: 53
  • 55. An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 5 j int integer variable 10
  • 56. An Illustration int i = 5, j = 10; int *ptr; /* declare a pointer-to-integer variable */ int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 5 j int integer variable 10 ptr int * integer pointer variable
  • 57. An Illustration int i = 5, j = 10; int *ptr; int **pptr; /* declare a pointer-to-pointer-to-integer variable */ ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 5 j int integer variable 10 ptr int * integer pointer variable pptr int ** integer pointer pointer variable Double Indirection
  • 58. An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; /* store address-of i to ptr */ pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 5 j int integer variable 10 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable *ptr int de-reference of ptr 5
  • 59. An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; /* store address-of ptr to pptr */ *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 5 j int integer variable 10 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable address of ptr *pptr int * de-reference of pptr value of ptr (address of i)
  • 60. An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 3 j int integer variable 10 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable address of ptr *ptr int de-reference of ptr 3
  • 61. An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 7 j int integer variable 10 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable address of ptr **pptr int de-reference of de-reference of pptr 7
  • 62. An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 7 j int integer variable 10 ptr int * integer pointer variable address of j pptr int ** integer pointer pointer variable address of ptr *ptr int de-reference of ptr 10
  • 63. An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 7 j int integer variable 9 ptr int * integer pointer variable address of j pptr int ** integer pointer pointer variable address of ptr **pptr int de-reference of de-reference of pptr 9
  • 64. An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable 7 j int integer variable 9 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable address of ptr *pptr int * de-reference of pptr value of ptr (address of i)
  • 65. An Illustration int i = 5, j = 10; int *ptr; int **pptr; ptr = &i; pptr = &ptr; *ptr = 3; **pptr = 7; ptr = &j; **pptr = 9; *pptr = &i; *ptr = -2; Data Table Name Type Description Value i int integer variable -2 j int integer variable 9 ptr int * integer pointer variable address of i pptr int ** integer pointer pointer variable address of ptr *ptr int de-reference of ptr -2
  • 66. Pointer Arithmetic  What’s ptr + 1?  The next memory location!  What’s ptr - 1?  The previous memory location!  What’s ptr * 2 and ptr / 2?  Invalid operations!!!
  • 67. Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) ? a[1] float float array element (variable) ? a[2] float float array element (variable) ? a[3] float float array element (variable) ? ptr float * float pointer variable *ptr float de-reference of float pointer variable ?
  • 68. Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) ? a[1] float float array element (variable) ? a[2] float float array element (variable) ? a[3] float float array element (variable) ? ptr float * float pointer variable address of a[2] *ptr float de-reference of float pointer variable ?
  • 69. Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) ? a[1] float float array element (variable) ? a[2] float float array element (variable) 3.14 a[3] float float array element (variable) ? ptr float * float pointer variable address of a[2] *ptr float de-reference of float pointer variable 3.14
  • 70. Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) ? a[1] float float array element (variable) ? a[2] float float array element (variable) 3.14 a[3] float float array element (variable) ? ptr float * float pointer variable address of a[3] *ptr float de-reference of float pointer variable ?
  • 71. Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) ? a[1] float float array element (variable) ? a[2] float float array element (variable) 3.14 a[3] float float array element (variable) 9.0 ptr float * float pointer variable address of a[3] *ptr float de-reference of float pointer variable 9.0
  • 72. Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) ? a[1] float float array element (variable) ? a[2] float float array element (variable) 3.14 a[3] float float array element (variable) 9.0 ptr float * float pointer variable address of a[0] *ptr float de-reference of float pointer variable ?
  • 73. Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) 6.0 a[1] float float array element (variable) ? a[2] float float array element (variable) 3.14 a[3] float float array element (variable) 9.0 ptr float * float pointer variable address of a[0] *ptr float de-reference of float pointer variable 6.0
  • 74. Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) 6.0 a[1] float float array element (variable) ? a[2] float float array element (variable) 3.14 a[3] float float array element (variable) 9.0 ptr float * float pointer variable address of a[2] *ptr float de-reference of float pointer variable 3.14
  • 75. Pointer Arithmetic and Array float a[4]; float *ptr; ptr = &(a[2]); *ptr = 3.14; ptr++; *ptr = 9.0; ptr = ptr - 3; *ptr = 6.0; ptr += 2; *ptr = 7.0; Data Table Name Type Description Value a[0] float float array element (variable) 6.0 a[1] float float array element (variable) ? a[2] float float array element (variable) 7.0 a[3] float float array element (variable) 9.0 ptr float * float pointer variable address of a[2] *ptr float de-reference of float pointer variable 7.0
  • 76. Pointer to Function  powerful feature of C is the function pointer.  A function has a physical location in memory that can be assigned to a pointer.  This address is the entry point of the function and it is the address used when the function is called.  Once a pointer points to a function, the function can be called through that pointer.  Function pointers also allow functions to be passed as arguments to other functions 76
  • 77. 77
  • 78. 78
  • 79. 79
  • 80. Pointers Tricks 80 How to Read C complex pointer double to integer Modularity Pointer with Constant
  • 81. How to Read C complex pointer expression Operator Precedence Associative (),[] 1 Left to Right *,Identifier 2 Right to Left Data Type 3 – 81 Before We Learn How to Read Complex Array we should first know precedence and associative . •Priority : Operator precedence describes the order in which C reads expressions •order : Order operators of equal precedence in an expression are applied Before Reading Article You Should know Precedence and Associative Table
  • 82. How to Read C complex pointer expression 82
  • 83. How to Read C complex pointer expression  char (* ptr)[5] Step 1 : •Observe Above Precedence Table •[] and () have Same Priority •Using Associativity , Highest Priority Element is decided •Associativity is from Left to Right First Priority is Given to “()” 83
  • 84. How to Read C complex pointer expression 84 Step 2 : •Between Pair of Brackets again we have to decide which one has highest priority ? ‘*’ or ‘ptr’ ? •* and Identifier ptr have Same Priority •Associativity is from Right to Left First Priority is Given to “ptr”
  • 85. How to Read C complex pointer expression 85 Read it as – ptr is pointer to a one dimensional array having size five which can store data of type char
  • 86. How to Read C complex pointer expression examples 86
  • 87. How to Read C complex pointer expression examples 87
  • 88. How to Read C complex pointer expression examples 88
  • 89. How to Read C complex pointer expression examples 89
  • 90. How to Read C complex pointer expression examples  Test Your self 90
  • 91. Pointers Tricks 91 How to Read C complex pointer Pointers on Embedded C Modularity Pointer with Constant
  • 95. Pointers Tricks 95 How to Read C complex pointer Pointers on Embedded C Modularity Pointer with Constant
  • 97. For example “UART” (send data)  Send_data(u16 data)  {  USART_Init ();  USART_Trans_Int_Enable();  USART_Trans_Enable();  USART_Transmit(u16 data);  } 97 USART_Init (){…} USART_Trans_Int_Enable (){…}USART_Trans_Enable (){…} USART_Transmit(u16 data); {…} UART
  • 98. For example “UART” (send data)  Send_data(u16 data)  {  USART_Init ();  USART_Trans_Int_Enable();  USART_Trans_Enable();  USART_Transmit(u16 data);  } 98 USART_Init (){…} USART_Trans_Int_Enable (){…}USART_Trans_Enable (){…} USART_Transmit(u16 data); {…} UARTSend dataByte Byte Once the UART sent the Data, The TX interrupt will insert and the CPU will call ISR(USART_TXC_vect) { …….. }
  • 99. For example “UART” (send data)  Send_data(u16 data)  {  USART_Init ();  USART_Trans_Int_Enable();  USART_Trans_Enable();  USART_Transmit(u16 data);  } 99 USART_Init (){…} USART_Trans_Int_Enable (){…}USART_Trans_Enable (){…} USART_Transmit(u16 data); {…} UARTSend dataByte Byte Once the UART sent the Data, The TX interrupt will insert and the CPU will call ISR(USART_TXC_vect) { …….. }
  • 100. 100 void (*Ptr_To_Trans_Int) (void); extern void USART_callback_Trans_Int(void (*Ptr_to_Func)(void)) { Ptr_To_Trans_Int = Ptr_to_Func; } USART_Init (){…} ISR(USART_TXC_vect) { …….. } ISR(USART_TXC_vect) { (*Ptr_To_Trans_Int)(); } UARTSend dataByte Byte
  • 101. Pointers Tricks 101 How to Read C complex pointer Pointers on Embedded C Modularity Pointer with Constant
  • 102. Variable Quantifiers - const  const is used with a datatype declaration or definition to specify an unchanging value  const objects may not be changed 102
  • 103. Variable Quantifiers - const  const is used with a datatype declaration or definition to specify an unchanging value  const objects may not be changed 103
  • 106. Complete the table yes/no … 106
  • 107. 107 int* ptr = &a; int* const ptr = &a; int const *ptr = &a; const int *ptr = &a;
  • 108. Complete the table yes/no … 108 Try Now
  • 109. Complete the table yes/no … 109
  • 110. 110
  • 111. References 111  C The Complete Reference 4th Ed Herbert Schildt  A Tutorial on Data Representation  std::printf, std::fprintf, std::sprintf, std::snprintf…..  C Programming for Engineers, Dr. Mohamed Sobh  C programming expert.  fresh2refresh.com/c-programming  C programming Interview questions and answers  C – Preprocessor directives  Constant Pointers and Pointers to Constant A Subtle Difference in C Programming