Pointers are variables that store the memory addresses of other variables. To assign addresses to pointers, the address of the variable is stored in the pointer variable using the ampersand operator. Pointers allow passing of arrays and strings to functions more efficiently and make it possible to return more than one value from a function. Pointers must be properly managed to avoid issues like null pointers, dangling pointers, and memory leaks.
Pointers are variables that store the memory addresses of other variables. To assign addresses to pointers, the address of the variable is stored in the pointer variable using the ampersand operator. Pointers allow passing of arrays and strings to functions more efficiently and make it possible to return more than one value from a function. Pointers must be properly managed to avoid issues like null pointers, dangling pointers, and memory leaks.
Pointers are variables that store the memory addresses of other variables. To assign addresses to pointers, the address of the variable is stored in the pointer variable using the ampersand operator. Pointers allow passing of arrays and strings to functions more efficiently and make it possible to return more than one value from a function. Pointers must be properly managed to avoid issues like null pointers, dangling pointers, and memory leaks.
• What is pointers ? In C++, pointers are variables that store the memory addresses of other variables.
• Address in C++ :
If we have a variable var in our program, &var will give us its
address in the memory. To Address the variables use ampersand operator (&) . • HOW WE CAN ASSIGNING HOW CAN WE DECLARE ADDRESSES TO POINTERS ? THE VARIABLES IN • Here is how we can assign addresses POINTER ? to pointers: THE GENERAL FORM TO • int var = 5; DECLARE THE POINTER IS : • int* pointVar; type *variableName ; • // assign address of var to For Example : pointVar pointer int *x; • pointVar = &var; Char *ch; FOR EXAMPLE: #include <iostream> using namespace std; int main() { // declare variables int var1 = 3; • Output int var2 = 24; • Address of var1: 0x7fff5fbff8ac int var3 = 17; // print address of var1 • Address of var2: 0x7fff5fbff8a8 cout << “Address of var1: “<< &var1 << endl; • Address of var3: 0x7fff5fbff8a4 // print address of var2 cout << “Address of var2: “ << &var2 << endl; // print address of var3 cout << “Address of var3: “ << &var3 << endl; } Changing Value Pointed by Pointers If pointVar points to the address of var, we can change the value of var by using *pointVar. • ADVANTAGES OF POINTER: For example, • Pointers save the memory. int var = 5; int* pointVar; • Pointers reduce the length and // assign address of var complexity of a program. pointVar = &var; • Pointers allow the passing of // change value at address pointVar arrays and strings to function *pointVar = 1; more efficiently. cout << var << endl; • Pointers make it possible to // Output: 1 return more than one value from Here, pointVar and &var have the same address, the function. the value of var will also be changed when *pointVar is changed. • Pointers increase the processing NULL POINTERS •
If there is no exact address that is • #include <iostream>
to be assigned, then the pointer • using namespace std; variable can be assigned a NULL. • int main() { It should be done during the • int *ip = NULL; declaration. Such a pointer is • cout << “Value of ip is: ” <<ip; known as a null pointer. Its value • return 0; is zero and is defined in many • } standard libraries like iostream • Output : Value of ip is: 000000 DANGLING POINTERS • When a pointer is pointing at the memory #include<iostream> address of a variable but after some time that Using namespace std; variable is deleted from that memory location int main() { while the pointer is still pointing to it, then int *ptr1 = new int; such a pointer is known as a dangling pointer *ptr1 = 1; . int *ptr2 = new int; *ptr2= 8; • A pointer pointing to a memory location that Delete = ptr2; has been deleted (or freed) is called dangling pointer Ptr2 = NULL; } MEMORY LEAKAGE
• Memory leakage occurs in C++ when programmers
allocates memory by using new keyword and forgets to deallocate the memory by using delete() function or delete[] operator. One of the most memory leakage occurs in C++ by using wrong delete operator. • #include <iostream> • using namespace std; POINTERS AND • int main() {
ARRAYS? • int *ip;
• int arr[ ] = { 10, 34, 13, 76, 5, 46 }; Arrays and pointers work based on a • ip = arr; related concept. There are different things • for (int x = 0; x < 6; x++) { to note when working with arrays having pointers. • cout << *ip << endl; • The array name itself denotes the base • ip++; address of the array. This means that to • } assign the address of an array to a pointer, you should not use an ampersand (&). • return 0; p = arr; • }