SlideShare a Scribd company logo
DATA STRUCTURES AND ALGORITHMS
LAB 5
Bianca Tesila
FILS, March 2014
OBJECTIVES
 Pointers
POINTERS: INTRODUCTION
 What is a pointer?
POINTERS: SHORT REMINDER FROM THE
LECTURES
 Memory space of a program=a sequence of bytes
 The bytes are numbered starting from 0: 0, 1, 2 …
 Every variable occupies a certain number of consecutive
bytes in memory
 An int occupies 4 bytes
 A double occupies 8 bytes
 A char occupies 1 byte
 The address of a variable = the index of the first byte
occupied in memory
 If an int variable occupies the bytes numbered 1000, 1001,
1002, 1003, the address of the variable is 1000
POINTERS: SHORT REMINDER FROM THE
LECTURES
 Every variable stores its value in the bytes it occupies in
the program’s memory
 A char stores its value in 1 byte
 An int stores its value as 4 consecutive bytes
 Pointer = special type of variable
 Its size = architecture-dependent (32 bits = 4 bytes in our case)
 Stores an address (the index of a byte in memory)
 The pointer points to the address it stores
 Multiple types of pointers
 int* p (pointer to int)
 char* p (pointer to char)
 int** p (pointer to pointer to int)
 double*** p
 ...
 Each type of pointer has 4 bytes and stores an address
 Differences:
 int x;
 int *p, char* q;
 p = q = &x;
 *p // refers to the int which starts at the address &x
 *q // refers to the char located at the address &x (the first byte of the 4-
byte int variable x)
POINTERS: SHORT REMINDER FROM THE
LECTURES
!!Example:
int x;
int *p;
char* q;
p = q = &x;
*p // refers to the int which starts at the address &x
*q // refers to the char located at the address &x (the first byte of the 4-byte int variable x)
POINTERS: OPERATORS
& (reference operator)
The address of a variable can be obtained by preceding the name
of a variable with an ampersand sign (&), known as reference
operator, and which can be literally translated as "address of".
myvar = 25;
foo = &myvar;
bar = myvar;
The variable that stores the address of another variable (like foo
in the previous example) is what in C++ is called a pointer.
POINTERS: OPERATORS
* (deference operator)
An interesting property of pointers is that they can be used to
access the variable they point to directly. This is done by
preceding the pointer name with the dereference operator (*).
The operator itself can be read as "value pointed to by".
baz = *foo;
This could be read as: "baz equal to value pointed to by foo",
and the statement would actually assign the value 25 to baz,
since foo is 1776, and the value pointed to by 1776 (following
the example above) would be 25.
POINTERS
 C++ has both pointers and references, whereas Java has only
references. C++ pointers are manipulated more like Java
references. You cannot manipulate (using assignment statements)
C++ references at all.
 The main difference between C++ pointers and references (in
Java or C++) is that the compiler manages references (knows you
want the contents, not the address) whereas you (the
programmer) must de-reference pointers to get at data. In other
words, the compiler automatically does the de-referencing when
it's a reference.
 To allocate memory, as in Java, use the keyword new . To
deallocate memory, use delete. There is no garbage collection so
all memory allocated by programmer must be deallocated by the
programmer. The compiler manages memory that it allocates,
e.g., int n; The compiler allocates memory for n and deallocates
its memory when it goes out of scope.
POINTERS
 For the beginning, let’s analyze PointersExample.cpp
!!Exercise:
Implement a function that reverses the elements of an array.
Hint: void invert(int *array, int arraySize)
An array variable contains the starting address of the array
(the array element of the first address) and a pointer that is
equal to the type of array elements.

More Related Content

PDF
Pointers
PDF
Pointer in c++ part1
PPTX
Pointers in C Language
PPTX
Pointer in c program
PPT
SPC Unit 3
PDF
Pointers in C
PPTX
Used of Pointer in C++ Programming
Pointers
Pointer in c++ part1
Pointers in C Language
Pointer in c program
SPC Unit 3
Pointers in C
Used of Pointer in C++ Programming

What's hot (20)

