0% found this document useful (0 votes)
57 views98 pages

C Pointers Part 6

This document discusses pointers in C programming. It defines a pointer as a variable that holds the memory address of another variable. Pointers allow a program to indirectly access the memory location of another variable. The document explains that pointers must be declared with a variable type followed by an asterisk. It also discusses pointer arithmetic, noting that only addition and subtraction can be used on pointers, and this arithmetic is relative to the base type. Finally, it briefly mentions pointers can be used to access array elements.

Uploaded by

AnuragNayan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
57 views98 pages

C Pointers Part 6

This document discusses pointers in C programming. It defines a pointer as a variable that holds the memory address of another variable. Pointers allow a program to indirectly access the memory location of another variable. The document explains that pointers must be declared with a variable type followed by an asterisk. It also discusses pointer arithmetic, noting that only addition and subtraction can be used on pointers, and this arithmetic is relative to the base type. Finally, it briefly mentions pointers can be used to access array elements.

Uploaded by

AnuragNayan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Follow us

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

 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.

[Link]
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

Why do we need Pointer?


embedded_system

 Simply because it’s there!


 It is used in some circumstances in C

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

What is the Output ? 10 embedded_system

[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

 There are only two arithmetic operations


that you can use on pointers:
addition and subtraction.

[Link]
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

rule govern pointer arithmetic. 13 embedded_system

[Link]
Follow us

Press

All pointer arithmetic is relative to its base


here
#LEARN_IN DEPTH

#Be_professional_in

14 embedded_system

type (assume 2-byte integers)

[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

 It is required to calculate the summation weight of 5


boxes. The user should enter the boxes

[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

 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)
[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

 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.

[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

Method 2 is completely equivalent


here
#LEARN_IN DEPTH

#Be_professional_in

28 embedded_system

to Method 1 which means that:

[Link]
Follow us

Finally we can summarize function


Press
here
#LEARN_IN DEPTH

#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

The sort function contains the two


Press
here
#LEARN_IN DEPTH

#Be_professional_in

30 embedded_system

types of parameters.

[Link]
Follow us

Pointer with Unknown Type (void*)


Press
here
#LEARN_IN DEPTH

#Be_professional_in

31 embedded_system

 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.
[Link]
Example: Universal Compare with void Pointers
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

32 embedded_system

[Link]
Follow us

Example: Universal Compare with void


Press
here
#LEARN_IN DEPTH

#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

 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).

[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

Using Pointer to Pointer Press


here
#LEARN_IN DEPTH

#Be_professional_in

37 embedded_system

[Link]
Follow us

Using Pointer to Pointer


Press
here
#LEARN_IN DEPTH

#Be_professional_in

38 embedded_system

[Link]
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

39
NULL and Unassigned Pointers
embedded_system

 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

[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

Pointer Arithmetic and Array


embedded_system

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) ?

*ptr = 6.0; ptr float * float pointer variable


ptr += 2; *ptr float de-reference of float pointer ?
variable
*ptr = 7.0;

[Link]
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

Pointer Arithmetic and Array


embedded_system

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) ?

*ptr = 6.0; ptr float * float pointer variable address of a[2]


ptr += 2; *ptr float de-reference of float pointer ?
variable
*ptr = 7.0;

[Link]
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

Pointer Arithmetic and Array


embedded_system

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) ?

*ptr = 6.0; ptr float * float pointer variable address of a[2]


ptr += 2; *ptr float de-reference of float pointer 3.14
variable
*ptr = 7.0;

[Link]
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

Pointer Arithmetic and Array


embedded_system

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) ?

*ptr = 6.0; ptr float * float pointer variable address of a[3]


ptr += 2; *ptr float de-reference of float pointer ?
variable
*ptr = 7.0;

[Link]
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

Pointer Arithmetic and Array


embedded_system

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

*ptr = 6.0; ptr float * float pointer variable address of a[3]


ptr += 2; *ptr float de-reference of float pointer 9.0
variable
*ptr = 7.0;

[Link]
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

Pointer Arithmetic and Array


embedded_system

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

*ptr = 6.0; ptr float * float pointer variable address of a[0]


ptr += 2; *ptr float de-reference of float pointer ?
variable
*ptr = 7.0;

[Link]
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

Pointer Arithmetic and Array


embedded_system

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

*ptr = 6.0; ptr float * float pointer variable address of a[0]


ptr += 2; *ptr float de-reference of float pointer 6.0
variable
*ptr = 7.0;

[Link]
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

Pointer Arithmetic and Array


embedded_system

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

*ptr = 6.0; ptr float * float pointer variable address of a[2]


ptr += 2; *ptr float de-reference of float pointer 3.14
variable
*ptr = 7.0;

[Link]
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

Pointer Arithmetic and Array


embedded_system

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

*ptr = 6.0; ptr float * float pointer variable address of a[2]


ptr += 2; *ptr float de-reference of float pointer 7.0
variable
*ptr = 7.0;

[Link]
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

63
Pointer to Function
embedded_system

 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

[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

How to Read C complex pointer


Press
here
#LEARN_IN DEPTH

#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

How to Read C complex pointer


Press
here
#LEARN_IN DEPTH

#Be_professional_in

69 embedded_system

expression

[Link]
Follow us

Press

How to Read C complex pointer


here
#LEARN_IN DEPTH

#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

How to Read C complex pointer


Press
here
#LEARN_IN DEPTH

#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

How to Read C complex pointer


Press
here
#LEARN_IN DEPTH

#Be_professional_in

72 embedded_system

expression

Read it as –

ptr is pointer to a one dimensional array having


size five which can store data of type char

[Link]
Follow us

Press

How to Read C complex pointer expression


here
#LEARN_IN DEPTH

#Be_professional_in

73 embedded_system

examples

[Link]
Follow us

Press
here

How to Read C complex pointer expression


#LEARN_IN DEPTH

#Be_professional_in

74 embedded_system

examples

[Link]
Follow us

Press
here
#LEARN_IN DEPTH

How to Read C complex pointer expression


#Be_professional_in

75 embedded_system

examples

[Link]
Follow us

Press
here
#LEARN_IN DEPTH

How to Read C complex pointer expression 76


#Be_professional_in
embedded_system

examples

[Link]
Follow us

Press
here
#LEARN_IN DEPTH

How to Read C complex pointer expression 77


#Be_professional_in
embedded_system

examples

 Test Your self

[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

void (*Ptr_To_Trans_Int) (void);


extern void USART_callback_Trans_Int(void (*Ptr_to_Func)(void))
{
Ptr_To_Trans_Int = Ptr_to_Func;
ISR(USART_TXC_vect)
}
{ USART_Init (){…}
(*Ptr_To_Trans_Int)();
}

Byte Send data Byte UART

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

 const is used with a datatype declaration or definition to specify an


unchanging value
 const objects may not be changed

[Link]
Follow us

Press
here
#LEARN_IN DEPTH

#Be_professional_in

90
Variable Quantifiers - const
embedded_system

 const is used with a datatype declaration or definition to specify an


unchanging value
 const objects may not be changed

[Link]
Follow us

Constant with pointer


Press
here
#LEARN_IN DEPTH

#Be_professional_in

91 embedded_system

[Link]
Follow us

Press

Constant with pointer


here
#LEARN_IN DEPTH

#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

int* ptr = &a;

int const *ptr = &a;


const int *ptr = &a; int* const ptr = &a;

[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

 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.
 [Link]/c-programming
 C programming Interview questions and answers
 C – Preprocessor directives
 Constant Pointers and Pointers to Constant A Subtle Difference in C
Programming
[Link]

You might also like