About Me
 Name : Ahmed Badr Sayed
 Department: Mechatronics Engineer, Ain Shams University
 Embedded System, Embedded linux Engineer
 E-mail: ahmedbadrsaid1998@gmail.com
 Linkedin: www.linkedin.com/in/ahmed-badr-sayed-63267a286
Pointers
Presented by Ahmed Badr Sayed
GDSC_Al-Azahar’2024
01
General syntax, Declaring and initializing of pointer and why do we need
pointers
02 Pass by value vs. Pass by reference (address)
03 Operations on pointers
04 Passing array to function
05 Multiple Indirection: Pointer to Pointer.
06 Pointer to Structure and pointer to function.
07 Types of pointers.
AGENDA
Pointer: is a variable which capable of storing the initial
address of the object(another variable) which it wants to
point to.
General Syntax
Data_type *pointer_name
Or
Data_type* pointer_name
Here the data type refers to the type of the value that
the pointer will point to.
Declaring and initializing of pointer
Why do we need pointers?
1. Dynamic memory allocation
2. Parameter passing
3. Manipulating hardware
4. Arrays and strings
5. Data structures
6. Memory efficiency
Pass by value vs. Pass by reference (address)
Pass by Value Pass by Address
means you are making a copy in memory
of the actual parameter's
value that is passed in
Sometimes called pass by reference,
means you are making a copy
in memory of the address
of the actual parameter.
S.A
Operations on pointers
1- Increment
Increments the pointer value by one step ( The step is the size of the pointee)
2- Decrement
Decrements the pointer value by one step
• In other words, the pointer datatype doesn’t control the memory size of the pointer
variable.
• The pointer datatype identifies the behavior of the operations carried out on the pointer
variable. (Read/Write)
S.A
3- Subtraction and Addition
S.A
Passing array to function
Remember that name of the array is the address of its first element.
Then arr is the same as &arr[0].
i.e. if we have an array:
int arr[10];
Which means if you passed the array name arr to function called func for example, then
you are passing address to int. So, the function func prototype should be declared as:
void func ( int *ptr);
Now, inside the function func, *ptr means that you are accessing the first element of the
array, *(ptr+1) means that you are accessing the second element of the array, and so on
…
Subscriptor vs Dereference operators
Example
if we have a pointer int *ptr;
Then ptr[0] is the same as *ptr
Also, ptr[1] is the same as *(ptr+1)
Also, ptr[10] is the same as *(ptr+10).
Multiple Indirection: Pointer to Pointer
S.A
In C, a pointer to a pointer is a type of multiple indirection, where a pointer variable points
to the memory location of another pointer variable.
#include <stdio.h>
int main() {
int x = 10;
int *ptr1 = &x;
int **ptr2 = &ptr1;
printf("The value of x is %dn", x);
printf("The value of *ptr1 is %dn", *ptr1);
printf("The value of **ptr2 is %dn", **ptr2);
**ptr2 = 20;
printf("The value of x is now %dn", x);
return 0;
}
This allows you to indirectly access a variable that is several
levels of indirection away
Pointer to Structure
Example
a pointer to a structure is a pointer that points to the memory location of a structure variable.
This allows you to access the individual members of the structure using pointer dereferencing or
pointer arithmetic.
Pointer to Function
Example
In C, a pointer to a function is a variable that stores the memory address of a function.
This allows you to call a function indirectly by calling the pointer variable instead of the
function directly. This can be useful for creating callback functions and for passing
functions as arguments to other functions.
HOW TO READ C COMPLEX POINTER
EXPRESSION
void pointer
In C, a pointer with an unknown type is a void* pointer. A void* pointer is a generic
pointer that can be used to point to data of any type. This can be useful for creating
functions or data structures that can operate on any data type without needing to
know the specific data type in advance.
Example
Dangling Pointer S.A
Dangling pointer is a pointer that points to deleted or de-allocated object.
he function will destroy after calling with all its local variables , so x
will be destroyed and its address also.
Wild Pointer
Wild pointer is any pointer that is used before initialization.
What are best practices to avoid wild pointers ?
NULL Pointer
NULL pointer is any pointer that is initialized with NULL.
It is good practice to initialize pointers to NULL if you do not have a valid memory
address to assign them to, as this can help prevent errors from dereferencing
uninitialized pointers.
Thank you!
Do you have any questions?

