Data Structures SWE 👉 Pointers 😲 How memory works ? Int x ; x = 12; OR Int x = 12; Float y = 3.99; 1 2 3. 99 X Y Beside its name, Any thing stored in the memory ( such as variables ), is allocated to a unique address. We can access any variable by its name or its address . To get the variable address we use address operator ( & ) X 0 1 1 Y 2 8 9 10 16 17 18 24 25 26 2 3 4 5 6 7 11 12 13 14 15 21 22 23 29 30 31 19 27 3. 20 28 99 Cout << y; // OUTPUT -> 3.99 ( Print value of y ) cout << &y; // OUTPUT -> 19 ( Print address of y ) NOTE : THE Addresses used on this example is not real ones, its only for declaration Real addresses is represented on hexadecimal form Usually starts with 0x to declare that it is a hexa form Ex: 0x5a01 0x7a09 0x00000123 Pointers > Pointer is variable that holds an address > alternate way to access memory locations Pointer definition int *ptr; or int* ptr; ( * ) is called the indirection operator How to assign a value to a pointer int num = 25; // assign the value 25 to the variable num int *p; // definition of the pointer p = &num; // pointed to the valeu 25 by the pointer p Memory num p 25 0x0012 Test Test Cout << num; // 25 // 0x0012 Cout << p; Cout << &num; // 0x0012 Cout << &p; // 0x0123 Cout << *p; // 25 *p = 20; Cout << num; //20 GDSC Benha University One variable can be denoted by multiple pointers Ptr_1 0x0016 int num =5; int *ptr_1, *ptr_2, *ptr_3; ptr_1 = &num; Ptr_2 0x0016 ptr_2 = &num; ptr_3 = ptr_1; num Ptr_3 0x0016 *ptr_1 *ptr_2 *Ptr_3 Pointer to pointer ( double pointer ) Pointer is a variable , so I can point to it with another pointer 0x0016 int num = 8; int *ptr_1, **ptr_2; 0x0016 ptr_1 = &num; ptr_2 = &ptr_1; Ptr_2 Ptr_1 0x0a13 0x0016 0x6a19 0x0a13 num 0x0016 note while using pointers NULL initialization Ptr_1 int *ptr_1 = 0; NULL int num = 12; Ptr_1 ptr_1 = &num; 0x0016 initial value must have the correct type float num = 0; int *ptr = &num; // ERROR float *ptr = &num; // correct num 0x0016 Pointers and array int arr[] = {1, 2, 3}; By default the array name is a pointer that points to the first address of the array arr 0x0016 Cout << arr ; // 0x0016 Cout << arr[0]; // 1 1 2 3 0x0016 0x0017 0x0018 Pointers and array int arr[] = {1, 2, 3}; By default the array name is a pointer that points to the first address of the array arr Cout << Cout << Cout << // 0x0016 arr ; *arr ; arr[0]; // 1 int *ptr = arr; Cout << ptr[1]; // 2 // 1 0x0016 1 2 3 0x0016 0x0017 0x0018 playing with Pointers and array Pointer Arithmetics int arr[] = {1, 2, 3}; int *ptr = arr; Cout << ( ptr+1 ); // 7 Cout << ( ptr+2 ); // 8 5 0x0016 prt Note : >> the number we add to ptr is not a value it’s a unit >> don’t forget the brackets ( ) 7 8 0x0017 Prt+1 0x0018 Prt+2 More playing 😂 with Pointers and array Pointer Arithmetic int arr[] = {1, 2, 3}; int *ptr = arr; Ptr += 2; // ptr = ptr + 2 Cout << *ptr ; // 8 5 0x0016 prt 7 8 0x0017 Prt+1 0x0018 Prt+2 More playing 😂 with Pointers and array Pointer Arithmetic int arr[] = {1, 2, 3}; int *ptr = arr; Ptr += 2; Cout << ptr - arr ; Cout << *( ptr - arr ); 5 0x0016 prt // ptr = ptr + 2 // 2 // ERROR 7 8 0x0017 Prt+1 0x0018 Prt+2 More playing 😂 with Pointers and array Pointer Arithmetic int arr[] = {1, 2, 3}; int *ptr = arr; Cout << *( ptr++ ); Cout << *( ++ptr ); // 5 // 6 // 7 5 0x0016 prt Cout << ++( *ptr ); 7 8 0x0017 Prt+1 0x0018 Prt+2 Common errors while using pointers >> Uninitialized pointers ❌ Int *ptr; *ptr = 56; ✅ Int num; Int *ptr =&num; *ptr = 56; int *q,*p; p = q; *p = 25; int *q = &num,*p; p = q; *p = 25; Common errors while using pointers >> Uninitialized pointers ❌ Int *ptr; *ptr = 56; ✅ Int num; Int *ptr =&num; *ptr = 56; int *q,*p; p = q; *p = 25; int *q = &num,*p; p = q; *p = 25; Common errors while using pointers >> NULL pointer direct usage ❌ Int *ptr = 0; *ptr = 56; ✅ Int *ptr = 0; Int num; Int *ptr =&num; *ptr = 56; Structures Structures (also called structs) are a way to group several related variables into one place. Each variable in the structure is known as a member of the structure. Unlike an array, a structure can contain many different data types (int, string, bool, etc.). To create a structure, use the struct keyword and declare each of its members inside curly braces. classes A class is a user-defined data type that we can use in our program, and it works as an object constructor, or a "blueprint" for creating objects. To create a class, use the class keyword Thanks 🤍 See you next session