0% found this document useful (0 votes)
2 views12 pages

Data Structure Lab1

The document provides an overview of pointers in data structures, explaining the concept of indirection where pointers hold memory addresses instead of direct values. It covers the declaration of pointers, the use of address and indirection operators, and illustrates how to use these concepts in programming. Additionally, it includes exercises for practical application of the discussed topics.

Uploaded by

wralyd6
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)
2 views12 pages

Data Structure Lab1

The document provides an overview of pointers in data structures, explaining the concept of indirection where pointers hold memory addresses instead of direct values. It covers the declaration of pointers, the use of address and indirection operators, and illustrates how to use these concepts in programming. Additionally, it includes exercises for practical application of the discussed topics.

Uploaded by

wralyd6
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

DATA STRUCTURE

LAB1: POINTERS

ENG. ABEER BALBAHAITH 1


INDIRECTION

 Pointer variables contain memory addresses as their values.


 Normally, a variable directly contains a specific value, A pointer contains the
memory address of a variable that in turn contains a specific value.
 a variable name directly references a value, and a pointer indirectly references a
value.
 Referencing a value through a pointer is called Indirection.

ENG. ABEER BALBAHAITH


2
DIRECT AND INDIRECT REFERENCING

count
count directly
7 reference a variable
that contains a value 7

countPtr count
Pointer countPtr
7 indirectly reference a
variable that contains
a value 7

Fig 1.1 

ENG. ABEER BALBAHAITH 3


DECLARING POINTERS

 Pointers like any variables must be declared before they can be used.
 Example: int countPtr ;
double *xPtr, *yPtr;

ENG. ABEER BALBAHAITH


4
POINTER OPERATORS

➢ ADDRESS (&) OPERATOR:

 The address operator (&) is a unary operator that obtains the memory address of
its operand.
 Example: int y = 5 ; //declare variable

int *yPtr = nullptr; //declare and initialize pointer


yPtr = &y; // assign address of y to pointer yPtr

ENG. ABEER BALBAHAITH


5
POINTER OPERATORS

➢ INDIRECTION (*) OPERATOR:

 The indirection or dereferencing operator (*) is a unary operator that returns the
value of the object to which it points .
 Example: cout << *yPtr <<endl ; //display the value of variable y indirectly which is 5

cout << y<<endl ; //display the value of variable y directly

*yPtr = 9; // assign 9 to variable y

cin >> *yPtr; // receive input value to y by dereferencing a pointer

ENG. ABEER BALBAHAITH


6
POINTER OPERATORS

REPRESENTING OF A POINTER POINTING TO A VARIABLE IN MEMORY:

yPtr y
600000 5

Location 500000 Location 600000

Fig 1.2 

ENG. ABEER BALBAHAITH


7
USING THE ADDRESS (&) AND INDIRECTION (*) OPERATORS

ENG. ABEER BALBAHAITH


8
PASS BY VALUE

ENG. ABEER BALBAHAITH


9
PASS BY REFERENCE WITH POINTERS

10
EXERCISE

 Create a function to calculate (log) base-10 of user input number using pass-by-
reference method, display the original value and the result.
 Create and array of 5 elements and display the addresses of all elements.

ENG. ABEER BALBAHAITH


11
REFERENCES

 Mark Weiss, (Data Structures & Algorithm Analysis in C++).


 Narasimha Karumanchi , (Data Structures and Algorithms Made Easy C).

ENG. ABEER BALBAHAITH


12

You might also like