![]() |
| Pointer Variable in C++ Explained with Examples |Beginner to Advanced Guide |
Pointer in C++ Programming
Pointer: Pointer is a special type of variable it can be used to store the address of another variable. It can store the address of same data type means an integer pointer can store the address of integer variable. Pointer variable used asterisk [*] symbol with any variable at the time of declaring.
We can use ampersand (&) symbol to get the address of variable. Pointers are one of the most powerful features of C++ programming. A pointer is a variable that stores the memory address of another variable.
pointer is a variable that stores the memory address of another variable. Pointers are powerful and versatile features of the C++ language and are used for various purposes, including dynamic memory allocation, working with arrays, and accessing data structures.
Advantages of Pointer in C++
- Dynamic Memory Allocation: Pointer is a allocate memory at runtime.
- Efficient Array Processing: Pointer can be used direct memory access.
- Pass by Reference: Pointer is a modify variables in functions efficiently.
- Data Structures: Pointer cab be used build linked lists, trees, graphs in C++.
- Function Pointers: Pointer can be store and call functions dynamically.
- Memory Efficiency: Pointer is a avoid copying large data structures.
Syntax of Pointer Declaration in C++
data_type *pointer_variable; declaration int x=100; int *p; p=&x; p now can be store the address |
Example of Pointer in C++ Programming
int main(){ int x=100; int *p; p=&x; cout<<"Value of x:"<<x<<endl; cout<<"Address of x:"<<&x<<endl; cout<<"Value of p:"<<*p<<endl; cout<<"Address of p:"<<&p; } ============OUTPUT============ Value of x:100 Address of x:0x7fff853919ec Value of p:100 Address of p:0x7fff853919e0 |
Example of Different Data Type Pointers in C++
int main(){ int x=300; float marks=86.90; char grade='A'; double salary=25839.8978; int *xp; float *mp; char *gp; double *sp; xp=&x; mp=&marks; gp=&grade; sp=&salary; cout<<"Integer: Value of x:"<<x<<endl; cout<<"Float: Value of Marks:"<<marks<<endl; cout<<"Char: Value of Grade:"<<grade<<endl; cout<<"Double: Value of Salary:"<<salary<<endl; cout<<"================================="<<endl; cout<<"sizeof Integer Pointer:bytes "<<sizeof(x)<<endl; cout<<"sizeof of Float Pointer:bytes "<<sizeof(marks); cout<<"sizeof of Character Pointer:bytes "<<sizeof(grade); } ============OUTPUT============ Integer: Value of x:300 Float: Value of Marks:86.900002 Char: Value of Grade: A Double: Value of Salary:25839.9 ================================= sizeof Integer Pointer: 4 bytes sizeof of Float Pointer: 4 bytes sizeof of Character Pointer: 1 bytes |
Null Pointer in C++
int main(){ int *p1 = NULL; int *p2 = 0; int *p3; int n = 100; int *p4 = &n; if (p1 == NULL) { cout<<"p1 is NULL pointer"<<endl; } if (p4 != NULL) { cout<<"p4 points to value:"<<*p4<<endl; } p4 = NULL; cout<<"After assigning NULL p4 Value is:"<<p4; } ============OUTPUT============ p1 is NULL pointer p4 points to value: 100 After assigning NULL p4 Value is: 0 |
Pointers With Arrays in CPP
Pointer with Array: A pointers and arrays have a very close relationship in C++ programming. Array names are essentially constant pointers to the first element of the array. Understanding this relationship is effective C programming and memory management.
Pointer with Array in C++ Program
int main(){ int a[5] ={10, 20, 30, 40, 50}; int *p=a; cout<<"Array Name is a:"<<a<<endl; cout<<"Address of First Element:"<<&a[0]<<endl; cout<<"Pointer Value p is:"<<p<<endl; cout<<"Accessing first element:"<<endl; cout<<"a[0]:"<<a[0]<<endl; cout<<"*a:"<<*a<<endl; cout<<"*p:"<<*p<<endl; cout<<"p[0]:"<<p[0]; } ============OUTPUT============ Array Name is a: 000000000062FE00 Address of First Element: 000000000062FE00 Pointer Value p is: 000000000062FE00 Accessing first element: a[0]: 10 *a: 10 *p: 10 p[0]: 10 |
Pointers with Functions in CPP
Pointers with Function: A pointer functions work important ways in C++ programming. Functions can receive pointers as parameters, return pointers, and you can even have pointers to functions. This enables powerful programming techniques like "call by reference", "dynamic function calls" and efficient data processing.
Passing Pointers to Functions (Call by Reference)
void incrementByValue(int n) { n++; } void incrementByReference(int *np) { (*np)++; cout<<"Inside incrementByReference:"<<*np<<endl; } void swap(int *a, int *b){ int temp = *a; *a = *b; *b = temp; } int main() { int x = 10, y = 20; cout<<"Original values: x ="<<x<<" y="<<y<<endl; incrementByValue(x); cout<<"After incrementByValue: x ="<<x<<endl; incrementByReference(&x); cout<<"After incrementByReference: x ="<<x<<endl; cout<<"Before swap: x="<<x<<" y="<<y<<endl; swap(&x, &y); cout<<"After swap: x="<<x<<" y="<<y; } ============OUTPUT============ Original values: x =10 y=20 After incrementByValue: x =10 Inside incrementByReference:11 After incrementByReference: x =11 Before swap: x=11 y=20 After swap: x=20 y=11 |
Pointers with Strings in CPP
Pointer with Strings: Pointer can be used combination of character arrays and since arrays and pointers have a close relationship understanding how pointers work with string. String manipulation using pointers is often more efficient and provides greater flexibility than array-based approaches.
String Pointer in C++ Examples
int main(){ char s[100] = "Jiocoding"; char *p; p=s; cout<<"Website Name is:"<<s<<endl; cout<<" First Character is:"<<*p; } ============OUTPUT============ Website Name is:Jiocoding First Character is:J |
Pointers to Pointers (Double Pointers) in CPP
A pointer to pointer is often referred to as a "double pointer". also known as a double pointer is a pointer that stores the address of another pointer. This concept allows for indirect access to values and enables powerful programming techniques like dynamic 2D arrays modifying pointer values in functions and creating complex data structures.
Pointer to Pointer in C++ Example
int main(){ int x = 100; int *p; int *pp; p=&x; pp=&x; cout<<"Value of x ="<<x<<endl; cout<<"Single Pointer Value *p ="<<*p<<endl; cout<<"Pointer to Pointer Value pp ="<<pp; } ============OUTPUT============ Value of x = 100 Single Pointer Value *p = 100 Pointer to Pointer Value pp =0x7fff7a9d63cc |

