0% found this document useful (0 votes)
15 views4 pages

Understanding Pointers in C++ Programming

The document explains the concept of pointers in C++, detailing how they store memory addresses of variables and how to declare and use them. It covers pointer arithmetic, the relationship between pointers and arrays, and how to create arrays of pointers. Additionally, it discusses passing pointers to functions and using pointers with strings.

Uploaded by

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

Understanding Pointers in C++ Programming

The document explains the concept of pointers in C++, detailing how they store memory addresses of variables and how to declare and use them. It covers pointer arithmetic, the relationship between pointers and arrays, and how to create arrays of pointers. Additionally, it discusses passing pointers to functions and using pointers with strings.

Uploaded by

us318194
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Introduction

Every variable is a memory location and every memory location has its address
defined which can be accessed using ampersand (&) operator which denotes an
address in memory. Consider the following which will print the address of the
variables defined:

#include <iostream>
void main ()
{
int var1;
char var2[10];

cout << "Address of var1 variable: ";


cout << &var1 << endl;

cout << "Address of var2 variable: ";


cout << &var2 << endl;
}

When the above code is compiled and executed, it produces result something as
follows:

Address of var1 variable: 0xbfebd5c0


Address of var2 variable: 0xbfebd5b6

What Are Pointers?


A pointer is a variable whose value is the address of another variable. Like any
variable or constant, you must declare a pointer before you can work with it. The
general form of a pointer variable declaration is:

type * pointer-name;

Here, type is the pointer's base type; it must be a valid C++ type and pointer-name is
the name of the pointer variable. For example:

int *ip; // pointer to an integer


double *dp; // pointer to a double
float *fp; // pointer to a float
char *ch // pointer to character

The actual data type of the value of all pointers, whether integer, float, character, or
otherwise, is the same, a long hexadecimal number that represents a memory
address. The only difference between pointers of different data types is the data type
of the variable or constant that the pointer points to.
Using Pointers in C++:

#include <iostream>
void main ()
{
int var = 20; // actual variable declaration.

abdul_bu@[Link] Page 1
int *ip; // pointer variable

ip = &var; // store address of var in pointer variable

cout << "Value of var variable: ";


cout << var << endl;

// print the address stored in ip pointer variable


cout << "Address stored in ip variable: ";
cout << ip << endl;

// access the value at the address available in pointer


cout << "Value of *ip variable: ";
cout << *ip << endl;
}

When the above code is compiled and executed, it produces result something as
follows:

Value of var variable: 20


Address stored in ip variable: 0xbfc601ac
Value of *ip variable: 20

Pointer Arithmetic
To understand pointer arithmetic, let us consider that ptr is an integer pointer which
points to the address 1000. Assuming 16-bit integers, let us perform the following
arithmatic operation on the pointer:

ptr++;

the ptr will point to the location 1002 because each time ptr is incremented, it will
point to the next integer. This operation will move the pointer to next memory
location without impacting actual value at the memory location. If ptr points to a
character whose address is 1000, then above operation will point to the location
1001 because next character will be available at 1001.

Pointer and Arrays


Pointers and arrays are strongly related. In fact, pointers and arrays are
interchangeable in many cases. For example, a pointer that points to the beginning
of an array can access that array by using either pointer arithmetic or array-style
indexing. Consider the following program:

#include <iostream>
const int MAX = 3;

int main ()
{
int var[MAX] = {10, 100, 200};
int *ptr;

abdul_bu@[Link] Page 2
// let us have array address in pointer.
ptr = var;
for (int i = 0; i < MAX; i++)
{
cout << "Address of var[" << i << "] = ";
cout << ptr << endl;

cout << "Value of var[" << i << "] = ";


cout << *ptr << endl;

// point to the next location


ptr++;
}
}

When the above code is compiled and executed, it produces result something as
follows:

Address of var[0] = 0xbfa088b0


Value of var[0] = 10
Address of var[1] = 0xbfa088b4
Value of var[1] = 100
Address of var[2] = 0xbfa088b8
Value of var[2] = 200

Pointer Arrays
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:

#include <iostream>
const int MAX = 3;
void 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 << "] = ";

abdul_bu@[Link] Page 3
cout << *ptr[i] << endl;
}
}

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

Pointer and function


C++ allows you to pass a pointer to a function. To do so, simply declare the function
parameter as a pointer type.

Example is given in “Passing Values by Address to a function”

Pointer and String


A string is an array of characters. A pointer of type char can refer to a string. For
example:

char name[] = ”Khalid Khan”;


char *ptr = name

The first statement declared and initializes a string. The second statement declares a
pointer variable and initializes it to the string name.

#include <iostream.h>
#include <conio.h>
void main()
{
char name[20], *ptr;
cout<<"Enter Your Name:";
cin>>name;
ptr=name;
cout<<ptr;
}

Output of the above program is:


Enter Your Name: Hameed
Hameed

abdul_bu@[Link] Page 4

You might also like