Blog
18 Best File Compression Software | Zip | Unzip Program
Zip is an archive format that offers data compression without data loss. A zip file may contain...
In C++, a pointer refers to a variable that holds the address of another variable. Like regular variables, pointers have a data type. For example, a pointer of type integer can hold the address of a variable of type integer. A pointer of character type can hold the address of a variable of character type.
You should see a pointer as a symbolic representation of a memory address. With pointers, programs can simulate call-by-reference. They can also create and manipulate dynamic data structures. In C++, a pointer variable refers to a variable pointing to a specific address in a memory pointed by another variable.
In this C++ tutorial, you will learn:
To understand C++ pointers, you must understand how computers store data.
When you create a variable in your C++ program, it is assigned some space the computer memory. The value of this variable is stored in the assigned location.
To know the location in the computer memory where the data is stored, C++ provides the & (reference) operator. The operator returns the address that a variable occupies.
For example, if x is a variable, &x returns the address of the variable.
The declaration of C++ takes the following syntax:
datatype *variable_name;
Here is an example of valid pointer declarations in C++:
int *x; // a pointer to integer double *x; // a pointer to double float *x; // a pointer to float char *ch // a pointer to a character
The reference operator (&) returns the variable's address.
The dereference operator (*) helps us get the value that has been stored in a memory address.
For example:
If we have a variable given the name num, stored in the address 0x234 and storing the value 28.
The reference operator (&) will return 0x234.
The dereference operator (*) will return 5.
#include <iostream> using namespace std; int main() { int x = 27; int *ip; ip = &x; cout << "Value of x is : "; cout << x << endl; cout << "Value of ip is : "; cout << ip<< endl; cout << "Value of *ip is : "; cout << *ip << endl; return 0; }
Output:
How this works:
Here is a screenshot of the code:
Code Explanation:
Arrays and pointers work based on a related concept. There are different things to note when working with arrays having pointers. The array name itself denotes the base address of the array. This means that to assign the address of an array to a pointer, you should not use an ampersand (&).
For example:
p = arr;
The above is correct since arr represents the arrays' address. Here is another example:
p = &arr;
The above is incorrect.
We can implicitly convert an array into a pointer. For example:
int arr [20]; int * ip;
Below is a valid operation:
ip = arr;
After the above declaration, ip and arr will be equivalent, and they will share properties. However, a different address can be assigned to ip, but we cannot assign anything to arr.
This example shows how to traverse an array using pointers:
#include <iostream> using namespace std; int main() { int *ip; int arr[] = { 10, 34, 13, 76, 5, 46 }; ip = arr; for (int x = 0; x < 6; x++) { cout << *ip << endl; ip++; } return 0; }
Output:
Here is a screenshot of the code:
Code Explanation:
If there is no exact address that is to be assigned, then the pointer variable can be assigned a NULL. It should be done during the declaration. Such a pointer is known as a null pointer. Its value is zero and is defined in many standard libraries like iostream.
#include <iostream> using namespace std; int main() { int *ip = NULL; cout << "Value of ip is: " << ip; return 0; }
Output:
Here is a screenshot of the code:
Code Explanation:
With C++, you can manipulate data directly from the computer's memory.
The memory space can be assigned or re-assigned as one wishes. This is made possible by Pointer variables.
Pointer variables point to a specific address in the computer's memory pointed to by another variable.
It can be declared as follows:
int *p;
Or,
int* p;
In the you example, we have declared the pointer variable p.
It will hold a memory address.
The asterisk is the dereference operator that means a pointer to.
The pointer p is pointing to an integer value in the memory address.
#include <iostream> using namespace std; int main() { int *p, x = 30; p = &x; cout << "Value of x is: " << *p; return 0; }
Output:
Here is a screenshot of the code:
Code Explanation:
Functions in C++ can return only one value. Further, all the variables declared in a function are allocated on the function call stack. As soon as the function returns, all the stack variables are destroyed.
Arguments to function are passed by value, and any modification done on the variables doesn't change the value of the actual variables that are passed. Following example helps illustrate this concept:-
Example 5:
#include <iostream> using namespace std; void test(int*, int*); int main() { int a = 5, b = 5; cout << "Before changing:" << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; test(&a, &b); cout << "\nAfter changing" << endl; cout << "a = " << a << endl; cout << "b = " << b << endl; return 0; } void test(int* n1, int* n2) { *n1 = 10; *n2 = 11; }
Output:
Here is a screenshot of the code:
Code Explanation:
Even Though, new values are assigned to variable a and b inside the function test, once the function call completes, the same is not reflected the outer function main.
Using pointers as function arguments helps to pass the variable's actual address in the function, and all the changes performed on the variable will be reflected in the outer function.
In the above case, the function 'test' has the address of variables 'a' and 'b.' These two variables are directly accessible from the function 'test', and hence any change done to these variables are reflected in the caller function 'main.'
Here, are pros/benefits of using Pointers
Zip is an archive format that offers data compression without data loss. A zip file may contain...
SolarMovie is a website that allows you to watch movies online, free without any payment. The...
Download PDF 1) Explain what is Model-View-Controller? MVC is a software architecture pattern for...
YouTube to MP3 Converters are applications that enable you to save YouTube video clips in mp3...
Introduction to Data Analysis Data analysis can be divided into three parts Extraction: First, we...
Download PDF 1) What is UNIX? It is a portable operating system that is designed for both...