0% found this document useful (0 votes)
35 views3 pages

Chapter 7-Pointers

Uploaded by

ShaggY
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)
35 views3 pages

Chapter 7-Pointers

Uploaded by

ShaggY
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/ 3

Programming I 2023

Chapter Seven Pointers


7.1. Overview

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.

Addresses in C++
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.

7.2. Pointer Declaration Syntax


The declaration of C++ takes the following syntax:
datatype *variable_name;
 The datatype is the base type of the pointer which must be a valid C++ data type.
 The variable_name is should be the name of the pointer variable.
 Asterisk used above for pointer declaration is similar to asterisk used to perform
multiplication operation. It is the asterisk that marks the variable as a pointer.
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

Page | 1
Programming I 2023
Chapter Seven Pointers
7.3. Reference operator (&) and Deference operator (*)
 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.

Example 1:
#include <iostream>
using namespace std;
int main() { Program Output
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;
}

How this works:

Page | 2
Programming I 2023
Chapter Seven Pointers

7.4. Pointers and Arrays


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.
Example:

This example shows how to traverse an array using pointers:

#include <iostream.h>
int main() {
int *ip;
int arr[] = { 10, 34, 13, 76, 5, 46 }; Program Output
ip = arr;
for (int x = 0; x < 6; x++) { 10
cout << *ip << endl; 34
ip++; 13
} 76
return 0; 5
} 46

Page | 3

You might also like