c programming session8
TRANSCRIPT
![Page 1: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/1.jpg)
C_ProgrammingPart 8
ENG. KEROLES SHENOUDA
1
![Page 2: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/2.jpg)
Access Wrong Bit on the Registerone of the most famous issue for the embedded programmer
2
Functionality issue
![Page 3: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/3.jpg)
Bit-FieldsTHINK FIRST THEN ANSWER
HOW TO USE STRUCTURE ON ACCESS
SPECIFIC BIT ON THE REGISTERS ?
3
![Page 4: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/4.jpg)
Bit-Fields
Unlike some other computer languages, C has a built-in
feature, called a bit-field, that allows you toaccess 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
![Page 5: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/5.jpg)
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
![Page 6: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/6.jpg)
Bit-FieldsFor example, the status port of a serial communications adapter might
return a status byte organized like this:
Bit Meaning When Set0 Change in clear-to-send line1 Change in data-set-ready2 Trailing edge detected3 Change in receive line4 Clear-to-send5 Data-set-ready6 Telephone ringing7 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;
![Page 7: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/7.jpg)
Bit-Fields
You might use statements like the ones shown here to enable a program to determine when it cansend or receive data:
7
status = get_port_status();if(status.cts) printf("clear to send");if(status.dsr) printf("data ready");
Bit Meaning When Set0 Change in clear-to-send line1 Change in data-set-ready2 Trailing edge detected3 Change in receive line4 Clear-to-send5 Data-set-ready6 Telephone ringing7 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;
![Page 8: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/8.jpg)
What is the Output ? 8
![Page 9: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/9.jpg)
What is the Output ? 9
![Page 10: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/10.jpg)
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;
status7 6 5 4 3 2 1 0
delta_cts
delta_dsr
tr_edge
delta_rec
cts
dsr
rec_line
![Page 11: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/11.jpg)
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 thestatus_type structure like this:
Also, notice that the bits after dsr do not need to be specified if they are not used.
11status
7 6 5 4 3 2 1 0
cts
dsr
![Page 12: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/12.jpg)
Bit-Fields
It is valid to mix normal structure members with bit-fields. For example
12
What is the Output ?
![Page 13: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/13.jpg)
Bit-Fields
It is valid to mix normal structure members with bit-fields. For example
13
What is the Output ?
![Page 14: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/14.jpg)
Bit-FieldsBit-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
![Page 15: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/15.jpg)
Pointers
15
![Page 16: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/16.jpg)
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
![Page 17: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/17.jpg)
Why do we need Pointer?
Simply because it’s there!
It is used in some circumstances in C
Remember this?
scanf(“%d”, &i);
![Page 18: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/18.jpg)
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 istype *name;
18
![Page 19: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/19.jpg)
Pointers Pointer Variables
19
![Page 20: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/20.jpg)
Pointers Pointer Variables
20
![Page 21: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/21.jpg)
Pointers Pointer Variables 21
![Page 22: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/22.jpg)
Pointers Pointer Variables
22
![Page 23: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/23.jpg)
23What is the Output ?
![Page 24: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/24.jpg)
Pointers Pointer Variables
24
![Page 25: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/25.jpg)
Pointer ArithmeticThere are only two arithmetic operations
that you can use on pointers:
addition and subtraction.
25
![Page 26: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/26.jpg)
26rule govern pointer arithmetic.
![Page 27: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/27.jpg)
All pointer arithmetic is relative to its base type (assume 2-byte integers)
27
![Page 28: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/28.jpg)
Pointer to Array 28
![Page 29: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/29.jpg)
29
![Page 30: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/30.jpg)
LAB Average of Weights
It is required to calculate the summation weight of 5 boxes. The user should enter the boxes
30
![Page 31: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/31.jpg)
31
![Page 32: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/32.jpg)
32
![Page 33: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/33.jpg)
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
![Page 34: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/34.jpg)
Pointer to Structure
34
->
![Page 35: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/35.jpg)
Pointer to Structure
35
![Page 36: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/36.jpg)
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
![Page 37: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/37.jpg)
Fast Data Transfer Using Pointers
37
![Page 38: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/38.jpg)
Fast Data Transfer Using Pointers
38
![Page 39: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/39.jpg)
Passing Arrays and Pointers to FunctionsNormal Array Passing
39
![Page 40: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/40.jpg)
Passing Arrays and Pointers to FunctionsArray Passing with Pointers
40
![Page 41: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/41.jpg)
Method 2 is completely equivelent to Method 1 which means that:
41
![Page 42: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/42.jpg)
Finally we can summarize function parameters types 1. Input Parameters (Calling by Value)
The parameter values is completely transmitted to the function. This gives thefunction 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 theability 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
![Page 43: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/43.jpg)
The sort function contains the two types of parameters.
43
![Page 44: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/44.jpg)
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, itis required to type cast each data access operation.
44
![Page 45: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/45.jpg)
Example: Universal Compare with void Pointers45
![Page 46: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/46.jpg)
Example: Universal Compare with void Pointers46
![Page 47: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/47.jpg)
The function prototype is:
int Compare(void* value1, void* value2, int type)which means it takes any two pointers with uncknowntype, the third parameter informs thetype of the submitted values (1 means integer, 2 means double).
47
![Page 48: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/48.jpg)
Pointer Conversionsvoid * 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 themalloc( ) function described later in this chapter)
48
![Page 49: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/49.jpg)
Multiple IndirectionPointer 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
![Page 50: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/50.jpg)
Using Pointer to Pointer 50
![Page 51: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/51.jpg)
Using Pointer to Pointer 51
![Page 52: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/52.jpg)
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. Fllowingprogram will crash becausethe (pX) pointer is not pointed to a valid address, it contain a memory gurbage
52
![Page 53: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/53.jpg)
NULL and Unassigned Pointers
To avoid using unassigned pointers, all pointers must hold a valid address, if not it must holda zero value. Sometimes zero value called (NULL). Above program may be fixed as shownbellow:
53
![Page 54: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/54.jpg)
Labs
54
![Page 55: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/55.jpg)
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
![Page 56: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/56.jpg)
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
![Page 57: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/57.jpg)
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
![Page 58: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/58.jpg)
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
![Page 59: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/59.jpg)
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)
![Page 60: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/60.jpg)
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
![Page 61: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/61.jpg)
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
![Page 62: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/62.jpg)
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
![Page 63: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/63.jpg)
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
![Page 64: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/64.jpg)
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)
![Page 65: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/65.jpg)
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
![Page 66: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/66.jpg)
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!!!
![Page 67: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/67.jpg)
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
?
![Page 68: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/68.jpg)
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
?
![Page 69: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/69.jpg)
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
![Page 70: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/70.jpg)
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
?
![Page 71: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/71.jpg)
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
![Page 72: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/72.jpg)
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
?
![Page 73: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/73.jpg)
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
![Page 74: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/74.jpg)
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
![Page 75: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/75.jpg)
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
![Page 76: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/76.jpg)
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 functionand it is the address used when the function is called.
Once a pointer points to a function, thefunction can be called through that pointer.
Function pointers also allow functions to be passed as arguments to other functions
76
![Page 77: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/77.jpg)
77
![Page 78: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/78.jpg)
78
![Page 79: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/79.jpg)
79
![Page 80: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/80.jpg)
Pointers Tricks
80
How to Read C complex pointer
double to integer
Modularity
Pointer with Constant
![Page 81: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/81.jpg)
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
![Page 82: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/82.jpg)
How to Read C complex pointer expression
82
![Page 83: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/83.jpg)
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
![Page 84: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/84.jpg)
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”
![Page 85: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/85.jpg)
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
![Page 86: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/86.jpg)
How to Read C complex pointer expressionexamples
86
![Page 87: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/87.jpg)
How to Read C complex pointer expressionexamples
87
![Page 88: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/88.jpg)
How to Read C complex pointer expressionexamples
88
![Page 89: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/89.jpg)
How to Read C complex pointer expressionexamples
89
![Page 90: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/90.jpg)
How to Read C complex pointer expressionexamples
Test Your self
90
![Page 91: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/91.jpg)
Pointers Tricks
91
How to Read C complex pointer
Pointers on Embedded C
Modularity
Pointer with Constant
![Page 92: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/92.jpg)
Pointers on Embedded C 92
![Page 93: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/93.jpg)
93What isthe Output ?
![Page 94: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/94.jpg)
94What isthe Output ?
![Page 95: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/95.jpg)
Pointers Tricks
95
How to Read C complex pointer
Pointers on Embedded C
Modularity
Pointer with Constant
![Page 96: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/96.jpg)
Modularity 96
![Page 97: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/97.jpg)
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
![Page 98: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/98.jpg)
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){ …….. }
![Page 99: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/99.jpg)
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){ …….. }
![Page 100: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/100.jpg)
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
![Page 101: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/101.jpg)
Pointers Tricks
101
How to Read C complex pointer
Pointers on Embedded C
Modularity
Pointer with Constant
![Page 102: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/102.jpg)
Variable Quantifiers - const
const is used with a datatype declaration or definition to specify an unchanging value
const objects may not be changed
102
![Page 103: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/103.jpg)
Variable Quantifiers - const
const is used with a datatype declaration or definition to specify an unchanging value
const objects may not be changed
103
![Page 104: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/104.jpg)
Constant with pointer 104
![Page 105: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/105.jpg)
Constant with pointer 105
![Page 106: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/106.jpg)
Complete the table yes/no … 106
![Page 107: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/107.jpg)
107
int* ptr = &a;
int* const ptr = &a;int const *ptr = &a;
const int *ptr = &a;
![Page 108: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/108.jpg)
Complete the table yes/no … 108
Try Now
![Page 109: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/109.jpg)
Complete the table yes/no … 109
![Page 110: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/110.jpg)
110
![Page 111: C programming session8](https://reader031.vdocuments.us/reader031/viewer/2022030318/5a647c167f8b9a36568b4ac7/html5/thumbnails/111.jpg)
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