PPTX
Chp3(pointers ref)
PPTX
Presentation on pointer.
PPTX
Unit 8. Pointers
PPTX
Fundamentals of Pointers in C
PPT
Pointers (Pp Tminimizer)
PPT
C++ Pointers And References
PPT
PPT
Introduction to pointers and memory management in C
PPTX
Pointer in c
ODP
Pointers in c++ by minal
PPTX
Pointers in c - Mohammad Salman
PPTX
Pointers in C
PPTX
Dynamic Memory Allocation in C
PPTX
Getting started with C++
PPTX
Pointers in c++
PPTX
Pointers in C/C++ Programming
PDF
Types of pointer in C
PPTX
ppt on pointers
PPT
Pointers C programming
Chp3(pointers ref)
Presentation on pointer.
Unit 8. Pointers
Fundamentals of Pointers in C
Pointers (Pp Tminimizer)
C++ Pointers And References
Introduction to pointers and memory management in C
Pointer in c
Pointers in c++ by minal
Pointers in c - Mohammad Salman
Pointers in C
Dynamic Memory Allocation in C
Getting started with C++
Pointers in c++
Pointers in C/C++ Programming
Types of pointer in C
ppt on pointers
Pointers C programming
Ad

Viewers also liked (20)

PDF
DSA-2012-Lect00
PPTX
Data structures and algorithms lab3
PDF
Difference between c# generics and c++ templates
PPTX
Data structures and algorithms lab8
PPTX
Data structures and algorithms lab11
PPTX
Data structures and algorithms lab2
PPTX
Data structures and algorithms lab1
PDF
Java Day-2
PDF
Enum Report
PDF
Java Day-7
PPTX
Java 102 intro to object-oriented programming in java - exercises
PDF
Introduction to Agile
PPSX
data structure(tree operations)
PPTX
Java 101 Intro to Java Programming
PPTX
Java 201 Intro to Test Driven Development in Java
PDF
Java data structures for principled programmer
PPTX
Introduction to computer architecture and organization
PPTX
Java 102 intro to object-oriented programming in java
PPTX
Computer Programming Overview
PPTX
Java 103 intro to java data structures
DSA-2012-Lect00
Data structures and algorithms lab3
Difference between c# generics and c++ templates
Data structures and algorithms lab8
Data structures and algorithms lab11
Data structures and algorithms lab2
Data structures and algorithms lab1
Java Day-2
Enum Report
Java Day-7
Java 102 intro to object-oriented programming in java - exercises
Introduction to Agile
data structure(tree operations)
Java 101 Intro to Java Programming
Java 201 Intro to Test Driven Development in Java
Java data structures for principled programmer
Introduction to computer architecture and organization
Java 102 intro to object-oriented programming in java
Computer Programming Overview
Java 103 intro to java data structures
Ad

Similar to Data structures and algorithms lab5 (20)

PDF
C++ Pointers , Basic to advanced Concept
PDF
Pointers
PPTX
Pointers in c language
PPTX
Pointers in c v5 12102017 1
PPTX
PDF
PSPC--UNIT-5.pdf
PPTX
PDF
PDF
Embedded C The IoT Academy
PPTX
pointers.pptx
PPTX
Pointers in c++
PPT
Lect 9(pointers) Zaheer Abbas
PPT
Lect 8(pointers) Zaheer Abbas
DOCX
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx
PPTX
oop lecture 2
PDF
CS-XII Python Fundamentals.pdf
PPT
13092119343434343432232323121211213435554
PPTX
FYBSC(CS)_UNIT-1_Pointers in C.pptx
PPTX
COM1407: Working with Pointers
C++ Pointers , Basic to advanced Concept
Pointers
Pointers in c language
Pointers in c v5 12102017 1
PSPC--UNIT-5.pdf
Embedded C The IoT Academy
pointers.pptx
Pointers in c++
Lect 9(pointers) Zaheer Abbas
Lect 8(pointers) Zaheer Abbas
CS 360 LAB 3 STRINGS, FUNCTIONS, AND METHODSObjective The purpos.docx
oop lecture 2
CS-XII Python Fundamentals.pdf
13092119343434343432232323121211213435554
FYBSC(CS)_UNIT-1_Pointers in C.pptx
COM1407: Working with Pointers

