Home | | Object Oriented Programming | Pointers - C++

Chapter: Object Oriented Programming(OOP) : Overview

Pointers - C++

The address operator (&) returns the memory address of a variable.

POINTERS

 

Getting the address of a Variable

The address operator (&) returns the memory address of a variable.

//  This program uses the & operator to determine a variable’s

//  address and the sizeof operator to determine its size.

 

#include <iostream.h> void main(void)

{

 

int x = 25;

cout << "The address of x is " << &x << endl;

 

cout << "The size of x is " << sizeof(x) << " bytes\n"; cout << "The value in x is " << x << endl;

 

}

 

The address of x is 0x8f05 The size of x is 2 bytes The value in x is 25 Pointer Variables

 

Pointer variables, which are often just called pointers, are designed to hold memory addresses. With pointer variables you can indirectly manipulate data stored in other variables Pointers are useful for the following:

 

Working with memory locations that regular variables don’t give you access to Working with strings and arrays

 

Creating new variables in memory while the program is running Creating arbitrarily-sized lists of values in memory

 

// This program stores the address of a variable in a pointer.

#include <iostream.h>

 

void main(void)

{

 

int x = 25; int *ptr;

 

ptr = &x; // Store the address of x in ptr cout << "The value in x is " << x << endl; cout << "The address of x is " << ptr << endl;

 

}

 

The value in x is 25

The address of x is 0x7e00

 

 

1. Pointer Arithmetic

 

Some mathematical operations may be performed on pointers.

 

The ++ and – operators may be used to increment or decrement a pointer variable. An integer may be added to or subtracted from a pointer variable. This may be performed with the +, - +=, or -= operators.

 

A pointer may be subtracted from another pointer.

//     This program uses a pointer to display the contents

//     of an integer array.

 

#include <iostream.h>

void main(void)

{

 

int set[8] = {5, 10, 15, 20, 25, 30, 35, 40};

 int *nums, index;

 

nums = set;

 

cout << "The numbers in set are:\n"; for (index = 0; index < 8; index++)

{

 

cout << *nums << " "; nums++;

 

}

 

cout << "\nThe numbers in set backwards are:\n";

for (index = 0; index < 8; index++)

 

{

nums--;

cout << *nums << " ";

}

}

 

Initializing Pointers

 

Pointers may be initialized with the address of an existing object.

//     This program uses a pointer to display the contents

//     of an integer array.

 

#include <iostream.h>

void main(void)

{

 

int set[8] = {5, 10, 15, 20, 25, 30, 35, 40};

int *nums = set; // Make nums point to set cout << "The numbers in set are:\n";

 

cout << *nums << " "; // Display first element

while (nums < &set[7])

 

{

nums++;

cout << *nums << " ";

}

 

 

cout << "\nThe numbers in set backwards are:\n";

cout << *nums << " "; // Display last element while (nums > set)

 

{

nums--;

cout << *nums << " ";

}

}

 

The numbers in set are: 5 10 15 20 25 30 35 40

 

The numbers in set backwards are: 40 35 30 25 20 15 10 5


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Object Oriented Programming(OOP) : Overview : Pointers - C++ |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.