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

Ch10 Pointers

The document provides a comprehensive overview of pointers in C++, explaining their significance, usage, and various operations such as pointer arithmetic, pointer to void, and the const modifier. It includes code examples demonstrating how to declare pointers, access variables through pointers, and manipulate strings using pointers. The content is aimed at enhancing understanding of object-oriented programming concepts related to memory management in C++.

Uploaded by

mamuneebg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views17 pages

Ch10 Pointers

The document provides a comprehensive overview of pointers in C++, explaining their significance, usage, and various operations such as pointer arithmetic, pointer to void, and the const modifier. It includes code examples demonstrating how to declare pointers, access variables through pointers, and manipulate strings using pointers. The content is aimed at enhancing understanding of object-oriented programming concepts related to memory management in C++.

Uploaded by

mamuneebg
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 17

Structure

Object
Oriented
Object
Programming
Video
Lecture

Pointers
Engr. Rashid Farid Chishti
chishti@iiu.edu.pk
https://youtube.com/rfchishti
https://sites.google.com/site/chishti
International Islamic University H-10, Islamabad,
Pakistan
http://www.iiu.edu.pk 1
Pointers
 Pointers are a special type of variables in which a
memory address is stored.
 They contain a memory address, not the value of the
variable.
 Pointers are an important and essential tool for
increasing the power of C++. A notable example is
the creation of data structures such as linked lists
and binary trees.
 In fact, several key features of C++, such as virtual
functions, the new operator, and the this pointer
require the use of pointers.
Address and Pointers
 The ideas behind pointers are not complicated. Here’s the first
key concept:
Every byte in the computer’s memory has an address.
 Addresses are numbers, just as they are for houses on a
street. The numbers start at 0 and go up from there 1, 2, 3,
and so on.
 If you have 1KB of memory, the highest address is 1023. (Of
course you have much more.)
 Your program, when it is loaded into memory, occupies a
certain range of these addresses.
 That means that every variable and every function in your
program starts at a Particular address. You can find the
address occupied by a variable by using the address-of
operator &.
The Address-of Operator &
#include <iostream>
#include <stdlib.h>
using namespace std; 0x6ffe1

33
int main(){ 4 var3

.3
int var1 = 11; // define and

4
char var2 = 'A'; // initialize
float var3 = 33.34; // three variables
cout << "Address of Var1 = "
0x6ffe1b ‘A’ var2
<< &var1 << endl; 0x6ffe1 11
cout <<"Address of Var2 = " c var1
<< static_cast<void *>(&var2)
<< endl;
cout <<"Address of Var3 = "
<< &var3 << endl;
system("PAUSE");
return 0;
}
Pointer Variable
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
int *ptr;
int var1 = 11;
int var2 = 22;
cout << "& ptr = " << &ptr << endl;
cout << "& var1 = " << &var1 << endl;
cout << "& var2 = " << &var2 << endl;
ptr = &var1; // pointer points
to var1
cout <<" ptr = " << ptr << endl; // print pointer
value
cout <<" *ptr = " << *ptr << endl; // print value of
var1
ptr = &var2; // pointer points
to var2
cout <<" ptr = " << ptr << endl; // print pointer
value
0x6ffe0 22 0x6ffe0 22

0 var2 0 var2

0x6ffe04 11 0x6ffe04 11
var1 var1

0x6ffe0 0x6ffe0
04

00
8 ptr 8 ptr
fe

fe
6f

6f
0x

0x
Pointer Variable
// other access using pointers
var1
#include <iostream>
#include <stdlib.h> 37
using namespace std;
int main() var2
{ 37
int var1, var2; // two integer variables
int* ptr; // pointer to integers
ptr = &var1; // set pointer to address of var1
*ptr = 37; // same as var1 = 37 ptr
var2 = *ptr; // same as var2 = var1
cout << "var1 = " << var1 << endl; // verify var2 is 37
cout << "var2 = " << var2 << endl; // verify var2 is 37
system("PAUSE");
return 0; Output
}
var1 = 37
var2 = 37
Press any key to continue...
Pointer to void
// pointers to type void ptr_int int_var
#include <iostream>
#include <stdlib.h>
ptr_flo flo_var
using namespace std;
int main(){
int int_var; // integer variable
float flo_var; // float variable ptr_void
int* ptr_int; // define pointer to int
float* ptr_flo; // define pointer to float
void* ptr_void; // define pointer to void
ptr_int = &int_var; // ok, int* to int*
// ptr_int = &flo_var; // error, float* to int*
// ptr_flo = &int_var; // error, int* to float*
ptr_flo = &flo_var; // ok, float* to float*
ptr_void = &int_var; // ok, int* to void*
ptr_void = &flo_var; // ok, float* to void*
system("PAUSE"); return 0; }
Pointers and Arrays
// array accessed with pointer notation
#include <iostream>
#include <stdlib.h>
using namespace std; int_array
int main() 31

