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

odoocms.class.material-5020-material_file

The document explains pointers in C++, detailing how to declare and use them to store memory addresses. It includes examples of assigning addresses to pointers, accessing values using the dereference operator, and using pointers with arrays. Additionally, it demonstrates how array names can act as pointers for accessing elements.

Uploaded by

hjamshaid81
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

odoocms.class.material-5020-material_file

The document explains pointers in C++, detailing how to declare and use them to store memory addresses. It includes examples of assigning addresses to pointers, accessing values using the dereference operator, and using pointers with arrays. Additionally, it demonstrates how array names can act as pointers for accessing elements.

Uploaded by

hjamshaid81
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Pointers

Address in C++

If we have a variable var in our program, &var will give us its address in the memory.

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;
}

As mentioned above, pointers are used to store addresses rather than values.

Here is how we can declare pointers.

int *pointVar;

We can also declare pointers in the following way.

int* pointVar; // preferred synta


Let's take another example of declaring pointers.

int* pointVar, p;

Assigning Addresses to Pointers


Here is how we can assign addresses to pointers:

int* pointVar, var;


var = 5;

// assign address of var to pointVar pointer


pointVar = &var;

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

Get the Value from the Address Using Pointers

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

int* pointVar, var;


var = 5;

// assign address of var to pointVar


pointVar = &var;

// access value pointed by pointVar


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

In the above code, the address of var is assigned to pointVar. We have used the *pointVar to get
the value stored in that address.
Example : Changing Value Pointed by Pointers
#include <iostream>
using namespace std;
int main() {
int var = 5;
int* pointVar;
// store address of var
pointVar = &var;
// print var
cout << "var = " << var << endl;
// print *pointVar
cout << "*pointVar = " << *pointVar << 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 *pointVar
cout << "*pointVar = " << *pointVar << endl
<< endl;
cout << "Changing value of *pointVar to 16:" << endl;
// change value of var to 16
*pointVar = 16;

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

Output : ????

C++ Pointers and Arrays


Example:

int *ptr;
int arr[5];

// store the address of the first


// element of arr in ptr
ptr = arr;

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;
}

 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.

Example: 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;
}
 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.
 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