More from Bianca Teşilă (6)

PDF
Akka Streams - An Adobe data-intensive story
PPTX
Data structures and algorithms lab10
PPTX
Data structures and algorithms lab9
PPTX
Data structures and algorithms lab7
PPTX
Data structures and algorithms lab6
PPTX
Data structures and algorithms lab4
Akka Streams - An Adobe data-intensive story
Data structures and algorithms lab10
Data structures and algorithms lab9
Data structures and algorithms lab7
Data structures and algorithms lab6
Data structures and algorithms lab4

Data structures and algorithms lab5

  • 1. DATA STRUCTURES AND ALGORITHMS LAB 5 Bianca Tesila FILS, March 2014
  • 4. POINTERS: SHORT REMINDER FROM THE LECTURES  Memory space of a program=a sequence of bytes  The bytes are numbered starting from 0: 0, 1, 2 …  Every variable occupies a certain number of consecutive bytes in memory  An int occupies 4 bytes  A double occupies 8 bytes  A char occupies 1 byte  The address of a variable = the index of the first byte occupied in memory  If an int variable occupies the bytes numbered 1000, 1001, 1002, 1003, the address of the variable is 1000
  • 5. POINTERS: SHORT REMINDER FROM THE LECTURES  Every variable stores its value in the bytes it occupies in the program’s memory  A char stores its value in 1 byte  An int stores its value as 4 consecutive bytes  Pointer = special type of variable  Its size = architecture-dependent (32 bits = 4 bytes in our case)  Stores an address (the index of a byte in memory)  The pointer points to the address it stores  Multiple types of pointers  int* p (pointer to int)  char* p (pointer to char)  int** p (pointer to pointer to int)  double*** p  ...  Each type of pointer has 4 bytes and stores an address  Differences:  int x;  int *p, char* q;  p = q = &x;  *p // refers to the int which starts at the address &x  *q // refers to the char located at the address &x (the first byte of the 4- byte int variable x)
  • 6. POINTERS: SHORT REMINDER FROM THE LECTURES !!Example: int x; int *p; char* q; p = q = &x; *p // refers to the int which starts at the address &x *q // refers to the char located at the address &x (the first byte of the 4-byte int variable x)
  • 7. POINTERS: OPERATORS & (reference operator) The address of a variable can be obtained by preceding the name of a variable with an ampersand sign (&), known as reference operator, and which can be literally translated as "address of". myvar = 25; foo = &myvar; bar = myvar; The variable that stores the address of another variable (like foo in the previous example) is what in C++ is called a pointer.
  • 8. POINTERS: OPERATORS * (deference operator) An interesting property of pointers is that they can be used to access the variable they point to directly. This is done by preceding the pointer name with the dereference operator (*). The operator itself can be read as "value pointed to by". baz = *foo; This could be read as: "baz equal to value pointed to by foo", and the statement would actually assign the value 25 to baz, since foo is 1776, and the value pointed to by 1776 (following the example above) would be 25.
  • 9. POINTERS  C++ has both pointers and references, whereas Java has only references. C++ pointers are manipulated more like Java references. You cannot manipulate (using assignment statements) C++ references at all.  The main difference between C++ pointers and references (in Java or C++) is that the compiler manages references (knows you want the contents, not the address) whereas you (the programmer) must de-reference pointers to get at data. In other words, the compiler automatically does the de-referencing when it's a reference.  To allocate memory, as in Java, use the keyword new . To deallocate memory, use delete. There is no garbage collection so all memory allocated by programmer must be deallocated by the programmer. The compiler manages memory that it allocates, e.g., int n; The compiler allocates memory for n and deallocates its memory when it goes out of scope.
  • 10. POINTERS  For the beginning, let’s analyze PointersExample.cpp !!Exercise: Implement a function that reverses the elements of an array. Hint: void invert(int *array, int arraySize) An array variable contains the starting address of the array (the array element of the first address) and a pointer that is equal to the type of array elements.