Sie sind auf Seite 1von 13

Computer Programming

in
C++

(PRACTICAL#10)
Objective: Pointers in C++
 Introduction
 Memory is a collection of bytes. Every byte in the computer’s memory has an
address where the data/variables values are stored.
 These addresses are numbers. X= 10

 Example int x = 10; 0x22ff

Data Type variable’s name/ identifier value Memory Address


The Address of Operator &

 You can find the address occupied by a variable by using the address-of operator.
 The address of a variable can be obtained by preceding the name of a variable with an
ampersand sign (&), known as address-of operator. For example:
Example int var1 = 10;
Cout<<&var1 // print the address of var1
 Remember that the address of a variable is not the same as its
contents
 The 0x in the beginning represents the address is in hexadecimal form.
You can find the address occupied by a variable by using the address-of operator.
Example
int var1 = 10; Memory Representation
int var2=20; Addresses
cout<<&var1; var1 10 ff0
cout<<&var2; var2 20 ff2
Pointer Variables

 A pointer is a variable which stores the address of a variable.


 OR Variables that holds address values.
 OR A pointer provides a way of accessing a variable without referring to the variable
directly, the mechanism used for this is the address of the variable.
 We have integer (int) for storing whole numbers, char for storing a single character. etc..
Similarly pointers for storing address values.
 How to declare a pointer?

int* p; // The asterisk means pointer to and read as pointer to int


// this variables can hold the address of integer variable.
Pointer Variables

Examples

int* p; // pointer to int

char* ch; // pointer to char

float* p; // pointer to float


Advantages

 They increase execution speed.


 Obtaining memory from the system
 It allows management of structures which are allocated memory
dynamically.
 Pointers are more efficient in handling the data tables.
Advantages

 It allows passing of arrays and strings to functions more efficiently.

 It makes possible to pass address of structure instead of entire structure to


the functions.

 It makes possible to return more than one value from the function.
Reference operator (&) and Deference operator (*)

Reference operator (&) gives the address of a variable.

Referencing means taking the address of an existing variable (using &) to set a pointer
variable.

Example

int var1=10;

char ch=‘a’;

int* ptr1;

char* prt2;

ptr1=&var1; //pinter points to var1;

Pt2=&ch; //pinter points to var1;


Reference operator (&) and Deference
operator (*)

 To get the value stored in the memory address, we use the dereference operator (*).
 Dereferencing a pointer means using the * operator (asterisk character) to
access the value stored at a pointer:
 Dereferencing means access value of a variable pointed to by the pointer.

 For example: int var1=10;


 int* ptr1;
 ptr1=&var1 // set pointer to address var1;

 cout<<*ptr1; // print contents pointed to by the pointer
Pointer to void

 The address that you want to put in a pointer must be the same type as the pointer,
 You can’t assign the address of a float variable to a pointer to int. For example
 float f=10.25;
 Int* ptr=&f; // ERROR: can’t assign float * to int *
 Pointer to void is a general-purpose pointer that can point to any data type.

 Void* ptr=&f; //OK


reinterpret_cast

 You can assign one kind of pointer type to another by using reinterpret_cast.
 Example
 float f=10.25;
 Int* ptr;
 ptr=reinterpret_cast<int*>(f);
Tasks for Lab #
Task # 1

Take input in variable and display same value by pointer.

Task # 2
Write a program that asks the user to enter integers as inputs to be stored in the variables 'a' and 'b' respectively. There are
also two integer pointers named ptrA and ptrB. Assign the values of 'a' and 'b' to ptrA and ptrB respectively, and display
them.

Task # 3
Write a program that reads a group of numbers from the user and places them in an array of type int.
Once the numbers are stored in the array, the program should print them. Use pointer notation whenever possible.

Das könnte Ihnen auch gefallen