Pointers are powerful features of C++ that differentiates it from other programming languages like Java and Python.
Pointers are used in C++ program to access the memory and manipulate the address.
Address in C++
To understand pointers, you should first know how data is stored on the computer.
Each variable you create in your program is assigned a location in the computer's memory. The value the variable stores is actually stored in the location assigned.
To know where the data is stored, C++ has an & operator. The & (reference) operator gives you the address occupied by a variable.
If var
is a variable then, &var
gives the address of that variable.
Example 1: Address in C++
#include <iostream>
using namespace std;
int main()
{
int var1 = 3;
int var2 = 24;
int var3 = 17;
cout << &var1 << endl;
cout << &var2 << endl;
cout << &var3 << endl;
}
Output
0x7fff5fbff8ac
0x7fff5fbff8a8
0x7fff5fbff8a4
Note: You may not get the same result on your system.
The 0x in the beginning represents the address is in hexadecimal form.
Notice that first address differs from second by 4-bytes and second address differs from third by 4-bytes.
This is because the size of integer (variable of type int
) is 4 bytes in 64-bit system.
#include <iostream>
using namespace std;
int main() {
int *pc, c;
c = 5;
cout << "Address of c (&c): " << &c << endl;
cout << "Value of c (c): " << c << endl << endl;
pc = &c; // Pointer pc holds the memory address of variable c
cout << "Address that pointer pc holds (pc): "<< pc << endl;
cout << "Content of the address pointer pc holds (*pc): " << *pc << endl << endl;
c = 11; // The content inside memory address &c is changed from 5 to 11.
cout << "Address pointer pc holds (pc): " << pc << endl;
cout << "Content of the address pointer pc holds (*pc): " << *pc << endl << endl;
*pc = 2;
cout << "Address of c (&c): " << &c << endl;
cout << "Value of c (c): " << c << endl << endl;
return 0;
}
Output
Address of c (&c): 0x7fff5fbff80c
Value of c (c): 5
Address that pointer pc holds (pc): 0x7fff5fbff80c
Content of the address pointer pc holds (*pc): 5
Address pointer pc holds (pc): 0x7fff5fbff80c
Content of the address pointer pc holds (*pc): 11
Address of c (&c): 0x7fff5fbff80c
Value of c (c): 2
Explanation of program
- When
c = 5;
the value 5 is stored in the address of variable c - 0x7fff5fbff8c.
- When
pc = &c;
the pointer pc holds the address of c - 0x7fff5fbff8c, and the expression (dereference operator) *pc
outputs the value stored in that address, 5.
- When
c = 11;
since the address pointer pc holds is the same as c - 0x7fff5fbff8c, change in the value of c is also reflected when the expression *pc
is executed, which now outputs 11.
- When
*pc = 2;
it changes the content of the address stored by pc - 0x7fff5fbff8c. This is changed from 11 to 2. So, when we print the value of c, the value is 2 as well.
Common mistakes when working with pointers
Suppose, you want pointer pc to point to the address of c. Then,
int c, *pc;
pc=c; /* Wrong! pc is address whereas, c is not an address. */
*pc=&c; /* Wrong! *pc is the value pointed by address whereas, %amp;c is an address. */
pc=&c; /* Correct! pc is an address and, %amp;pc is also an address. */
*pc=c; /* Correct! *pc is the value pointed by address and, c is also a value. */
Pointers are the variables that hold address. Not only can pointers store address of a single variable, it can also store address of cells of an
array.
Consider this example:
int* ptr;
int a[5];
ptr = &a[2]; // &a[2] is the address of third element of a[5].
Suppose, pointer needs to point to the fourth element of an array, that is, hold address of fourth array element in above case.
Since ptr points to the third element in the above example, ptr + 1
will point to the fourth element.
You may think, ptr + 1
gives you the address of next byte to the ptr
. But it's not correct.
This is because pointer ptr is a pointer to an int
and size of int
is fixed for a operating system (size of int
is 4 byte of 64-bit operating system). Hence, the address between ptr
and ptr + 1
differs by 4 bytes.
If pointer ptr was pointer to char
then, the address between ptr
and ptr + 1
would have differed by 1 byte since size of a character is 1 byte.
Example 1: C++ Pointers and Arrays
C++ Program to display address of elements of an array using both array and pointers
#include <iostream>
using namespace std;
int main()
{
float arr[5];
float *ptr;
cout << "Displaying address using arrays: " << endl;
for (int i = 0; i < 5; ++i)
{
cout << "&arr[" << i << "] = " << &arr[i] << endl;
}
// ptr = &arr[0]
ptr = arr;
cout<<"\nDisplaying address using pointers: "<< endl;
for (int i = 0; i < 5; ++i)
{
cout << "ptr + " << i << " = "<< ptr + i << endl;
}
return 0;
}
Output
Displaying address using arrays:
&arr[0] = 0x7fff5fbff880
&arr[1] = 0x7fff5fbff884
&arr[2] = 0x7fff5fbff888
&arr[3] = 0x7fff5fbff88c
&arr[4] = 0x7fff5fbff890
Displaying address using pointers:
ptr + 0 = 0x7fff5fbff880
ptr + 1 = 0x7fff5fbff884
ptr + 2 = 0x7fff5fbff888
ptr + 3 = 0x7fff5fbff88c
ptr + 4 = 0x7fff5fbff890
#include <iostream>
using namespace std;
const int MAX = 3;
int main () {
int var[MAX] = {10, 100, 200};
for (int i = 0; i < MAX; i++) {
cout << "Value of var[" << i << "] = ";
cout << var[i] << endl;
}
return 0;
}
When the above code is compiled and executed, it produces the following result −
Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200
There may be a situation, when we want to maintain an array, which can store pointers to an int or char or any other data type available. Following is the declaration of an array of pointers to an integer −
int *ptr[MAX];
This declares ptr as an array of MAX integer pointers. Thus, each element in ptr, now holds a pointer to an int value. Following example makes use of three integers which will be stored in an array of pointers as follows −
Live Demo#include <iostream>
using namespace std;
const int MAX = 3;
int main () {
int var[MAX] = {10, 100, 200};
int *ptr[MAX];
for (int i = 0; i < MAX; i++) {
ptr[i] = &var[i]; // assign the address of integer.
}
for (int i = 0; i < MAX; i++) {
cout << "Value of var[" << i << "] = ";
cout << *ptr[i] << endl;
}
return 0;
}
When the above code is compiled and executed, it produces the following result −
Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200
You can also use an array of pointers to character to store a list of strings as follows −
Live Demo#include <iostream>
using namespace std;
const int MAX = 4;
int main () {
const char *names[MAX] = { "Zara Ali", "Hina Ali", "Nuha Ali", "Sara Ali" };
for (int i = 0; i < MAX; i++) {
cout << "Value of names[" << i << "] = ";
cout << (names + i) << endl;
}
return 0;
}
When the above code is compiled and executed, it produces the following result −
Value of names[0] = 0x7ffd256683c0
Value of names[1] = 0x7ffd256683c8
Value of names[2] = 0x7ffd256683d0
Value of names[3] = 0x7ffd256683d8
pointer and array
#include <iostream>
using namespace std;
int main ()
{
int numbers[5];
int * p;
p = numbers; *p = 10;
p++; *p = 20;
p++; *p = 30;
p++; *p = 40;
p++; *p = 50;
for (int n=0; n<5; n++)
cout << numbers[n] << ", ";
return 0;
}
pointer and arrays.
#include <iostream>
using namespace std;
int main ()
{
cout <<"simple array out put inilization\n";
int numbers[5]={100,200,300,400,500};
int *p;
p = numbers;
// for simple out put
for (int n=0; n<5; n++)
cout << numbers[n] << ", ";
cout <<"\n\n loop using pointers";
for (int n=0; n<5; n++)
cout << *(p+n) << ", ";
cout<<"GGGGGGGGGGGGGGGGGGGGGG\n\n";
cout << numbers[2];
p = &numbers[2];
*p = 30;
cout << "after *p=30................>"<<numbers[2];
p = numbers + 3; *p = 40;
p = numbers; *(p+4) = 50;
for (int n=0; n<5; n++)
cout << numbers[n] << ", ";
return 0;
}
output
simple array out put inilization
100, 200, 300, 400, 500,
loop using pointers100, 200, 300, 400, 500, GGGGGGGGGGGGGGGGGGGGGG
300after *p=30................>30100, 200, 30, 40, 50,
--------------------------------
Process exited with return value 0
Press any key to continue . . .
Hello Admin,
ReplyDeleteEvery factor you make in your program is appointed an area in the PC's memory. The worth of the variable stores is really put away in the area allocated. To know where the information is put away, C++ has an and administrator. The and (reference) administrator gives you the location involved by a variable. On the off chance that var is a variable, at that point, &var gives the location of that variable.
Regards,
Thanks
RITU
Hello Admin,
ReplyDeleteEvery factor you make in your program is appointed an area in the PC's memory. The worth of the variable stores is really put away in the area allocated. To know where the information is put away, C++ has an and administrator. The and (reference) administrator gives you the location involved by a variable. On the off chance that var is a variable, at that point, &var gives the location of that variable.
Regards,komal
Thanks