*(int_array +
*(int_array +
{
int intarray[5] = 54
{ 31, 54, 77, 52, 93 };

for ( int j = 0 ; j < 5 ; j++ ) 77


// print value

3)
4)
cout << *(intarray+j) << endl; 52
system("PAUSE");
return 0; 93
}
Pointer Arithmetic
// Adding and Subtracting a pointer variable
#include <iostream> ptr_int my_array
#include <stdlib.h>
using namespace std; 31 [0]
int main(){
int my_array[] = 54 [1]
{ 31, 54, 77, 52, 93 };

int* ptr;
77 [2]

ptr = my_array; 52 [3]


// print at index 0;
cout << *ptr << " "; 93 [4]
ptr = ptr + 2;

// print at index 2; 31 77
cout << *ptr << " ";
Pointer Arithmetic

ptr++; ptr_int my_array


// print at index 3;
cout << *ptr << " "; 31 [0]
ptr = ptr-2; 54 [1]
// print at index 1;
cout << *ptr << " ";
77 [2]
ptr--;
// print at index 0; 52 [3]
cout << *ptr << endl;
93 [4]
system("PAUSE");
return 0;
}
31 77 52 54 31
Press any key to ..
Pointer and Functions
// arguments passed by pointer
#include <iostream>
#include <stdlib.h>
using namespace std;
void centimize(double*);
int main()
{
double length = 10.0; // var has value of 10 inches
cout << "Length = " << length << " inches" << endl;
centimize(&length); // change var to centimeters
cout << "Length = " << length << " centimeters" <<
endl;
system("PAUSE");
return 0;
}
void centimize(double* ptr) // store the address in
ptdr
{
*ptr = *ptr * 2.54; // *ptrd is the same as var
Pointer to String Constant
// strings defined using array and pointer notation str1
D
#include <iostream> D
str2 e
#include <stdlib.h> f e
using namespace std; i f
i
int main(){ n
e n
// str1 is an address, that is a pointer constant e
d
char str1[] = "Defined as an array" ; d
// while str2 is a pointer variable. it can be changed
a
char* str2 = "Defined as a pointer" ; s a
s
cout << str1 << endl; // display both strings
a
cout << str2 << endl; a
// str1++; // can’t do this; str1 is a constant p n
str2++; // this is OK, str2 is a pointer o
a
cout << str2 << endl; // now str2 starts “efined...”i
n r
system("PAUSE");
t r
return 0; a
e
} r y
\0 \0
String as Function Argument
// displays a string with pointer notation ps
#include <iostream> str
#include <stdlib.h> T
using namespace std; h
void dispstr(char*); // prototype i
s
int main()
{ i
char str[] = "This is amazing" ; s
dispstr(str); // display the string
system("PAUSE"); return 0; a
m
}
a
void dispstr(char* ps){ z
while( *ps ) // until null ('\0') character, i
cout << *ps++; // print characters n

cout << endl; g


\0
}
Copying a String Using Pointers
// copies one string to another with pointers
#include <iostream>
#include <stdlib.h>
using namespace std;
void copystr(char*, const char*); // function declaration
int main(){
char* str1 = "Self-conquest is the greatest victory." ;
char str2[80]; // empty string
copystr(str2, str1); // copy str1 to str2
cout << str2 << endl; // display str2
system("PAUSE"); return 0;
}
void copystr(char* dest, const char* src){
while( *src ){ // until null ('\0') character
*dest = *src ; // copy chars from src to dest
dest++; src++ ; // increment pointers
} *dest = '\0';} // copy null character
The const Modifier and Pointers
 The use of the const modifier with pointer declarations can be
confusing, because it can mean one of two things, depending on
where it’s placed. The following statements show the two
possibilities:
const int* cptrInt; // cptrInt is a pointer to constant int
int* const ptrcInt; // ptrcInt is a constant pointer to int
 In the first declaration, you cannot change the value of whatever
cptrInt points to, although you can change cptrInt itself.
 In the second declaration, you can change what ptrcInt points to,
but you cannot change the value of ptrcInt itself.
 You can remember the difference by reading from right to left, as
indicated in the comments.
The const Modifier and Pointers
#include <iostream>
#include <stdlib.h>
using namespace std;
int main(){
const int a = 5; int b = 10;
const int* cptrInt; // cptrInt is a pointer to constant
int
// ptrcInt is a constant pointer to int
int* const ptrcInt = &b;
cptrInt = &a;
// *cptrInt = 25; // can not change a constant value
*ptrcInt = 100;
cptrInt = &b;
// ptrcInt = &a; // can not change address of pointer

cout << "*ptrcInt = " << *ptrcInt << endl;


cout << "*cptrInt = " << *cptrInt << endl;
system("PAUSE"); return 0; }

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy