C Pointers Part 6
C Pointers Part 6
Press
here
#LEARN_IN DEPTH
#Be_professional_in
1 embedded_system
Embedded System
PART 6 (C Programming)
POINTERS
[Link] SHENOUDA
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
2 embedded_system
Pointers
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
3
What Are Pointers?
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
Remember this?
scanf(“%d”, &i);
[Link]
Follow us
Pointers
Press
here
#LEARN_IN DEPTH
#Be_professional_in
5 embedded_system
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;
[Link]
Follow us
Pointers
Press
here
#LEARN_IN DEPTH
#Be_professional_in
6 embedded_system
Pointer Variables
[Link]
Follow us
Pointers
Press
here
#LEARN_IN DEPTH
#Be_professional_in
7
Pointer Variables
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
8
Pointers Pointer Variables
embedded_system
[Link]
Follow us
Pointers
Press
here
#LEARN_IN DEPTH
#Be_professional_in
9
Pointer Variables
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
[Link]
Follow us
Pointers
Press
here
#LEARN_IN DEPTH
#Be_professional_in
11 embedded_system
Pointer Variables
[Link]
Follow us
Pointer Arithmetic
Press
here
#LEARN_IN DEPTH
#Be_professional_in
12 embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
[Link]
Follow us
Press
#Be_professional_in
14 embedded_system
[Link]
Follow us
Press
here
Pointer to Array
#LEARN_IN DEPTH
#Be_professional_in
15 embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
16 embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
17
LAB Average of Weights
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
18 embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
19 embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
20
Pointers and Arrays
embedded_system
str[4]
or
*(p1+4)
[Link]
Follow us
Pointer to
Press
here
#LEARN_IN DEPTH
#Be_professional_in
21 embedded_system
Structure
->
[Link]
Follow us
Pointer to
Press
here
#LEARN_IN DEPTH
#Be_professional_in
22 embedded_system
Structure
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
23
Pointers AND Functions
embedded_system
[Link]
Follow us
Fast
Press
here
#LEARN_IN DEPTH
#Be_professional_in
24 embedded_system
Data
Transfer
Using
Pointers
[Link]
Follow us
Fast
Press
here
#LEARN_IN DEPTH
#Be_professional_in
25 embedded_system
Data
Transfer
Using
Pointers
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
26 embedded_system
Passing
Arrays and
Pointers to
Functions
Normal Array
Passing
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
27 embedded_system
Passing
Arrays and
Pointers to
Functions
Array
Passing with
Pointers
[Link]
Follow us
Press
#Be_professional_in
28 embedded_system
[Link]
Follow us
#Be_professional_in
29 embedded_system
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.
[Link]
Follow us
#Be_professional_in
30 embedded_system
types of parameters.
[Link]
Follow us
#Be_professional_in
31 embedded_system
Press
here
#LEARN_IN DEPTH
#Be_professional_in
32 embedded_system
[Link]
Follow us
#Be_professional_in
33 embedded_system
Pointers
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
34
The function prototype is:
embedded_system
[Link]
Follow us
Pointer Conversions
Press
here
#LEARN_IN DEPTH
#Be_professional_in
35 embedded_system
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)
[Link]
Follow us
Multiple Indirection
Press
here
#LEARN_IN DEPTH
#Be_professional_in
36 embedded_system
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
[Link]
Follow us
#Be_professional_in
37 embedded_system
[Link]
Follow us
#Be_professional_in
38 embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
39
NULL and Unassigned Pointers
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
40
NULL and Unassigned Pointers
embedded_system
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:
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
41 embedded_system
Labs
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
An Illustration
embedded_system
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 5
**pptr = 9; j int integer variable 10
*pptr = &i;
*ptr = -2;
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
An Illustration
embedded_system
int i = 5, j = 10;
int *ptr; /* declare a pointer-to-integer variable */
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 5
**pptr = 9; j int integer variable 10
*pptr = &i; ptr int * integer pointer variable
*ptr = -2;
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
An Illustration
embedded_system
int i = 5, j = 10;
int *ptr;
int **pptr; /* declare a pointer-to-pointer-to-integer variable */
ptr = &i;
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 5
**pptr = 9; j int integer variable 10
*pptr = &i; ptr int * integer pointer variable
*ptr = -2;
pptr int ** integer pointer pointer variable
[Link]
Double
Indirection
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
An Illustration
embedded_system
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i; /* store address-of i to ptr */
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 5
**pptr = 9; j int integer variable 10
*pptr = &i; ptr int * integer pointer variable address of i
*ptr = -2;
pptr int ** integer pointer pointer variable
*ptr int de-reference of ptr [Link]
5
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
An Illustration
embedded_system
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr; /* store address-of ptr to pptr */
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 5
**pptr = 9; j int integer variable 10
*pptr = &i; ptr int * integer pointer variable address of i
*ptr = -2;
pptr int ** integer pointer pointer variable address of ptr
*pptr int * de-reference of pptr[Link]
value of ptr
(address of i)
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
An Illustration
embedded_system
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 3
**pptr = 9; j int integer variable 10
*pptr = &i; ptr int * integer pointer variable address of i
*ptr = -2;
pptr int ** integer pointer pointer variable address of ptr
*ptr int de-reference of ptr [Link]
3
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
An Illustration
embedded_system
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 7
**pptr = 9; j int integer variable 10
*pptr = &i; ptr int * integer pointer variable address of i
*ptr = -2;
pptr int ** integer pointer pointer variable address of ptr
**pptr int de-reference of de-reference of 7
[Link]
pptr
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
An Illustration
embedded_system
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 7
**pptr = 9; j int integer variable 10
*pptr = &i; ptr int * integer pointer variable address of j
*ptr = -2;
pptr int ** integer pointer pointer variable address of ptr
*ptr int de-reference of ptr [Link]
10
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
An Illustration
embedded_system
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 7
**pptr = 9; j int integer variable 9
*pptr = &i; ptr int * integer pointer variable address of j
*ptr = -2;
pptr int ** integer pointer pointer variable address of ptr
**pptr int de-reference of de-reference of 9
[Link]
pptr
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
An Illustration
embedded_system
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable 7
**pptr = 9; j int integer variable 9
*pptr = &i; ptr int * integer pointer variable address of i
*ptr = -2;
pptr int ** integer pointer pointer variable address of ptr
*pptr int * de-reference of pptr[Link]
value of ptr
(address of i)
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
An Illustration
embedded_system
int i = 5, j = 10;
int *ptr;
int **pptr;
ptr = &i;
pptr = &ptr;
*ptr = 3; Data Table
**pptr = 7; Name Type Description Value
ptr = &j; i int integer variable -2
**pptr = 9; j int integer variable 9
*pptr = &i; ptr int * integer pointer variable address of i
*ptr = -2;
pptr int ** integer pointer pointer variable address of ptr
*ptr int de-reference of ptr [Link]
-2
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
Pointer Arithmetic
embedded_system
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!!!
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
float a[4];
Data Table
float *ptr;
Name Type Description Value
ptr = &(a[2]);
a[0] float float array element (variable) ?
*ptr = 3.14;
a[1] float float array element (variable) ?
ptr++;
a[2] float float array element (variable) ?
*ptr = 9.0;
ptr = ptr - 3;
a[3] float float array element (variable) ?
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
float a[4];
Data Table
float *ptr;
Name Type Description Value
ptr = &(a[2]);
a[0] float float array element (variable) ?
*ptr = 3.14;
a[1] float float array element (variable) ?
ptr++;
a[2] float float array element (variable) ?
*ptr = 9.0;
ptr = ptr - 3;
a[3] float float array element (variable) ?
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
float a[4];
Data Table
float *ptr;
Name Type Description Value
ptr = &(a[2]);
a[0] float float array element (variable) ?
*ptr = 3.14;
a[1] float float array element (variable) ?
ptr++;
a[2] float float array element (variable) 3.14
*ptr = 9.0;
ptr = ptr - 3;
a[3] float float array element (variable) ?
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
float a[4];
Data Table
float *ptr;
Name Type Description Value
ptr = &(a[2]);
a[0] float float array element (variable) ?
*ptr = 3.14;
a[1] float float array element (variable) ?
ptr++;
a[2] float float array element (variable) 3.14
*ptr = 9.0;
ptr = ptr - 3;
a[3] float float array element (variable) ?
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
float a[4];
Data Table
float *ptr;
Name Type Description Value
ptr = &(a[2]);
a[0] float float array element (variable) ?
*ptr = 3.14;
a[1] float float array element (variable) ?
ptr++;
a[2] float float array element (variable) 3.14
*ptr = 9.0;
ptr = ptr - 3;
a[3] float float array element (variable) 9.0
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
float a[4];
Data Table
float *ptr;
Name Type Description Value
ptr = &(a[2]);
a[0] float float array element (variable) ?
*ptr = 3.14;
a[1] float float array element (variable) ?
ptr++;
a[2] float float array element (variable) 3.14
*ptr = 9.0;
ptr = ptr - 3;
a[3] float float array element (variable) 9.0
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
float a[4];
Data Table
float *ptr;
Name Type Description Value
ptr = &(a[2]);
a[0] float float array element (variable) 6.0
*ptr = 3.14;
a[1] float float array element (variable) ?
ptr++;
a[2] float float array element (variable) 3.14
*ptr = 9.0;
ptr = ptr - 3;
a[3] float float array element (variable) 9.0
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
float a[4];
Data Table
float *ptr;
Name Type Description Value
ptr = &(a[2]);
a[0] float float array element (variable) 6.0
*ptr = 3.14;
a[1] float float array element (variable) ?
ptr++;
a[2] float float array element (variable) 3.14
*ptr = 9.0;
ptr = ptr - 3;
a[3] float float array element (variable) 9.0
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
float a[4];
Data Table
float *ptr;
Name Type Description Value
ptr = &(a[2]);
a[0] float float array element (variable) 6.0
*ptr = 3.14;
a[1] float float array element (variable) ?
ptr++;
a[2] float float array element (variable) 7.0
*ptr = 9.0;
ptr = ptr - 3;
a[3] float float array element (variable) 9.0
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
63
Pointer to Function
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
64 embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
65 embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
66 embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
67 embedded_system
Pointers Tricks
Modularity
How to Read C complex pointer
double to integer
Pointer with Constant
[Link]
Follow us
#Be_professional_in
68 embedded_system
expression
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
Operator Precedence Associative
(),[] 1 Left to Right
*,Identifier 2 Right to Left
Data Type 3 –
[Link]
Follow us
#Be_professional_in
69 embedded_system
expression
[Link]
Follow us
Press
#Be_professional_in
70 embedded_system
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 “()”
[Link]
Follow us
#Be_professional_in
71 embedded_system
expression
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”
[Link]
Follow us
#Be_professional_in
72 embedded_system
expression
Read it as –
[Link]
Follow us
Press
#Be_professional_in
73 embedded_system
examples
[Link]
Follow us
Press
here
#Be_professional_in
74 embedded_system
examples
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
75 embedded_system
examples
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
examples
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
examples
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
78 embedded_system
Pointers Tricks
Modularity
How to Read C complex pointer
Pointers on Embedded C
Pointer with Constant
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
79
Pointers on Embedded C
embedded_system
[Link]
Follow us
Press
What is here
#LEARN_IN DEPTH
#Be_professional_in
80 embedded_system
the Output ?
[Link]
Follow us
Press
What is here
#LEARN_IN DEPTH
#Be_professional_in
81 embedded_system
the Output ?
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
82 embedded_system
Pointers Tricks
Modularity
How to Read C complex pointer
Pointers on Embedded C
Pointer with Constant
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
83
Modularity
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
84
For example “UART” (send data)
embedded_system
Send_data(u16 data)
{
USART_Init ();
USART_Trans_Int_Enable();
USART_Trans_Enable();
USART_Transmit(u16 data);
}
USART_Transmit(u16 data);
{…} USART_Init (){…}
USART_Trans_Enable (){…} USART_Trans_Int_Enable (){…}
UART
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
85
For example “UART” (send data)
embedded_system
Send_data(u16 data)
{
USART_Init ();
USART_Trans_Int_Enable();
USART_Trans_Enable();
USART_Transmit(u16 data);
}
USART_Transmit(u16 data);
{…} USART_Init (){…}
USART_Trans_Enable (){…} USART_Trans_Int_Enable (){…}
Once the UART
sent the Data,
The TX interrupt
will insert and Byte Send data Byte UART
the CPU will call
ISR(USART_TXC_vect)
{ …….. } [Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
86
For example “UART” (send data)
embedded_system
Send_data(u16 data)
{
USART_Init ();
USART_Trans_Int_Enable();
USART_Trans_Enable();
USART_Transmit(u16 data);
}
USART_Transmit(u16 data);
{…} USART_Init (){…}
USART_Trans_Enable (){…} USART_Trans_Int_Enable (){…}
Once the UART
sent the Data,
The TX interrupt
will insert and Byte Send data Byte UART
the CPU will call
ISR(USART_TXC_vect)
{ …….. } [Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
87 embedded_system
ISR(USART_TXC_vect)
{ …….. } [Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
88 embedded_system
Pointers Tricks
Modularity
How to Read C complex pointer
Pointers on Embedded C
Pointer with Constant
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
89
Variable Quantifiers - const
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
90
Variable Quantifiers - const
embedded_system
[Link]
Follow us
#Be_professional_in
91 embedded_system
[Link]
Follow us
Press
#Be_professional_in
92 embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
93
Complete the table yes/no …
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
94 embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
95
Complete the table yes/no …
embedded_system
Try Now
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
96
Complete the table yes/no …
embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
97 embedded_system
[Link]
Follow us
Press
here
#LEARN_IN DEPTH
#Be_professional_in
98
References
embedded_system