0% found this document useful (0 votes)
18 views

C Pointers Module

C++ pointers are variables that store the memory addresses of other variables and can be used to access and modify the values stored at those addresses. Pointers can also store the address of cells of an array and can be used to point to every element of the array. Common mistakes when working with pointers include assigning a pointer the value of a variable instead of the address of the variable and dereferencing a pointer before assigning an address to it.

Uploaded by

samanthachua155
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

C Pointers Module

C++ pointers are variables that store the memory addresses of other variables and can be used to access and modify the values stored at those addresses. Pointers can also store the address of cells of an array and can be used to point to every element of the array. Common mistakes when working with pointers include assigning a pointer the value of a variable instead of the address of the variable and dereferencing a pointer before assigning an address to it.

Uploaded by

samanthachua155
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

C++ Pointers

In C++, pointers are variables that store the memory addresses of other
variables.

Address in C++
Every variable we declare in our program has an associated location in the
memory, which we call the memory address of the variable.
If we have a variable var in our program, &var returns its memory address. For
example,
#include <iostream>
using namespace std;

int main()
{
// declare variables
int var1 = 3;
int var2 = 24;
int var3 = 17;

// print address of var1


cout << "Address of var1: "<< &var1 << endl;

// print address of var2


cout << "Address of var2: " << &var2 << endl;

// print address of var3


cout << "Address of var3: " << &var3 << endl;
}

Output
Address of var1: 0x7fff5fbff8ac
Address of var2: 0x7fff5fbff8a8
Address of var3: 0x7fff5fbff8a4

Here, 0x at the beginning represents the address in the hexadecimal form.


Notice that the first address differs from the second by 4 bytes, and the
second address differs from the third by 4 bytes.
The difference is because the size of an int is 4 bytes in a 64-bit system.

Note: You may not get the same results when you run the program. This is
because the address depends on the environment in which the program runs.

C++ Pointers
Here is how we can declare pointers:

int *point_var;

Here, we have declared a variable point_var which is a pointer to an int .

We can also declare pointers in the following way:

int* point_var; // preferred syntax

Assigning Addresses to Pointers


Here is how we can assign addresses to pointers:
int var = 5;
int* point_var = &var;

Here, 5 is assigned to the variable var . And the address of var is assigned to
the point_var pointer with the code point_var = &var .

Note: It is a good practice to initialize pointers as soon as they are declared.

Get the Value from the Address Using Pointers


To get the value pointed by a pointer, we use the * operator. For example:

int var = 5;

// assign address of var to point_var


int* point_var = &var;

// access value pointed by point_var


cout << *point_var << endl; // Output: 5

In the above code, the address of var is assigned to point_var . We have used
the *point_var to get the value stored in that address.
When * is used with pointers, it's called the dereference operator. It operates
on a pointer and gives the value pointed by the address stored in the pointer.
That is, *point_var = var .

Note: In C++, point_var and *point_var are completely different. We cannot do


something like *point_var = &var; . Here, point_var is a pointer that stores the
address of variable it points to while *point_var returns the value stored at the
address pointed by point_var .
Example 1: Working of C++ Pointers
#include <iostream>
using namespace std;
int main() {
int var = 5;

// store address of var


int* point_var = &var;

// print value of var


cout << "var = " << var << endl;

// print address of var


cout << "Address of var (&var) = " << &var << endl
<< endl;

// print pointer point_var


cout << "point_var = " << point_var << endl;

// print the content of the address point_var points to


cout << "Content of the address pointed to by point_var (*point_var) = " <<
*point_var << endl;

return 0;
}

Output

var = 5
Address of var (&var) = 0x61ff08

point_var = 0x61ff08
Content of the address pointed to by point_var (*point_var) = 5

Working of C++ pointers


Changing Value Pointed by Pointers
If point_var points to the address of var , we can change the value of var by
using *point_var .

For example,

int var = 5;
int* point_var = *var;

// change value at address point_var


*point_var = 1;

cout << var << endl; // Output: 1

Here, point_var and &var have the same address; the value of var will also be
changed when *point_var is changed.

Example 2: Changing Value Pointed by Pointers


#include <iostream>
using namespace std;
int main() {
int var = 5;

// store address of var


int* point_var = &var;

// print var
cout << "var = " << var << endl;

// print *point_var
cout << "*point_var = " << *point_var << endl
<< endl;

cout << "Changing value of var to 7:" << endl;


// change value of var to 7
var = 7;

// print var
cout << "var = " << var << endl;

// print *point_var
cout << "*point_var = " << *point_var << endl
<< endl;

cout << "Changing value of *point_var to 16:" << endl;

// change value of var to 16


*point_var = 16;

// print var
cout << "var = " << var << endl;

// print *point_var
cout << "*point_var = " << *point_var << endl;
return 0;
}

Output

var = 5
*point_var = 5

Changing value of var to 7:


var = 7
*point_var = 7

Changing value of *point_var to 16:


var = 16
*point_var = 16

Here point_var holds the address of var , and by


dereferencing point_var with *point_var , we can access and modify the value
stored at that address, which in turn affects the original variable var.
Common Mistakes When Working with Pointers
Suppose we want a pointer point_var to point to the address of var . Then,

