NIOS Computer Science: Chapter 15 – Pointer Part 1

Download PDF of This Page (Size: 250K)

Figure shows pointer

Figure Shows Pointer

Figure shows pointer

Introduction

In C++, the programming with pointers is more powerful and it is used extensively. It saves the processing time.

Pointer is a variable which holds the address of another variable. So, programming is concerned with the address, not with the direct value stored.

Objectives

After going through this lesson, you would be able to:

  • use pointers in arrays

  • define pointer variables in a structure and access data members through pointer

  • define pointer objects in a class and access members through pointer

Pointer

A pointer is a variable that represents the location (rather than the value) of a data item such as a variable or an array element.

Pointers are used frequently in C++, as they have a number of useful applications. Consider the following example:

# include < iostream.h >

void main ( )

{

int A = 5;

cout << & A;

int *ptr;

ptr = & A;

cout < < *ptr;

}

If variable A in the above example has a value 5, then & A holds the address of memory cell A. The variable which holds the address is called pointer variable.

int *ptr means that a variable ptr is a pointer to a memory cell which can hold the int data type.

*ptr is used for the value stored in a memory cell pointed to by the pointer ptr. It is called de-referencing the pointer.

The output of the above program is the address of memory cell A and value 5.

void *ptr;

Here ptr can point to any data type.

Image of point to any data type

Image of Point to Any Data Type

Image of point to any data type