Pointers_in_c_2024.01.10_embedded _c.pptx

  • 1.
    About Me  Name: Ahmed Badr Sayed  Department: Mechatronics Engineer, Ain Shams University  Embedded System, Embedded linux Engineer  E-mail: ahmedbadrsaid1998@gmail.com  Linkedin: www.linkedin.com/in/ahmed-badr-sayed-63267a286
  • 2.
    Pointers Presented by AhmedBadr Sayed GDSC_Al-Azahar’2024
  • 3.
    01 General syntax, Declaringand initializing of pointer and why do we need pointers 02 Pass by value vs. Pass by reference (address) 03 Operations on pointers 04 Passing array to function 05 Multiple Indirection: Pointer to Pointer. 06 Pointer to Structure and pointer to function. 07 Types of pointers. AGENDA
  • 4.
    Pointer: is avariable which capable of storing the initial address of the object(another variable) which it wants to point to. General Syntax Data_type *pointer_name Or Data_type* pointer_name Here the data type refers to the type of the value that the pointer will point to.
  • 5.
  • 6.
    Why do weneed pointers? 1. Dynamic memory allocation 2. Parameter passing 3. Manipulating hardware 4. Arrays and strings 5. Data structures 6. Memory efficiency
  • 7.
    Pass by valuevs. Pass by reference (address) Pass by Value Pass by Address means you are making a copy in memory of the actual parameter's value that is passed in Sometimes called pass by reference, means you are making a copy in memory of the address of the actual parameter.
  • 8.
    S.A Operations on pointers 1-Increment Increments the pointer value by one step ( The step is the size of the pointee) 2- Decrement Decrements the pointer value by one step
  • 9.
    • In otherwords, the pointer datatype doesn’t control the memory size of the pointer variable. • The pointer datatype identifies the behavior of the operations carried out on the pointer variable. (Read/Write)
  • 10.
  • 11.
    S.A Passing array tofunction Remember that name of the array is the address of its first element. Then arr is the same as &arr[0]. i.e. if we have an array: int arr[10]; Which means if you passed the array name arr to function called func for example, then you are passing address to int. So, the function func prototype should be declared as: void func ( int *ptr); Now, inside the function func, *ptr means that you are accessing the first element of the array, *(ptr+1) means that you are accessing the second element of the array, and so on …
  • 12.
    Subscriptor vs Dereferenceoperators Example if we have a pointer int *ptr; Then ptr[0] is the same as *ptr Also, ptr[1] is the same as *(ptr+1) Also, ptr[10] is the same as *(ptr+10).
  • 13.
    Multiple Indirection: Pointerto Pointer S.A In C, a pointer to a pointer is a type of multiple indirection, where a pointer variable points to the memory location of another pointer variable. #include <stdio.h> int main() { int x = 10; int *ptr1 = &x; int **ptr2 = &ptr1; printf("The value of x is %dn", x); printf("The value of *ptr1 is %dn", *ptr1); printf("The value of **ptr2 is %dn", **ptr2); **ptr2 = 20; printf("The value of x is now %dn", x); return 0; } This allows you to indirectly access a variable that is several levels of indirection away
  • 14.
    Pointer to Structure Example apointer to a structure is a pointer that points to the memory location of a structure variable. This allows you to access the individual members of the structure using pointer dereferencing or pointer arithmetic.
  • 15.
    Pointer to Function Example InC, a pointer to a function is a variable that stores the memory address of a function. This allows you to call a function indirectly by calling the pointer variable instead of the function directly. This can be useful for creating callback functions and for passing functions as arguments to other functions.
  • 16.
    HOW TO READC COMPLEX POINTER EXPRESSION
  • 17.
    void pointer In C,a pointer with an unknown type is a void* pointer. A void* pointer is a generic pointer that can be used to point to data of any type. This can be useful for creating functions or data structures that can operate on any data type without needing to know the specific data type in advance. Example
  • 18.
    Dangling Pointer S.A Danglingpointer is a pointer that points to deleted or de-allocated object. he function will destroy after calling with all its local variables , so x will be destroyed and its address also.
  • 19.
    Wild Pointer Wild pointeris any pointer that is used before initialization. What are best practices to avoid wild pointers ?
  • 20.
    NULL Pointer NULL pointeris any pointer that is initialized with NULL. It is good practice to initialize pointers to NULL if you do not have a valid memory address to assign them to, as this can help prevent errors from dereferencing uninitialized pointers.
  • 21.
    Thank you! Do youhave any questions?