int var = 5;

// Wrong!
// point_var is an address but var is not
int* point_var = var;

// Wrong!
// &var is an address
// *point_var is the value stored in &var
*point_var = &var;

// Correct!
// point_var is an address and so is &var
point_var = &var;

// Correct!
// both *point_var and var are values
*point_var = var;

C++ Pointers and Arrays


In C++, Pointers are variables that hold addresses of other variables. Not only
can a pointer store the address of a single variable, it can also store the
address of cells of an array.
Consider this example:

int *ptr;
int arr[5];
// store the address of the first
// element of arr in ptr
ptr = arr;

Here, ptr is a pointer variable while arr is an int array. The code ptr =

arr; stores the address of the first element of the array in variable ptr .

Notice that we have used arr instead of &arr[0] . This is because both are the
same. So, the code below is the same as the code above.

int *ptr;
int arr[5];
ptr = &arr[0];

The addresses for the rest of the array elements are given
by &arr[1] , &arr[2] , &arr[3] , and &arr[4] .

Point to Every Array Elements


Suppose we need to point to the fourth element of the array using the same
pointer ptr .

Here, if ptr points to the first element in the above example then ptr + 3 will
point to the fourth element. For example,

int *ptr;
int arr[5];
ptr = arr;

ptr + 1 is equivalent to &arr[1];


ptr + 2 is equivalent to &arr[2];
ptr + 3 is equivalent to &arr[3];
ptr + 4 is equivalent to &arr[4];

Similarly, we can access the elements using the single pointer. For example,
// use dereference operator
*ptr == arr[0];
*(ptr + 1) is equivalent to arr[1];
*(ptr + 2) is equivalent to arr[2];
*(ptr + 3) is equivalent to arr[3];
*(ptr + 4) is equivalent to arr[4];

Suppose if we have initialized ptr = &arr[2]; then

ptr - 2 is equivalent to &arr[0];


ptr - 1 is equivalent to &arr[1];
ptr + 1 is equivalent to &arr[3];
ptr + 2 is equivalent to &arr[4];

Working of C++
Pointers with Arrays

Note: The address between ptr and ptr + 1 differs by 4 bytes. It is


because ptr is a pointer to an int data. And, the size of int is 4 bytes in a 64-
bit operating system.
Similarly, if pointer ptr is pointing to char type data, then the address
between ptr and ptr + 1 is 1 byte. It is because the size of a character is 1
byte.
Example 1: C++ Pointers and Arrays
// C++ Program to display address of each element of an array

#include <iostream>
using namespace std;

int main()
{
float arr[3];

// declare pointer variable


float *ptr;

cout << "Displaying address using arrays: " << endl;

// use for loop to print addresses of all array elements


for (int i = 0; i < 3; ++i)
{
cout << "&arr[" << i << "] = " << &arr[i] << endl;
}

// ptr = &arr[0]
ptr = arr;

cout<<"\nDisplaying address using pointers: "<< endl;

// use for loop to print addresses of all array elements


// using pointer notation
for (int i = 0; i < 3; ++i)
{
cout << "ptr + " << i << " = "<< ptr + i << endl;
}

return 0;
}
Run Code

Output

Displaying address using arrays:


&arr[0] = 0x61fef0
&arr[1] = 0x61fef4
&arr[2] = 0x61fef8

Displaying address using pointers:


ptr + 0 = 0x61fef0
ptr + 1 = 0x61fef4
ptr + 2 = 0x61fef8

In the above program, we first simply printed the addresses of the array
elements without using the pointer variable ptr .

Then, we used the pointer ptr to point to the address of a[0] , ptr + 1 to point
to the address of a[1] , and so on.

In most contexts, array names decay to pointers. In simple words, array


names are converted to pointers. That's the reason why we can use pointers
to access elements of arrays.

However, we should remember that pointers and arrays are not the same..

Example 2: Array name used as pointer


// C++ Program to insert and display data entered by using pointer notation.

#include <iostream>
using namespace std;

int main() {
float arr[5];

// Insert data using pointer notation


cout << "Enter 5 numbers: ";
for (int i = 0; i < 5; ++i) {
// store input number in arr[i]
cin >> *(arr + i) ;

// Display data using pointer notation


cout << "Displaying data: " << endl;
for (int i = 0; i < 5; ++i) {

// display value of arr[i]


cout << *(arr + i) << endl ;

return 0;
}

Output

Enter 5 numbers: 2.5


3.5
4.5
5
2
Displaying data:
2.5
3.5
4.5
5
2

Here,

1. We first used the pointer notation to store the numbers entered by the user
into the array arr .

cin >> *(arr + i) ;

This code is equivalent to the code below:


cin >> arr[i];

Notice that we haven't declared a separate pointer variable, but rather we are
using the array name arr for the pointer notation.
As we already know, the array name arr points to the first element of the
array. So, we can think of arr as acting like a pointer.
2. Similarly, we then used for loop to display the values of arr using pointer
notation.

cout << *(arr + i) << endl ;

This code is equivalent to

cout << arr[i] << endl ;

You might also like