SlideShare ist ein Scribd-Unternehmen logo
1 von 145
Downloaden Sie, um offline zu lesen
C++ 
Programming Language 
L06-POINTERS 
Mohammad Shaker 
mohammadshaker.com 
@ZGTRShaker 
2010, 11, 12, 13, 14
Float, double, long double 
C++ data types 
Structured 
Simple 
Address 
Pointer 
Reference 
enum 
Floating 
Array 
Struct 
Union 
Class 
Char, Short, int, long, bool 
Integral
Pointers
Pointerspowerful, difficult
PointersVery powerful, Not so difficult
Pointers 
•Powerful feature in C++, but one of the most difficult to master 
•Using Dynamic objects 
–Can survive after the function ends in which they were allocated 
–Allow flexible-sized arrays and lists 
–Lists, Trees, stacks, Queues 
–Managing big sized, large objects throw passing them into functions 
•Note: 
–Can declare pointer to any data type 
•In general the type of the pointer must match the type of the data it’s set to point to
Pointers 
•Pointer variable contain an “address” rather than a data “value” 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
int*ptr; // initialize a pointer to an integer object 
} 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
float*ptr; // initialize a pointer to an float object 
}
Pointers 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
int* ptr; // initialize a pointer to an integer object 
} 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
int*ptr; // initialize a pointer to an integer object 
} 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
int * ptr; // initialize a pointer to an integer object 
}
Pointers 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
int*ptr1, *ptr2; // ptr1, ptr2 are pointers 
} 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
int*ptr1, p; 
// ptr1: pointer to int 
// p: int 
}
Pointers 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
int*ptr1, p; 
// ptr1: pointer to int 
// p: int 
} 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
int* ptr1, p; 
// ptr1: pointer to int 
// p: int 
}
Pointers 
#include<iostream> 
usingnamespace::std; 
typedefint* IntPtr; 
voidmain(void) 
{ 
IntPtr Ptr1,Ptr2; // now, Ptr1,Ptr2 both are pointers to integer 
} 
int*ptr, c;// ptr: pointer, c: integer 
int* ptr,c;// ptr: pointer, c: integer 
int(* ptr),c;// ptr: pointer, c: integer 
intc, *ptr;// ptr: pointer, c: integer 
(int*) ptr,c;// compiler error 
int* ptr;// ptr: pointer 
int* ptr;// ptr: pointer 
int*ptr;// ptr: pointer
Pointers 
c2is not pointing to anything, because it hasn’t been initialized, thus called, “null” pointer 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
intc1=3, *c2; // c1: integer, c2: pointer to int 
} 
3 
c1 
c2
Pointers 
•“ The address of ” operator 
–& 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
intc1; 
c1 = 3; 
cout << "The value of variable c1 = "<< c1 << endl; 
cout << "The address of variable c1 in memory = "<< &c1 << endl; 
} 
The value of variable c1 = 3 
The address of variable c1 in memory = 0024EC64 
Press any key to continue
Pointers 
•A pointer is also a “variable” 
–So it also has its own memory address 
100 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 100 ; 
int*ptr = new int; 
*ptr = i ; 
} 
memory 
1024
100 
i 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
int i = 100 ; 
int*ptr = new int; 
*ptr = i ; 
} 
A pointer is also a “variable” 
So it also has its own memory address 
memory 
1024 
Pointers
100 
1424 
i 
ptr 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 100 ; 
int *ptr = new int; 
*ptr = i ; 
} 
A pointer is also a “variable” 
So it also has its own memory address 
memory 
1024 
1064 
1424 
Pointers
100 
1424 
100 
i 
ptr 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
int i = 100 ; 
int *ptr = new int; 
*ptr = i ; 
} 
A pointer is also a “variable” 
So it also has its own memory address 
memory 
1024 
1064 
1424 
Pointers
200 
i 
ptr 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 200 ; 
int*ptr ; 
*ptr = 200 ; 
} 
Runtime Error 
memory 
1024 
1064 
Pointers
100 
1024 
i 
ptr 
memory 
1024 
1064 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 100 ; 
int*ptr ; 
ptr = &i ; 
} 
The “pointer” now has the “address” of the “variable” 
Pointers
100 
1024 
i 
ptr 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 100 ; 
int*ptr = &i ; 
// initialize at definition time 
} 
memory 
1024 
1064 
Pointers
Pointers 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 100; 
int* ptr; 
ptr = &i; 
} 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 100; 
int* ptr = &i; 
} 
As we know,they are all the same! 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
inti=100, * ptr = &i; 
} 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 100, * ptr; 
ptr = &i; 
}
100 
1024 
i 
ptr 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 100 ; 
int*ptr = &i ; 
} 
memory 
1024 
1064 
Pointers
Code Cracking -Pointers 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 100; 
int* ptr; 
ptr = &i; 
cout << i << " "<< &i << endl; 
cout << ptr << " "<< &ptr << endl; 
} 
100 0020F180 
0020F180 0020F184 
Press any key to continue 
When printing the pointer “ptr” all by itself, the output will be the address of the variable in which the pointer “ptr” points to
memory 
002AF360 
002AF364 
100 
002AF360 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 100 ; 
int* ptr ; 
ptr = &i ; 
cout << "the address of which the pointer points to ="<< ptr << endl ; 
cout<< "the address of the variable =" << &i<< endl; 
cout << "the address of the pointer ="<< &ptr << endl ; 
cout << "the value of the variable ="<< i << endl ; 
cout << "the value of the variable of which the pointer points to ="<< *ptr << endl; 
} 
the address of which the pointer points to =002AF360 
the address of the variable =002AF360 
the address of the pointer =002AF364 
the value of the variable =100 
the value of the variable of which the pointer points to =100 
Press any key to continue . . . 
i 
ptr
nullPointers
Pointers 
#include<iostream> 
#include<stdlib.h> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 100; 
int* ptr; 
ptr = &i; 
if(ptr == NULL) 
{ 
cout << "I'm NULL "<< endl; 
} 
else 
{ 
cout << "I'm not a NULL! "<< endl; 
} 
} 
I'm not a NULL! 
Press any key to continue
Pointers 
#include<iostream> 
#include<stdlib.h> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 100; 
int* ptr; 
if(ptr == NULL) 
{ 
cout << "I'm NULL "<< endl; 
} 
else 
{ 
cout << "I'm not a NULL! "<< endl; 
} 
} 
I'm NULL 
Press any key to continue 
The pointer hasn’t been initialize yet so it’s usuallya NULL
Pointers 
#include<iostream> 
#include<stdlib.h> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 100; 
intj = 123; 
int* ptr; 
ptr = &i; 
ptr = &j; // dereferencing the pointer 
cout << &i << endl; 
cout << &j << endl; 
cout << ptr << endl; 
} 
001DF2BC 
001DF2C0 
001DF2C0 
Press any key to continue
Pointers 
#include<iostream> 
#include<stdlib.h> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 100; 
intj = 123; 
int* ptr; 
ptr = &i; 
ptr = &j; 
cout << &i << endl; 
cout << &j << endl; 
cout << ptr << endl; 
cout << &ptr << endl; 
} 
0031EBBC 
0031EBC0 
0031EBC0 
0031EBC4 
Press any key to continue
Pointers 
#include<iostream> 
#include<stdlib.h> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 100; 
int* ptr = &i; 
ptr = NULL; 
if(ptr == NULL) 
{ 
cout << "I'm NULL "<< endl; 
} 
else 
{ 
cout << "I'm not a NULL! "<< endl; 
} 
cout << ptr << endl; 
cout << &ptr << endl; 
} 
I'm NULL 
00000000 
001FF284 
Press any key to continue
Pointers 
#include<iostream> 
#include<stdlib.h> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 100; 
int* ptr; 
cout << *ptr << endl; 
} 
Runtime error 
It’s a run time error to print the value of a ptr that hasn’t has been initialized yet
Pointers 
#include<iostream> 
#include<stdlib.h> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 100; 
int* ptr = &i; 
ptr = NULL; 
if(ptr == NULL) 
{ 
cout << "I'm NULL "<< endl; 
} 
else 
{ 
cout << "I'm not a NULL! "<< endl; 
} 
cout << *ptr << endl; 
} 
Runtime error 
It’s a runtime error to print the value of a NULL ptr
Pointers 
#include<iostream> 
#include<stdlib.h> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 100; 
int* ptr = NULL; 
if(ptr == NULL) 
{ 
cout << "I'm NULL "<< endl; 
} 
else 
{ 
cout << "I'm not a NULL! "<< endl; 
} 
} 
I'm NULL 
Press any key to continue
Pointers 
#include<iostream> 
#include<stdlib.h> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 100; 
int* ptr; 
if(ptr = NULL) 
{ 
cout << "I'm NULL "<< endl; 
} 
else 
{ 
cout << "I'm not a NULL! "<< endl; 
} 
} 
I'm not a NULL! 
Press any key to continue 
Why?
Pointers 
#include<iostream> 
#include<stdlib.h> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 100; 
int* ptr = NULL; 
if(ptr = NULL) 
{ 
cout << "I'm NULL "<< endl; 
} 
else 
{ 
cout << "I'm not a NULL! "<< endl; 
} 
} 
I'm not a NULL! 
Press any key to continue 
Why?
Pointers 
#include<iostream> 
#include<stdlib.h> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 100; 
int* ptr = NULL; 
if(ptr = NULL) 
{ 
cout << "I'm NULL "<< endl; 
} 
else 
{ 
cout << "I'm not a NULL! "<< endl; 
} 
} 
I'm not a NULL! 
Press any key to continue 
Why?
Pointers 
#include<iostream> 
#include<stdlib.h> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 100; 
int* ptr = NULL; 
if(ptr = NULL) 
{ 
cout << "I'm NULL "<< endl; 
} 
else 
{ 
cout << "I'm not a NULL! "<< endl; 
} 
} 
I'm not a NULL! 
Press any key to continue 
Why?
Pointers 
#include<iostream> 
#include<stdlib.h> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 100; 
int* ptr = NULL; 
if(ptr = NULL) 
{ 
cout << "I'm NULL "<< endl; 
} 
else 
{ 
cout << "I'm not a NULL! "<< endl; 
} 
} 
I'm not a NULL! 
Press any key to continue 
Why?
Pointers 
#include<iostream> 
#include<stdlib.h> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 100; 
int* ptr = &i; 
*ptr = 300; 
cout << i << endl; 
cout << *ptr << endl; 
} 
#include<iostream> 
#include<stdlib.h> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 100; 
int* ptr =&i; 
i = 400; 
cout << i << endl; 
cout << *ptr << endl; 
} 
300 
300 
400 
400
Pointers, Rules of Usage
Pointers 
•Rules for using pointers: 
–If the pointer is initialized 
•#1: make sure the value stored in pointer is a valid address before using it 
•#2: the type is the same between the address’s item and the pointer 
–If the pointer is not initialized 
•You can’t use “*” then.
Pointers 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
doublei; 
double*ptr; 
ptr =&i; 
} 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
doublei; 
double*ptr; 
*ptr = 20; 
} 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
doublei; 
int *ptr; 
ptr =&i; 
} 
voidmain(void) 
{ 
doublei; 
double*ptr= &i; 
if(!ptr) 
{ 
cout << “That's a bad address "<< endl; 
cout << "The address is "<< &ptr << endl; 
} 
else 
{ 
cout << "That's a good address "<< endl; 
cout << "The address is "<< &ptr << endl; 
} 
} 
Compiler error, the pointer is on different type from the variable it’s pointing to 
That's a good address 
The address is 002DECE4 
Compile & run 
Runtime error 
!ptr used for testing invalid address for ptr
Pointers 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
doublei; 
double*ptr= NULL; 
if(!ptr) 
{ 
cout << “That's a bad address "<< endl; 
cout << "The address is "<< &ptr << endl; 
} 
else 
{ 
cout << "That's a good address "<< endl; 
cout << "The address is "<< &ptr << endl; 
} 
} 
That's a bad address 
The address is 0020F0F4 
Press any key to continue 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
doublei; 
double*ptr; 
if(!ptr) 
{ 
cout << “That's a bad address "<< endl; 
cout << "The address is "<< &ptr << endl; 
} 
else 
{ 
cout << "That's a good address "<< endl; 
cout << "The address is "<< &ptr << endl; 
} 
} 
That's a good address 
The address is 0018EF74 
Press any key to continue
Pointers 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
doublei; 
double*ptr; 
if(!ptr) 
{ 
cout << “That's a bad address "<< endl; 
} 
else 
{ 
cout << "That's a good address "<< endl; 
cout << "The address is "<< &ptr << endl; 
} 
} 
that's a bad address 
Press any key to continue 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
floatx1 = 3; 
floatx2 = 4; 
float*ptr1, *ptr2; 
ptr1 = &x1; 
ptr2 = &x2; 
cout << “Code_1"<< endl; 
cout << x1 << " -"<< *ptr1 << endl; 
cout << x2 << " -"<< *ptr2 << endl; 
x1 = x1+2; 
x2 = *ptr1 * x2; 
cout << "Code_2"<< endl; 
cout << x1 << " -"<< *ptr1 << endl; 
cout << x2 << " -"<< *ptr2 << endl; 
} 
Code_1 
3 -3 
4 -4 
Code_2 
5 -5 
20 –20
Pointers 
p1 = i1;// *int!= int, not correct 
p1 = & i1;// *int = *int, correct 
p2 = & i2;// *int = *int, correct 
*p1 = i2 * 2;// int = int, correct 
*p2 = *p1 + 50;// int = int, correct 
cout << *p2 << endl;
Pointers 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
inti1=100, i2=300; 
int*p1,*p2; 
p1 = & i1;// *int = *int, correct 
p2 = & i2;// *int = *int, correct 
*p1 = i2 * 2; // int = int, correct 
*p2 = *p1 + 50; // int = int, correct 
cout << *p2 << endl; 
} 
650
Pointers 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
chari1= 'c', i2= 's'; 
char*p1,*p2; 
p2 = &i1; 
i1 = 'd'; 
cout << *p2 << endl; 
} 
d 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
chari1= 'c', i2= 's'; 
char*p1,*p2; 
p2 = &i1; 
i1 = 'd'; 
p1 = &i2; 
p2 = p1; 
cout << *p2 << endl; 
} 
s
Pointers 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
chari1= 'c', i2= 's'; 
char*p1,*p2; 
p2 = &i1; 
i1 = 'd'; 
p1 = &i2; 
p2 = p1; 
i2 = 'k'; 
cout << *p2 << endl; 
cout << *p1 << endl; 
} 
k 
k 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
chari1= 'c', i2= 'j'; 
char*p1 = &i1, *p2 = &i2; 
p1 = p2; // not the same as *p1 = *p2 
cout << p2 << endl; 
cout << p1 << endl; 
cout << *p1 << endl; 
cout << *p2 << endl; 
cout << "i1 = "<< i1 << endl; 
cout << "i2 = "<< i2 << endl; 
} 
j 
j 
j 
j 
i1 = c 
i2 = j 
Press any key to continue
Pointers 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
chari1= 'c', i2= 'j'; 
char*p1 = &i1, *p2 = &i2; 
*p1 = *p2;// not the same as p1 = p2 
cout << p2 << endl; 
cout << p1 << endl; 
cout << *p1 << endl; 
cout << *p2 << endl; 
cout << "i1 = "<< i1 << endl; 
cout << "i2 = "<< i2 << endl; 
} 
j 
j 
j 
j 
i1 = j 
i2 = j 
Press any key to continue
Pointers
Pointers 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
charstr1 [] = "GoGo"; 
char*str2 = "GoGo"; 
} 
G 
o 
G 
o 
0 
G 
o 
G 
o 
0 
str1 
str2
Pointers 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
charstr1 [] = "GoGo1"; 
char*str2 = "GoGo2"; 
if(str1 == str2) 
{ 
cout << "The same. "<< endl; 
} 
else 
{ 
cout << "Not the same. "<< endl; 
} 
cout << str1 << endl; 
cout << str2 << endl; 
} 
Not the same. 
GoGo1 
GoGo2 
Press any key to continue 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
charstr1 [] = "GoGo1"; 
char*str2 = "GoGo2"; 
if(str1 = str2) 
{ 
cout << "The same. "<< endl; 
} 
else 
{ 
cout << "Not the same. "<< endl; 
} 
cout << str1 << endl; 
cout << str2 << endl; 
} 
Can’t convert from char* to char[5]
Pointers 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
char*str1 = "GoGo1"; 
char*str2 = "GoGo2"; 
if(str1 == str2) 
{ 
cout << "The same. "<< endl; 
} 
else 
{ 
cout << "Not the same. "<< endl; 
} 
cout << str1 << endl; 
cout << str2 << endl; 
} 
Not the same. 
GoGo1 
GoGo2 
Press any key to continue 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
char*str1 = "GoGo1"; 
char*str2 = "GoGo2"; 
if(str1 = str2) 
{ 
cout << "The same. "<< endl; 
} 
else 
{ 
cout << "Not the same. "<< endl; 
} 
cout << str1 << endl; 
cout << str2 << endl; 
} 
The same. 
GoGo2 
GoGo2 
Press any key to continue
Pointers 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
charstr1[] = "GoGo1"; 
charstr2[] = "GoGo2"; 
if(str1 == str2) 
{ 
cout << "The same. "<< endl; 
} 
else 
{ 
cout << "Not the same. "<< endl; 
} 
cout << str1 << endl; 
cout << str2 << endl; 
} 
Not the same. 
GoGo1 
GoGo2 
Press any key to continue 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
charstr1[] = "GoGo1"; 
charstr2[] = "GoGo2"; 
str1 = str2; 
if(str1 == str2) 
{ 
cout << "The same. "<< endl; 
} 
else 
{ 
cout << "Not the same. "<< endl; 
} 
cout << str1 << endl; 
cout << str2 << endl; 
} 
Compiler error. Assigning memory locations (can’t do that for arrays! You can only dereference pointers!).
Pointers 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
charstr1[] = "GoGo1"; 
charstr2[] = "GoGo2"; 
if(str1 = str2) 
{ 
cout << "The same. "<< endl; 
} 
else 
{ 
cout << "Not the same. "<< endl; 
} 
cout << str1 << endl; 
cout << str2 << endl; 
} 
Compiler error 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
charstr1[] = "GoGo1"; 
charstr2[] = "GoGo2"; 
str1 = "sdsd"; 
if(str1 == str2) 
{ 
cout << "The same. "<< endl; 
} 
else 
{ 
cout << "Not the same. "<< endl; 
} 
cout << str1 << endl; 
cout << str2 << endl; 
} 
Compiler error
References
References 
•Alias 
–What does that means? 
•Creating another name for the variable, so that the Alias refers to the same memory address as does the original variable 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
floatfoo; 
float&bar = foo; // must be initialized at definition time 
} 
Must be initialized at definition time
References 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
floatfoo; 
float&bar; 
bar = &foo; 
} 
Compiler error. Must be initialized when declared 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
floatfoo; 
float&bar = foo; 
foo = 33; 
cout << bar << endl; 
cout << foo << endl; 
} 
33 
33
References 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
floatfoo; 
float&bar = foo; 
cout << bar << endl; 
cout << foo << endl; 
} 
7.06997e+018 
7.06997e+018 
Press any key to continue 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
floatfoo; 
float&bar = foo; 
floati =3; 
bar = i; 
cout << bar << endl; 
cout << foo << endl; 
} 
3 
3
References 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
floatfoo; 
float&bar = foo; 
floati =3; 
bar = &i; 
cout << bar << endl; 
cout << foo << endl; 
} 
Compiler error 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
floatfoo; 
int&bar = foo; 
cout << bar << endl; 
cout << foo << endl; 
} 
Compiler error, types differ
Passing Parameters to Functions-Very Important -
Passing Parameters to Functions 
•By value 
•By reference 
–with reference parameters 
–with pointer parameters
Passing Parameters to Functions 
#include<iostream> 
usingnamespace::std; 
voidBadSwap (inta, intb) 
{ 
inttemp; 
temp = a; 
a = b; 
b = temp; 
} 
voidmain(void) 
{ 
inta = 2; 
intb = 3; 
cout << "Before Bad Swap:"<< endl; 
cout << "a is = "<< a << endl; 
cout << "b is = "<< b << endl; 
BadSwap(a,b); 
cout << "After Bad Swap:"<< endl; 
cout << "a becomes = "<< a << endl; 
cout << "b becomes = "<< b << endl; 
} 
Before Bad Swap: 
a is = 2 
b is = 3 
After Bad Swap: 
a becomes = 2 
b becomes = 3 
#include<iostream> 
usingnamespace::std; 
voidSwap (int&a, int&b) 
{ 
inttemp; 
temp = a; 
a = b; 
b = temp; 
} 
voidmain(void) 
{ 
inta = 2; 
intb = 3; 
cout << "Before Swap:"<< endl; 
cout << "a is = "<< a << endl; 
cout << "b is = "<< b << endl; 
Swap(a,b); 
cout << "After Swap:"<< endl; 
cout << "a becomes = "<< a << endl; 
cout << "b becomes = "<< b << endl; 
} 
Before Swap: 
a is = 2 
b is = 3 
After Swap: 
a becomes = 3 
b becomes = 2 
Passing by value 
Passing by reference
Passing Parameters to Functions 
#include<iostream> 
usingnamespace::std; 
voidSwap (int*a, int*b) 
{ 
inttemp; 
temp = *a; 
*a = *b; 
*b = temp; 
} 
voidmain(void) 
{ 
int*a; 
int*b; 
*a = 3; 
*b = 5; 
Swap(a,b); 
cout << *a << endl; 
cout << *b << endl; 
} 
Compile but runtimer error. The is because the pointers hasn’t been initialized (without new) 
#include<iostream> 
usingnamespace::std; 
voidSwap (int*a, int*b) 
{ 
inttemp; 
temp = *a; 
a = b; 
*b = temp; 
} 
voidmain(void) 
{ 
int*a = newint; 
int*b = newint; 
*a = 3; 
*b = 5; 
Swap(a,b); 
cout << *a << endl; 
cout << *b << endl; 
} 
3 
3
Passing Parameters to Functions 
#include<iostream> 
usingnamespace::std; 
voidSwap (int*a, int*b) 
{ 
int*temp = newint; 
*temp = *a; 
*a = *b; 
*b = *temp; 
} 
voidmain(void) 
{ 
inta = 2; 
intb = 3; 
cout << "Before Swap:"<< endl; 
cout << "a is = "<< a << endl; 
cout << "b is = "<< b << endl; 
Swap(&a,&b); 
cout << "After Swap:"<< endl; 
cout << "a becomes = "<< a << endl; 
cout << "b becomes = "<< b << endl; 
} 
Before Swap: 
a is = 2 
b is = 3 
After Swap: 
a becomes = 3 
b becomes = 2 
#include<iostream> 
usingnamespace::std; 
voidSwap (int*a, int*b) 
{ 
int*temp = newint; 
*temp = *a; 
*a = *b; 
*b = *temp; 
} 
voidmain(void) 
{ 
inta = 2; 
intb = 3; 
cout << "Before Bad Swap:"<< endl; 
cout << "a is = "<< a << endl; 
cout << "b is = "<< b << endl; 
Swap(a,b); 
cout << "After Bad Swap:"<< endl; 
cout << "a becomes = "<< a << endl; 
cout << "b becomes = "<< b << endl; 
} 
Compiler error. Can’t convert int to *int
Passing Parameters to Functions 
#include<iostream> 
usingnamespace::std; 
voidSwap (int*a, int*b) 
{ 
int*temp = newint; 
temp = a; 
a = b; 
b = temp; 
} 
voidmain(void) 
{ 
inta = 2; 
intb = 3; 
cout << "Before Bad Swap:"<< endl; 
cout << "a is = "<< a << endl; 
cout << "b is = "<< b << endl; 
Swap(&a,&b); 
cout << "After Bad Swap:"<< endl; 
cout << "a becomes = "<< a << endl; 
cout << "b becomes = "<< b << endl; 
} 
Before Bad Swap: 
a is = 2 
b is = 3 
After Bad Swap: 
a becomes = 2 
b becomes = 3 
Coz the pointers are returned to their original pointing locations after the method is done!
Passing Parameters to Functions 
#include<iostream> 
usingnamespace::std; 
voidSwap (int*a, int*b) 
{ 
inttemp; 
temp = *a; 
*a = *b; 
*b = temp; 
} 
voidmain(void) 
{ 
int*a = newint; 
int*b = newint; 
*a = 3; 
*b = 5; 
cout << "Before Swap:"<< endl; 
cout << "a is = "<< *a << endl; 
cout << "b is = "<< *b << endl; 
Swap(a,b); 
cout << "After Swap:"<< endl; 
cout << "a becomes = "<< *a << endl; 
cout << "b becomes = "<< *b << endl; 
} 
Before Swap: 
a is = 3 
b is = 5 
After Swap: 
a becomes = 5 
b becomes = 3 
#include<iostream> 
usingnamespace::std; 
voidSwap (int*a, int*b) 
{ 
inttemp; 
temp = *a; 
*a = *b; 
*b = temp; 
} 
voidmain(void) 
{ 
int*a = newint; 
int*b = newint; 
*a = 3; 
*b = 5; 
Swap(&a,&b); 
cout << *a << endl; 
cout << *b << endl; 
} 
Compiler error Can’t convert **int to *int
Passing Parameters to Functions 
#include<iostream> 
usingnamespace::std; 
int* f () 
{ 
intc = 2; 
cout << "the address of c in f fuction "<< &c << endl; 
return(&c); 
} 
voidWrong() 
{ 
int* p; 
cout << "The address of the p before calling f is = "<< p << endl; 
p = f(); 
cout << "The address of the p after calling f is = "<< p << endl; 
cout << "The value of the p is now!!! = "<< *p << endl; 
} 
voidmain(void) 
{Wrong();} 
The address of the p before calling f is = 00000000 
the address of c in f fuction 0014F190 
The address of the p after calling f is = 0014F190 
The value of the p is now!!! = 1372776 
Press any key to continue 
Function must not return a pointerto a localvariable in the function
Passing Parameters to Functions 
#include<iostream> 
usingnamespace::std; 
intf () 
{ 
intc = 2; 
cout << "the address of c in f fuction "<< &c << endl; 
return(c); 
} 
voidWrong() 
{ 
int* p = newint; 
cout << "The address of the p before calling f is = "<< p << endl; 
*p = f(); 
cout << "The address of the p after calling f is = "<< p << endl; 
cout << "The value of the p is now!!! = "<< *p << endl; 
} 
voidmain(void) 
{Wrong();} 
The address of the p before calling f is = 00311DE0 
the address of c in f fuction 0028EEF8 
The address of the p after calling f is = 00311DE0 
The value of the p is now!!! = 2 
Press any key to continue
Passing Parameters to Functions 
voidswap1 (intx, inty) 
{ 
inttemp = x; 
x = y; 
y = temp;} 
voidswap2 (int*x, int*y) 
{ 
inttemp = *x; 
*x = *y; 
*y = temp;} 
voidswap3 (int&x, int&y ) 
{ 
inttemp = x; 
x = y; 
y = temp; } 
voidmain(void) 
{ 
inti1 = 5; 
inti2 = 7; 
//How should we call 
//the function 
} 
void main(void) 
{ 
inti1 = 5; 
inti2 = 7; 
swap1(i1,i2); 
swap2(&i1,&i2); 
swap3(i1,i2); 
} 
#include<iostream> 
usingnamespace::std; 
voidswap1 (intx, inty){ 
inttemp = x; 
x = y; 
y = temp;} 
voidswap2 (int*x, int*y){ 
inttemp = *x; 
*x = *y; 
*y = temp;} 
voidswap3 (int&x, int&y ) 
{ 
inttemp = x; x = y;y = temp; } 
voidmain(void) 
{inti1 = 5, i2 = 7; 
swap1(i1,i2); 
cout << "After 1st sawp function "<< endl; 
cout << "i1 = "<< i1 <<endl; 
cout << "i2 = "<< i2 << endl; 
swap2(&i1,&i2); 
cout << "After 2nd sawp function "<< endl; 
cout << "i1 = "<<i1 <<endl; 
cout << "i2 = "<< i2 << endl; 
swap3(i1,i2); 
cout << "After 3rd sawp function "<< endl; 
cout << "i1 = "<< i1 <<endl; 
cout << "i2 = "<< i2 << endl;} 
After 1st sawp function 
i1 = 5 
i2 = 7 
After 2nd sawp function 
i1 = 7 
i2 = 5 
After 3rd sawp function 
i1 = 5 
i2 = 7
Dynamic Memory Allocation
Dynamic Memory Allocation 
•What Dynamic means? 
•What Memory Allocation means? 
•What Dynamic Memory Allocation means?
Dynamic Memory Allocation 
•When Dynamic Memory Allocation is used? 
•Array that its extent is not known till Runtime 
•Objects that needs to be created but not known till Runtime 
•What Dynamic Memory Allocation do? 
–Allocate and de-allocate memory at run time depending on the information at running time 
•new delete keywords (Dynamic)
Dynamic Memory Allocation 
•Static Memory Allocation 
–Allocated at compile time 
•Dynamic Memory Allocation 
–Allocated at Run time
Dynamic Memory Allocation Pointers and Arrays
Pointers and Arrays 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
inta[200]; // Static, Allocate at compile time 
} 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
intArrLength; 
int*ptr; 
cin >> ArrLength; 
ptr = newint[ArrLength]; // Dynamic, allocate at Runtime 
} 
The name of “Array” is the address of the its first element in memory
Pointers and Arrays 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
int*p1, *p2; 
p1 = newint;// allocate but not initialized yet 
p2 = newint; // allocate but not initialized yet 
} 
p1 
p2 
-842150451 
546546546
Pointers and Arrays 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
int*p1, *p2; 
p1 = newint;// allocate but not initialized yet 
p2 = newint(5);// allocate and initialized 
cout << *p1 << endl; 
cout << *p2 << endl; 
} 
5 
p1 
p2 
-842150451 
5
Pointers and Arrays 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
int*p1, *p2; 
p1 = newint; 
p2 = newint[5]; 
cout << *p1 << endl; 
cout << *p2 << endl; 
} 
p1 
p2 
-842150451 
-842150451
Pointers and Arrays 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
int*p1; 
p1 = newint[5]; 
*p1 = 2; 
*(p1+1) = 3; 
cout << *p1 << endl; 
cout << *(p1+1) << endl; 
} 
2 
3 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
int*p1; 
p1 = newint[5]; 
*p1 = 2; 
*(p1+1) = 3; 
cout << *p1 << endl; 
cout << *(p1+1) << endl; 
cout << *(p1+2) << endl; 
} 
2 
3 
-842150451 
Press any key to continue
Pointers and Arrays 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
int*p1; 
p1 = newint[5]; 
*p1 = 2; 
*(p1+1) = 4; 
cout << *p1 << endl; 
cout << *(p1+1) << endl; 
cout << *p1+1 << endl; 
// again, the array name is the address 
// of its first element in memory 
} 
2 
4 
3 
Press any key to continue 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
int*p1; 
p1 = newint[5]; 
*p1 = 2; 
*p1+1 = 4; 
} 
Compiler error
Pointers and Arrays 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
int*p1; 
p1 = newint[5]; 
*p1 = 2; 
*(p1++) = 4; 
cout << *p1 << endl; 
cout << *(p1+1) << endl; 
cout << *(p1-1) << endl; 
} 
-842150451 
-842150451 
4 
Press any key to continue 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
int*p1; 
p1 = newint[5]; 
*p1 = 2; 
cout << *p1 << endl; 
cout << p1 << endl; 
deletep1; 
cout << *p1 << endl; 
cout << p1 << endl; 
} 
2 
006D1848 
-572662307 
006D1848 
Press any key to continue
Pointers and Arrays 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
int*p1; 
p1 = newint[5]; 
*p1 = 2; 
cout << *p1 << endl; 
cout << p1 << endl; 
deletep1; 
cout << *p1 << endl; 
cout << p1 << endl; 
*p1 = 3; 
// Re-allocating without new!!!!!, it works!!! 
cout << *p1 << endl; 
cout << p1 << endl; 
} 
2 
00061848 
-572662307 
00061848 
3 
00061848
Pointers and Arrays 
•Now, Memory is allocated 
–But not initialized 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
intArr[10]; 
} 
? 
? 
? 
? 
? 
? 
? 
? 
? 
?
Pointers and Arrays 
•Now, Memory is allocated 
–But not initialized 
? 
? 
? 
? 
? 
? 
? 
? 
? 
2 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
intArr[10]; 
Arr[9] = 2; 
cout << Arr[9] << endl; 
cout << Arr[2] << endl; 
}
Pointers and Arrays 
•In C++, the array is really just a pointer to its first element 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
intArr[10]; 
} 
Arr
Pointers and Arrays 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
intArr[8]= {1,2,4,78,9,2,7,3}; 
if(Arr == &Arr[0]) 
{ 
cout << "This will ALWAYS be true!"<< endl; 
} 
} 
This will ALWAYS be true! 
Press any key to continue
Pointers and Arrays 
Arr[0] 
*Arr 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
intArr[8]; 
cout << Arr[0] << endl; 
cout << *Arr << endl; 
cout << *(&Arr[0]) << endl; 
} 
1306992 
1306992 
1306992
Pointers and Arrays 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
intArr[] = {1,2,3}; 
int*ArrPtr = Arr; 
ArrPtr++; 
ArrPtr++; 
ArrPtr-=2; 
cout << *(ArrPtr++) << endl; 
ArrPtr--; 
ArrPtr = Arr; 
ArrPtr += 2; 
cout << ArrPtr -Arr << endl; 
cout << *ArrPtr << endl; 
cout << *Arr << endl; 
} 
1 
2 
3 
1
Pointers and Arrays 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
intsum = 0; 
intArr[ArrSize] = {1,3}; 
for(inti=0; i<ArrSize; i++) 
{ 
sum += Arr[i]; 
} 
cout << "The sum is = "<< sum << endl; 
} 
4 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
intsum = 0; 
intArr[ArrSize] = {1,3}; 
int*ptr; 
for(ptr = Arr; ptr < Arr+ArrSize-1; ++ptr) 
{ 
sum += *ptr; 
} 
cout << "The sum is = "<< sum << endl; 
} 
4
Pointers and Arrays 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
intsum = 0; 
intArr[ArrSize] = {1,3}; 
int*ptr; 
for(ptr = Arr; ptr < &Arr[ArrSize]; ++ptr) 
{ 
sum += *ptr; 
} 
cout << "The sum is = "<< sum << endl; 
} 
4
Pointers and Arrays 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
intsum = 0; 
intArr[ArrSize] = {1,3}; 
int* ptr = Arr; 
inti = 2; 
cout << ptr[1] << endl; 
cout << ptr[2] << endl; 
} 
3 
0 
Press any key to continue 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
intArr[10]; 
for(inti = 0; i< 10; i++) 
{ 
Arr[i] = i; 
} 
int* p = Arr; 
cout << *p << endl; 
cout << *p+1 << endl; 
cout << *p++ << endl; 
cout << *++p << endl; 
cout << *(p+1) << endl; 
cout << *(p+5) << endl; 
} 
0 
1 
0 
2 
3 
7
Pointers and Arrays 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
intArr[10]; 
int*Iter = &Arr[0]; 
int*Iter_End = &Arr[10]; 
inti = 0; 
while(Iter!= Iter_End) 
{ 
cout << "We're in elemet #"<< i 
<< ", with value = "<< *Iter << endl; 
++Iter; 
i++; 
} 
} 
We're in elemet #0, with value = 2429382 
We're in elemet #1, with value = 1371168 
We're in elemet #2, with value = 1518572499 
We're in elemet #3, with value = 1 
We're in elemet #4, with value = 3336456 
We're in elemet #5, with value = 1371200 
We're in elemet #6, with value = 2430657 
We're in elemet #7, with value = 1371888 
We're in elemet #8, with value = 1371336 
We're in elemet #9, with value = 3336456 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
intArr[10] = {}; 
int*Iter = &Arr[0]; 
int*Iter_End = &Arr[10]; 
inti = 0; 
while(Iter!= Iter_End) 
{ 
cout << "We're in elemet #"<< i 
<< ", with value = "<< *Iter << endl; 
++Iter; 
i++; 
} 
} 
We're in elemet #0, with value = 0 
We're in elemet #1, with value = 0 
We're in elemet #2, with value = 0 
We're in elemet #3, with value = 0 
We're in elemet #4, with value = 0 
We're in elemet #5, with value = 0 
We're in elemet #6, with value = 0 
We're in elemet #7, with value = 0 
We're in elemet #8, with value = 0 
We're in elemet #9, with value = 0
Pointers and Arrays 
•Dynamic arrays 
–Size is not specified at compile time but at Run time! 
•By using new 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
int*ptr = newint[4]; // allocating 4 interger elements 
}
Pointers and Arrays 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
int*ptr = newint[]; 
cout << *ptr << endl; 
cout << *(ptr+3) << endl; 
} 
-33686019 
-1962934272 
Press any key to continue 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
int*ptr = newint[]; 
cout << *ptr << endl; 
cout << *(ptr+3) << endl; 
delete[] ptr; 
cout << *ptr << endl; 
} 
-33686019 
-1962934272 
-572662307 
Press any key to continue
Pointers and Arrays 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
int*ptr = newint[6]; 
cout << *ptr << endl; 
cout << *(ptr+3) << endl; 
delete[] ptr; 
cout << *ptr << endl; 
} 
-842150451 
-842150451 
-572662307 
Press any key to continue 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
int*ptr = newint[]; 
cout << *ptr << endl; 
cout << *(ptr+3) << endl; 
deleteptr []; 
cout << *ptr << endl; 
} 
Compiler error The syntax for deleting isdelete[] ptr;
Pointers and Arrays 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
float* pArr; 
intLength; 
cin >> Length;// 5 
pArr = newfloat[Length]; 
for(inti= 0; i < Length; i++ ) 
{ 
pArr[i] = i; 
} 
cout << "The array is: "<< endl; 
for(inti= 0; i < Length; i++ ) 
{ 
cout << pArr[i] << endl; 
} 
} 
5 
The array is: 
0 
1 
2 
3 
4 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
float* pArr; 
intLength; 
cin >> Length;// 5 
pArr = newfloat[Length]; 
for(float i= 0; i < Length; i++ ) 
{ 
pArr[i] = i; 
} 
cout << "The array is: "<< endl; 
for(inti= 0; i < Length; i++ ) 
{ 
cout << pArr[i] << endl; 
} 
} 
Compiler error 
Counter must be an integral type 
0 –0x000441C40 
2 –0x000441C48 
3 –0x000441C50 
4 –0x000441C58
Pointers and Strings
Pointers and Strings 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
charKoKo[] = "Hello!"; // defining a string 
char*Ptr = KoKo; 
*Ptr = 'C'; 
*Ptr++ = 'V'; 
*Ptr = 'B'; 
cout << KoKo << endl; 
} 
VBllo! 
Press any key to continue 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
char KoKo[] = "Hello!"; // defining a string 
char *Ptr = KoKo; 
*Ptr = 'C'; 
*Ptr++ = "V"; 
*Ptr = 'B'; 
cout << KoKo << endl; 
} 
Compiler error, ””
Pointers and Strings 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
char*p = newchar[10]; 
cout << *(p+9) << endl; 
} 
=
Pointers and Strings 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
char*p = newchar[10]; 
cin >> p; 
cout << p << endl; 
cout << *(p+9) << endl; 
} 
123456789 
123456789 
Press any key to continue 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
char*p = newchar[10]; 
cin >> p; 
cout << p << endl; 
cout << *(p+9) << endl; 
} 
32423 
32423 
═ 
Press any key to continue
Pointers and Strings 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
char*p = newchar[10]; 
cin >> p; 
cout << p << endl; 
cout << *(p+9) << endl; 
} 
123456789123456789 
123456789123456789 
1 
Press any key to continue 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
char*p = newchar[10]; 
cin >> p; 
p[9]='0';// now used to cut down the string 
// heheheeee!!!:D 
cout << p << endl; 
cout << *(p+9) << endl; 
} 
213124234324234 
213124234 
Press any key to continue
Pointers and Strings 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
char*p = newchar[10]; 
cin >> p; 
p[9]='0';// now used to cut down the string 
// heheheeee!!!:D 
cout << p << endl; 
cout << *(p+9) << endl; 
} 
234234 
234234 
Press any key to continue 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
char*p = "COOL MAN!"; 
cout << p << endl; 
cout << *p << endl; 
cout << &*p << endl; 
cout << *&p << endl; 
cout << *(p+3) << endl; 
} 
COOL MAN! 
C 
COOL MAN! 
COOL MAN! 
L
Pointers andMulti-dimensional Arrays
Pointers and Multi-dimensional Arrays 
Z 
e 
Z 
e 
Z 
o 
o 
Z 
o 
o 
M 
o 
M 
o 
o 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
char* WoW[4] = {"MoMoo", "MeMe", “ZeZe", "ZooZoo"}; 
} 
What does that means?! 
M 
e 
M 
e
Pointers and Multi-dimensional Arrays 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
char* WoW[4] = {"MoMoo", "MeMe", “ZeZe", "ZooZoo"}; 
} 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
char* WoW[] = {"MoMoo","MeMe", “ZeZe", "ZooZoo"}; 
} 
Z 
e 
Z 
e 
Z 
o 
o 
Z 
o 
o 
M 
o 
M 
o 
o 
Both are the same 
M 
e 
M 
e 
Size of the arrays is determined at compile time
Pointers and Multi-dimensional Arrays 
What’s the difference?! 
int 
int 
int 
ThatIsCrazy1 
int 
ThatIsCrazy2 
int 
int 
int 
int 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
int*ThatIsCrazy1 [4]; 
int(*ThatIsCrazy2) [4]; 
}
Pointers and Multi-dimensional Arrays 
What’s the difference?! 
int 
int 
int 
ThatIsCrazy1 
int 
ThatIsCrazy2 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
int*ThatIsCrazy1 [4]; 
int*(ThatIsCrazy2 [4]); 
} 
int 
int 
int 
int 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
int*ThatIsCrazy1 [4]; 
int(*ThatIsCrazy2) [4]; 
}
constPointers
constPointers 
•What does constfor pointers means? 
–ptris a pointer which points to a constinteger? 
–OR, ptris a pointer (not a constpointer)? 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
constint*ptr; 
}
Pointers and Strings 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
inti = 2; 
constint*ptr; 
ptr = &i;// changing the pointer 
} 
Compile and run 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
inti = 2; 
constint*ptr; 
ptr = &i; 
*ptr = 3; 
} 
Compiler error Coz the thing which the pointer points to is const and shouldn’t be modified!
Pointers and Strings 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
inti = 2; 
constint*ptr; 
ptr = &i; 
cout << *ptr << endl; 
} 
2 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
inti; 
int*constptr =&i; 
} 
What does this means? 
Ptr is a pointer which is const which points to integer
Pointers and Strings 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
inti; 
int*constptr; 
} 
Compiler error 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
inti,j; 
int*constptr =&i; 
ptr = &j; 
} 
Compiler error
Pointers and Strings 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
inti=2, j; 
int*constptr =&i; 
cout << *ptr << endl; 
} 
2 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
inti, j; 
int*constptr =&i; 
cout << *ptr << endl; 
} 
2458743545
constPointers 
•Pointer to const 
–constint*ptr; 
–intconst*ptr; 
•constpointer 
–int*constptr;
constPointers 
•Const pointer to const 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
inti = 3; 
constint*ptr1;// pointer to const 
intconst*ptr2;// pointer to const 
int*constptr3 = &i;// const pointer 
constint*constptr4 = &i;// const pointer to const 
intconst*constptr5 = &i;// const pointer to const 
}
Pointers and Strings 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
inti = 3; 
constint*ptr1;// pointer to const 
intconst*ptr2;// pointer to const 
int*constptr3;// const pointer 
constint*constptr4;//const pointer to const 
intconst*constptr5;//const pointer to const 
} 
Compiler error for: 
ptr3 
ptr4 
ptr5 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
inti = 3, j = 4; 
// const pointer to const 
constint*constptr1 = &i; 
// const pointer to const 
intconst*constptr3 = &i; 
// attempting to dereferance 
// and change pointer 
ptr1 = &j; 
} 
Compiler error
Pointers and Strings 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
inti = 3, j = 4; 
// const pointer to const 
constint*constptr1 = &i; 
// const pointer to const 
intconst*constptr3 = &i; 
*ptr1 = 3; // change the int 
} 
Compiler error 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
inti = 3, j = 4; 
// const pointer to const 
constint*constptr1 = &i; 
// const pointer to const 
intconst*constptr3 = &i; 
// Attempting to dereferance 
// and change the pointer 
ptr1 = &j; 
} 
Compiler error
Pointers to Pointers
Pointers to Pointers 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
charc = 'c'; 
char*ptr; 
char**pptr; 
c = 'b'; 
ptr = &c; 
pptr = &ptr; 
cout << c << endl; 
cout << *ptr << endl; 
cout << ptr << endl; 
cout << &c << endl; 
cout << pptr << endl; 
cout << &ptr << endl; 
cout << &pptr << endl; 
} 
b 
b 
b 
b 
0028EF54 
0028EF54 
0028EF4C 
‘b’ 
pptr 
ptr 
c
voidPointers
voidPointers 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{int*iptr; 
void*vptr; 
inti = 3; 
floatf = 4; 
iptr = &i; 
//iptr = &f; // error 
vptr = &i; 
cout << *vptr << endl; 
cout << &vptr << endl; 
cout << vptr << endl; 
vptr = &f; 
cout << *vptr << endl; 
cout << &vptr << endl; 
cout << vptr << endl; 
} 
Compiler error. Why? 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
int*iptr; 
void*vptr; 
inti = 3; 
floatf = 4; 
iptr = &i; 
vptr = &i; 
cout << (*((int*)vptr)) << endl; 
cout << &vptr << endl; 
cout << vptr << endl; 
vptr = &f; 
cout << (*((float*)vptr)) << endl; 
cout << &vptr << endl; 
cout << vptr << endl; 
} 
3 
0016EEC4 
0016EEB8 
4 
0016EEC4 
0016EEBC
Returning Pointers
Returning Pointers 
#include<iostream> 
usingnamespace::std; 
int[] GoToFun(int A[2]) 
{ 
cout << A[0] << endl; 
A[0] = 1; 
returnA; 
} 
voidmain(void) 
{ 
intArr[] = {3,4}; 
GoToFun(Arr); 
} 
Compiler error. Arrays can’t be used as a return type of functions. So, how can we do it?!?!!! 
#include<iostream> 
usingnamespace::std; 
int* GoToFun(int*p1) 
{ 
cout << p1[0] << endl; 
p1[0] = 1; 
cout << p1[0] << endl; 
returnp1; 
} 
voidmain(void) 
{ 
intArr[] = {3,4}; 
int*ptr = Arr; 
cout << "In main, "<< *ptr << endl; 
GoToFun(ptr); 
cout << "In main, "<< *ptr << endl; 
} 
In main, 3 
3 
1 
In main, 1
Returning Pointers 
#include<iostream> 
usingnamespace::std; 
int* GoToFun(int*p1) 
{ 
cout << p1[0] << endl; 
p1[0] = 1; 
cout << p1[0] << endl; 
returnp1; 
} 
voidmain(void) 
{ 
intArr[] = {3,4}; 
int*ptr = &Arr; 
cout << "In main, "<< *ptr << endl; 
GoToFun(ptr); 
cout << "In main, "<< *ptr << endl; 
} 
Error1error C2440: 'initializing': cannot convert from 'int (*)[2]' to 'int *' c:UsersZGTRDocumentsVisual Studio 2008ProjectsTempC++File++TempC++File++MyFile.cpp16TempC++File++ 
#include<iostream> 
usingnamespace::std; 
int& Reproducer(intA[], intindex) 
{ 
returnA[index]; 
} 
voidmain(void) 
{ 
inti = 5; 
intArr[] = {3,4,7}; 
cout << "Before"<< endl; 
for(i = 0; i < 3;++i) 
{ 
cout << Arr[i] << endl;;;;; 
} 
Reproducer(Arr,1) = -10; 
cout << "After"<< endl; 
for(i = 0; i < 3;++i) 
{ 
cout << Arr[i] << endl;;;;; 
} 
} 
Before 
3 
4 
7 
After 
3 
-10 
7
Returning Pointers 
#include<iostream> 
usingnamespace::std; 
int& Reproducer(intA[], intindex) 
{ 
return&A[index]; 
} 
voidmain(void) 
{ 
inti = 5; 
intArr[] = {3,4,7}; 
cout << "Before"<< endl; 
for(i = 0; i < 3;++i) 
{ 
cout << Arr[i] << endl;;;;; 
} 
Reproducer(Arr,1) = -10; 
cout << "After"<< endl; 
for(i = 0; i < 3;++i) 
{ 
cout << Arr[i] << endl;;;;; 
} 
} 
Compiler error. &A[index];
Memory Problems
Memory Problems 
•Memory leak 
–Loss of available memory space 
–Occurs when the dynamic memory space has been allocated but never de-allocated 
•Dangling pointer 
–A pointer that points to dynamic memory that has been de-allocated
Memory Problems -LEAK 
•Now 4 is allocated but can never been reached or used 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
int*p; 
p = newint; 
*p = 4; 
p = newint; 
*p = 5; 
} 
4 
5 
p1 
4 
p1
Memory Problems -LEAK 
•Now 6 is allocated but can never been reached or used 
00401DE0 -4 
p1 address 0028EEA4 
00401DE0 -4 
p2 address 0028EEA0 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
int*p1; 
p1 = newint(5); 
*p1 = 4; 
int*p2 = newint(6); 
p2 = p1; 
cout << p1 << " -"<< *p1 << endl; 
cout << "p1 address "<< &p1 << endl; 
cout << p2 << " -"<< *p2 << endl; 
cout << "p2 address "<< &p2 << endl; 
} 
5 
6 
p1 
5 
p1 
6 
p2 
p2
Memory Problems -Dangling 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
int*p1; 
p1 = newint(5); 
int*p2 = newint(6); 
p2 = p1; 
deletep1; 
cout << p1 << " -"<< *p1 << endl; 
cout << "p1 address "<< &p1 << endl; 
cout << p2 << " -"<< *p2 << endl; 
cout << "p2 address "<< &p2 << endl; 
} 
6 
p1 
5 
p1 
6 
p2 
p2 
00271DE0 --33686272 
p1 address 0025F064 
00271DE0 --33686272 
p2 address 0025F060
Code Cracking
Code Cracking 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
int*p; 
if(1) 
{ 
intx= 343299; 
*p = x; 
} 
cout << *p << endl; 
} 
Compile but runtime error 
#include<iostream> 
usingnamespace::std; 
int*p; 
voidSthWrong() 
{ 
intb = 3; 
p = &b; 
} 
voidmain(void) 
{ 
cout << p << endl; 
cout << &p << endl; 
SthWrong(); 
cout << p << endl; 
cout << &p << endl; 
cout << *p << endl; 
} 
00000000 
00300974 
0019EE68 
00300974 
1699624
Code Cracking 
#include<iostream> 
usingnamespace::std; 
int*p; 
voidSthWrong() 
{ 
intb = 3; 
*p = b; 
} 
voidmain(void) 
{ 
cout << p << endl; 
cout << &p << endl; 
SthWrong(); 
cout << p << endl; 
cout << &p << endl; 
cout << *p << endl; 
} 
Compile but runtime error 
Missing new 
#include<iostream> 
usingnamespace::std; 
int*p; 
voidSthWrong() 
{ 
intb = 3; 
*p = b; 
} 
voidmain(void) 
{ 
p = newint; 
cout << p << endl; 
cout << &p << endl; 
SthWrong(); 
cout << p << endl; 
cout << &p << endl; 
cout << *p << endl; 
} 
005B1DE0 
00310978 
005B1DE0 
00310978 
3
Code Cracking 
#include<iostream> 
#include<stdlib.h> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 100; 
int* ptr = NULL; 
if(ptr = NULL) 
{ 
cout << "I'm NULL "<< endl; 
} 
else 
{ 
cout << "I'm not a NULL! "<< endl; 
} 
} 
I'm not a NULL! 
Press any key to continue 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 100; 
int*ptr =&i; 
*ptr = int(&i); 
cout << *ptr << endl; 
cout << &i << endl; 
} 
2223152 
0021EC30 
Press any key to continue
Code Cracking 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 100; 
int*ptr; 
*ptr = &i; 
} 
Compiler error, can’t convert from *int to int
Ever dreamed to pass a functionas a parameter?
Function Pointers
Function PointersDelegates, Event Handling, etc.
Function PointersAllows operations with pointers to functionsFunction pointers contains the address of the function in memory
Function Pointers 
#include<iostream> 
usingnamespace::std; 
intAdd(inta, intb ){returna+b; } 
intSub(inta, intb ){returna-b; } 
int(*AddPtr) (int,int) = Add; 
int(*SubPtr) (int,int) = Sub; 
intOperationToDo (intx, inty, int(*func) (int,int)) 
{intResult; 
Result = (*func)(x,y); 
returnResult;} 
voidmain(void) 
{inta=3, b=4; 
intResult; 
Result = OperationToDo(a,b,AddPtr); 
cout << "a="<< a << endl; 
cout << "b="<< b << endl; 
cout << "Result ="<< Result << endl; 
Result = OperationToDo(b,a,SubPtr); 
cout << "a="<< a << endl; 
cout << "b="<< b << endl; 
cout << "Result ="<< Result << endl; 
} 
a=3 
b=4 
Result =7 
a=3 
b=4 
Result =1
Quiz
Quiz 1, 2 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
chari1=100, i2=300; 
char*p1,*p2; 
p2 = &i1; 
cout << *p2 << endl; 
} 
d 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
int*p1; 
p1 = newint[5]; 
*p1 = 2; 
*(++p1) = 4; 
cout << *p1 << endl; 
cout << *(p1+1) << endl; 
cout << *(p1-1) << endl; 
} 
4 
-842150451 
2 
Press any key to continue
Quiz 3, 4 
#include<iostream> 
usingnamespace::std; 
voidmain(void) 
{ 
inti = 100; 
int*ptr; 
*ptr = 100; 
cout << ptr << endl; 
cout << *ptr << endl; 
} 
Runtime error. Printing *ptr without initializing it first 
#include<iostream> 
usingnamespace::std; 
int*p; 
voidSthWrong() 
{ 
intb = 3; 
*p = b; 
} 
voidmain(void) 
{ 
p = newint; 
cout << p << endl; 
cout << &p << endl; 
SthWrong(); 
cout << p << endl; 
cout << &p << endl; 
cout << *p << endl; 
} 
005B1DE0 
00310978 
005B1DE0 
00310978 
3
Quiz 5, 6 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
intArr[10]; 
for(inti = 0; i< 10; i++) 
{ 
Arr[i] = 2*i; 
} 
int* p = Arr; 
cout << *p << endl; 
cout << *p+1 << endl; 
cout << *p++ << endl; 
cout << *++p << endl; 
cout << *(p+1) << endl; 
cout << *(p+5) << endl; 
} 
0 
1 
0 
4 
6 
14 
#include<iostream> 
usingnamespace::std; 
constintArrSize = 10; 
voidmain(void) 
{ 
charKoKo[] = "WOWAIOE"; // defining a string 
char*Ptr = KoKo; 
*Ptr = 'C'; 
*Ptr++ = 'K'; 
*Ptr = 'B'; 
cout << KoKo << endl; 
} 
KBWAIOE 
Press any key to continue

Weitere ähnliche Inhalte

Was ist angesagt?

Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...ssuserd6b1fd
 
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...ssuserd6b1fd
 
Modern C++ Concurrency API
Modern C++ Concurrency APIModern C++ Concurrency API
Modern C++ Concurrency APISeok-joon Yun
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...ssuserd6b1fd
 
C tech questions
C tech questionsC tech questions
C tech questionsvijay00791
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...ssuserd6b1fd
 
Programming with GUTs
Programming with GUTsProgramming with GUTs
Programming with GUTsKevlin Henney
 
What We Talk About When We Talk About Unit Testing
What We Talk About When We Talk About Unit TestingWhat We Talk About When We Talk About Unit Testing
What We Talk About When We Talk About Unit TestingKevlin Henney
 
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays 2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays kinan keshkeh
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...ssuserd6b1fd
 
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...ssuserd6b1fd
 
13 Strings and Text Processing
13 Strings and Text Processing13 Strings and Text Processing
13 Strings and Text ProcessingIntro C# Book
 

Was ist angesagt? (20)

C++ L09-Classes Part2
C++ L09-Classes Part2C++ L09-Classes Part2
C++ L09-Classes Part2
 
C++ L10-Inheritance
C++ L10-InheritanceC++ L10-Inheritance
C++ L10-Inheritance
 
Unit 3
Unit 3 Unit 3
Unit 3
 
c programming
c programmingc programming
c programming
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 4 of 5 by...
 
c programming
c programmingc programming
c programming
 
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
Notes for C++ Programming / Object Oriented C++ Programming for MCA, BCA and ...
 
Modern C++ Concurrency API
Modern C++ Concurrency APIModern C++ Concurrency API
Modern C++ Concurrency API
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 5 of 5 by...
 
C++ Pointers
C++ PointersC++ Pointers
C++ Pointers
 
C tech questions
C tech questionsC tech questions
C tech questions
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5  b...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 3 of 5 b...
 
Programming with GUTs
Programming with GUTsProgramming with GUTs
Programming with GUTs
 
Functional C++
Functional C++Functional C++
Functional C++
 
What We Talk About When We Talk About Unit Testing
What We Talk About When We Talk About Unit TestingWhat We Talk About When We Talk About Unit Testing
What We Talk About When We Talk About Unit Testing
 
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays 2 BytesC++ course_2014_c9_ pointers and dynamic arrays
2 BytesC++ course_2014_c9_ pointers and dynamic arrays
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
 
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
 
Fp201 unit4
Fp201 unit4Fp201 unit4
Fp201 unit4
 
13 Strings and Text Processing
13 Strings and Text Processing13 Strings and Text Processing
13 Strings and Text Processing
 

Andere mochten auch

YEG-Agile-planning
YEG-Agile-planningYEG-Agile-planning
YEG-Agile-planningAmir Barylko
 
Agile requirements
Agile requirementsAgile requirements
Agile requirementsAmir Barylko
 
PRDCW-advanced-design-patterns
PRDCW-advanced-design-patternsPRDCW-advanced-design-patterns
PRDCW-advanced-design-patternsAmir Barylko
 
sdec11-Advanced-design-patterns
sdec11-Advanced-design-patternssdec11-Advanced-design-patterns
sdec11-Advanced-design-patternsAmir Barylko
 

Andere mochten auch (7)

Agile planning
Agile planningAgile planning
Agile planning
 
YEG-Agile-planning
YEG-Agile-planningYEG-Agile-planning
YEG-Agile-planning
 
Agile requirements
Agile requirementsAgile requirements
Agile requirements
 
agile-planning
agile-planningagile-planning
agile-planning
 
PRDCW-advanced-design-patterns
PRDCW-advanced-design-patternsPRDCW-advanced-design-patterns
PRDCW-advanced-design-patterns
 
Refactoring
RefactoringRefactoring
Refactoring
 
sdec11-Advanced-design-patterns
sdec11-Advanced-design-patternssdec11-Advanced-design-patterns
sdec11-Advanced-design-patterns
 

Ähnlich wie C++ L06-Pointers

Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxajajkhan16
 
ch5_additional.ppt
ch5_additional.pptch5_additional.ppt
ch5_additional.pptLokeshK66
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfyamew16788
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanMohammadSalman129
 
Please use the code below and make it operate as one program- Notating.pdf
Please use the code below and make it operate as one program- Notating.pdfPlease use the code below and make it operate as one program- Notating.pdf
Please use the code below and make it operate as one program- Notating.pdfseoagam1
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptxsajinis3
 
Array and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptxArray and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptxJumanneChiyanda
 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - ReferenceMohammed Sikander
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxRamakrishna Reddy Bijjam
 
Pointers in c++ programming presentation
Pointers in c++ programming presentationPointers in c++ programming presentation
Pointers in c++ programming presentationSourabhGour9
 

Ähnlich wie C++ L06-Pointers (20)

Pointer
PointerPointer
Pointer
 
Pointers in c++ by minal
Pointers in c++ by minalPointers in c++ by minal
Pointers in c++ by minal
 
Pointer in C
Pointer in CPointer in C
Pointer in C
 
Unit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptxUnit-I Pointer Data structure.pptx
Unit-I Pointer Data structure.pptx
 
ch5_additional.ppt
ch5_additional.pptch5_additional.ppt
ch5_additional.ppt
 
Pointer.pptx
Pointer.pptxPointer.pptx
Pointer.pptx
 
C++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdfC++ Nested loops, matrix and fuctions.pdf
C++ Nested loops, matrix and fuctions.pdf
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad SalmanPointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
 
PSPC--UNIT-5.pdf
PSPC--UNIT-5.pdfPSPC--UNIT-5.pdf
PSPC--UNIT-5.pdf
 
Please use the code below and make it operate as one program- Notating.pdf
Please use the code below and make it operate as one program- Notating.pdfPlease use the code below and make it operate as one program- Notating.pdf
Please use the code below and make it operate as one program- Notating.pdf
 
Pointers
PointersPointers
Pointers
 
Pointer
PointerPointer
Pointer
 
Cpp c++ 1
Cpp c++ 1Cpp c++ 1
Cpp c++ 1
 
PPS-POINTERS.pptx
PPS-POINTERS.pptxPPS-POINTERS.pptx
PPS-POINTERS.pptx
 
Array and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptxArray and string in C++_093547 analysis.pptx
Array and string in C++_093547 analysis.pptx
 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - Reference
 
Pointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptxPointers and single &multi dimentionalarrays.pptx
Pointers and single &multi dimentionalarrays.pptx
 
Pointers in c++ programming presentation
Pointers in c++ programming presentationPointers in c++ programming presentation
Pointers in c++ programming presentation
 
4 Pointers.pptx
4 Pointers.pptx4 Pointers.pptx
4 Pointers.pptx
 
oodp elab.pdf
oodp elab.pdfoodp elab.pdf
oodp elab.pdf
 

Mehr von Mohammad Shaker

12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian Graduate12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian GraduateMohammad Shaker
 
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]Mohammad Shaker
 
Interaction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with PsychologyInteraction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with PsychologyMohammad Shaker
 
Short, Matters, Love - Passioneers Event 2015
Short, Matters, Love -  Passioneers Event 2015Short, Matters, Love -  Passioneers Event 2015
Short, Matters, Love - Passioneers Event 2015Mohammad Shaker
 
Unity L01 - Game Development
Unity L01 - Game DevelopmentUnity L01 - Game Development
Unity L01 - Game DevelopmentMohammad Shaker
 
Android L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and WearablesAndroid L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and WearablesMohammad Shaker
 
Interaction Design L03 - Color
Interaction Design L03 - ColorInteraction Design L03 - Color
Interaction Design L03 - ColorMohammad Shaker
 
Interaction Design L05 - Typography
Interaction Design L05 - TypographyInteraction Design L05 - Typography
Interaction Design L05 - TypographyMohammad Shaker
 
Interaction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and CouplingInteraction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and CouplingMohammad Shaker
 
Android L04 - Notifications and Threading
Android L04 - Notifications and ThreadingAndroid L04 - Notifications and Threading
Android L04 - Notifications and ThreadingMohammad Shaker
 
Android L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOSAndroid L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOSMohammad Shaker
 
Interaction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile ConstraintsInteraction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile ConstraintsMohammad Shaker
 
Interaction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and GridsInteraction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and GridsMohammad Shaker
 
Android L10 - Stores and Gaming
Android L10 - Stores and GamingAndroid L10 - Stores and Gaming
Android L10 - Stores and GamingMohammad Shaker
 
Android L06 - Cloud / Parse
Android L06 - Cloud / ParseAndroid L06 - Cloud / Parse
Android L06 - Cloud / ParseMohammad Shaker
 
Android L08 - Google Maps and Utilities
Android L08 - Google Maps and UtilitiesAndroid L08 - Google Maps and Utilities
Android L08 - Google Maps and UtilitiesMohammad Shaker
 
Android L03 - Styles and Themes
Android L03 - Styles and Themes Android L03 - Styles and Themes
Android L03 - Styles and Themes Mohammad Shaker
 
Android L02 - Activities and Adapters
Android L02 - Activities and AdaptersAndroid L02 - Activities and Adapters
Android L02 - Activities and AdaptersMohammad Shaker
 

Mehr von Mohammad Shaker (20)

12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian Graduate12 Rules You Should to Know as a Syrian Graduate
12 Rules You Should to Know as a Syrian Graduate
 
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
Ultra Fast, Cross Genre, Procedural Content Generation in Games [Master Thesis]
 
Interaction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with PsychologyInteraction Design L06 - Tricks with Psychology
Interaction Design L06 - Tricks with Psychology
 
Short, Matters, Love - Passioneers Event 2015
Short, Matters, Love -  Passioneers Event 2015Short, Matters, Love -  Passioneers Event 2015
Short, Matters, Love - Passioneers Event 2015
 
Unity L01 - Game Development
Unity L01 - Game DevelopmentUnity L01 - Game Development
Unity L01 - Game Development
 
Android L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and WearablesAndroid L07 - Touch, Screen and Wearables
Android L07 - Touch, Screen and Wearables
 
Interaction Design L03 - Color
Interaction Design L03 - ColorInteraction Design L03 - Color
Interaction Design L03 - Color
 
Interaction Design L05 - Typography
Interaction Design L05 - TypographyInteraction Design L05 - Typography
Interaction Design L05 - Typography
 
Interaction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and CouplingInteraction Design L04 - Materialise and Coupling
Interaction Design L04 - Materialise and Coupling
 
Android L05 - Storage
Android L05 - StorageAndroid L05 - Storage
Android L05 - Storage
 
Android L04 - Notifications and Threading
Android L04 - Notifications and ThreadingAndroid L04 - Notifications and Threading
Android L04 - Notifications and Threading
 
Android L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOSAndroid L09 - Windows Phone and iOS
Android L09 - Windows Phone and iOS
 
Interaction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile ConstraintsInteraction Design L01 - Mobile Constraints
Interaction Design L01 - Mobile Constraints
 
Interaction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and GridsInteraction Design L02 - Pragnanz and Grids
Interaction Design L02 - Pragnanz and Grids
 
Android L10 - Stores and Gaming
Android L10 - Stores and GamingAndroid L10 - Stores and Gaming
Android L10 - Stores and Gaming
 
Android L06 - Cloud / Parse
Android L06 - Cloud / ParseAndroid L06 - Cloud / Parse
Android L06 - Cloud / Parse
 
Android L08 - Google Maps and Utilities
Android L08 - Google Maps and UtilitiesAndroid L08 - Google Maps and Utilities
Android L08 - Google Maps and Utilities
 
Android L03 - Styles and Themes
Android L03 - Styles and Themes Android L03 - Styles and Themes
Android L03 - Styles and Themes
 
Android L02 - Activities and Adapters
Android L02 - Activities and AdaptersAndroid L02 - Activities and Adapters
Android L02 - Activities and Adapters
 
Android L01 - Warm Up
Android L01 - Warm UpAndroid L01 - Warm Up
Android L01 - Warm Up
 

Kürzlich hochgeladen

Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdfKamal Acharya
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdfKamal Acharya
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Call Girls Mumbai
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptxJIT KUMAR GUPTA
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"mphochane1998
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxSCMS School of Architecture
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadhamedmustafa094
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...HenryBriggs2
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityMorshed Ahmed Rahath
 
Air Compressor reciprocating single stage
Air Compressor reciprocating single stageAir Compressor reciprocating single stage
Air Compressor reciprocating single stageAbc194748
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxSCMS School of Architecture
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdfKamal Acharya
 
Rums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdfRums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdfsmsksolar
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projectssmsksolar
 
Learn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic MarksLearn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic MarksMagic Marks
 

Kürzlich hochgeladen (20)

Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 
Online food ordering system project report.pdf
Online food ordering system project report.pdfOnline food ordering system project report.pdf
Online food ordering system project report.pdf
 
Online electricity billing project report..pdf
Online electricity billing project report..pdfOnline electricity billing project report..pdf
Online electricity billing project report..pdf
 
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
Bhubaneswar🌹Call Girls Bhubaneswar ❤Komal 9777949614 💟 Full Trusted CALL GIRL...
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptxS1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
S1S2 B.Arch MGU - HOA1&2 Module 3 -Temple Architecture of Kerala.pptx
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
kiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal loadkiln thermal load.pptx kiln tgermal load
kiln thermal load.pptx kiln tgermal load
 
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak HamilCara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
Cara Menggugurkan Sperma Yang Masuk Rahim Biyar Tidak Hamil
 
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
scipt v1.pptxcxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx...
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
Air Compressor reciprocating single stage
Air Compressor reciprocating single stageAir Compressor reciprocating single stage
Air Compressor reciprocating single stage
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptxHOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
HOA1&2 - Module 3 - PREHISTORCI ARCHITECTURE OF KERALA.pptx
 
Hostel management system project report..pdf
Hostel management system project report..pdfHostel management system project report..pdf
Hostel management system project report..pdf
 
Rums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdfRums floating Omkareshwar FSPV IM_16112021.pdf
Rums floating Omkareshwar FSPV IM_16112021.pdf
 
2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects2016EF22_0 solar project report rooftop projects
2016EF22_0 solar project report rooftop projects
 
Learn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic MarksLearn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic Marks
 

C++ L06-Pointers

  • 1. C++ Programming Language L06-POINTERS Mohammad Shaker mohammadshaker.com @ZGTRShaker 2010, 11, 12, 13, 14
  • 2. Float, double, long double C++ data types Structured Simple Address Pointer Reference enum Floating Array Struct Union Class Char, Short, int, long, bool Integral
  • 6. Pointers •Powerful feature in C++, but one of the most difficult to master •Using Dynamic objects –Can survive after the function ends in which they were allocated –Allow flexible-sized arrays and lists –Lists, Trees, stacks, Queues –Managing big sized, large objects throw passing them into functions •Note: –Can declare pointer to any data type •In general the type of the pointer must match the type of the data it’s set to point to
  • 7. Pointers •Pointer variable contain an “address” rather than a data “value” #include<iostream> usingnamespace::std; voidmain(void) { int*ptr; // initialize a pointer to an integer object } #include<iostream> usingnamespace::std; voidmain(void) { float*ptr; // initialize a pointer to an float object }
  • 8. Pointers #include<iostream> usingnamespace::std; voidmain(void) { int* ptr; // initialize a pointer to an integer object } #include<iostream> usingnamespace::std; voidmain(void) { int*ptr; // initialize a pointer to an integer object } #include<iostream> usingnamespace::std; voidmain(void) { int * ptr; // initialize a pointer to an integer object }
  • 9. Pointers #include<iostream> usingnamespace::std; voidmain(void) { int*ptr1, *ptr2; // ptr1, ptr2 are pointers } #include<iostream> usingnamespace::std; voidmain(void) { int*ptr1, p; // ptr1: pointer to int // p: int }
  • 10. Pointers #include<iostream> usingnamespace::std; voidmain(void) { int*ptr1, p; // ptr1: pointer to int // p: int } #include<iostream> usingnamespace::std; voidmain(void) { int* ptr1, p; // ptr1: pointer to int // p: int }
  • 11. Pointers #include<iostream> usingnamespace::std; typedefint* IntPtr; voidmain(void) { IntPtr Ptr1,Ptr2; // now, Ptr1,Ptr2 both are pointers to integer } int*ptr, c;// ptr: pointer, c: integer int* ptr,c;// ptr: pointer, c: integer int(* ptr),c;// ptr: pointer, c: integer intc, *ptr;// ptr: pointer, c: integer (int*) ptr,c;// compiler error int* ptr;// ptr: pointer int* ptr;// ptr: pointer int*ptr;// ptr: pointer
  • 12. Pointers c2is not pointing to anything, because it hasn’t been initialized, thus called, “null” pointer #include<iostream> usingnamespace::std; voidmain(void) { intc1=3, *c2; // c1: integer, c2: pointer to int } 3 c1 c2
  • 13. Pointers •“ The address of ” operator –& #include<iostream> usingnamespace::std; voidmain(void) { intc1; c1 = 3; cout << "The value of variable c1 = "<< c1 << endl; cout << "The address of variable c1 in memory = "<< &c1 << endl; } The value of variable c1 = 3 The address of variable c1 in memory = 0024EC64 Press any key to continue
  • 14. Pointers •A pointer is also a “variable” –So it also has its own memory address 100 #include<iostream> usingnamespace::std; voidmain(void) { inti = 100 ; int*ptr = new int; *ptr = i ; } memory 1024
  • 15. 100 i #include<iostream> usingnamespace::std; voidmain(void) { int i = 100 ; int*ptr = new int; *ptr = i ; } A pointer is also a “variable” So it also has its own memory address memory 1024 Pointers
  • 16. 100 1424 i ptr #include<iostream> usingnamespace::std; voidmain(void) { inti = 100 ; int *ptr = new int; *ptr = i ; } A pointer is also a “variable” So it also has its own memory address memory 1024 1064 1424 Pointers
  • 17. 100 1424 100 i ptr #include<iostream> usingnamespace::std; voidmain(void) { int i = 100 ; int *ptr = new int; *ptr = i ; } A pointer is also a “variable” So it also has its own memory address memory 1024 1064 1424 Pointers
  • 18. 200 i ptr #include<iostream> usingnamespace::std; voidmain(void) { inti = 200 ; int*ptr ; *ptr = 200 ; } Runtime Error memory 1024 1064 Pointers
  • 19. 100 1024 i ptr memory 1024 1064 #include<iostream> usingnamespace::std; voidmain(void) { inti = 100 ; int*ptr ; ptr = &i ; } The “pointer” now has the “address” of the “variable” Pointers
  • 20. 100 1024 i ptr #include<iostream> usingnamespace::std; voidmain(void) { inti = 100 ; int*ptr = &i ; // initialize at definition time } memory 1024 1064 Pointers
  • 21. Pointers #include<iostream> usingnamespace::std; voidmain(void) { inti = 100; int* ptr; ptr = &i; } #include<iostream> usingnamespace::std; voidmain(void) { inti = 100; int* ptr = &i; } As we know,they are all the same! #include<iostream> usingnamespace::std; voidmain(void) { inti=100, * ptr = &i; } #include<iostream> usingnamespace::std; voidmain(void) { inti = 100, * ptr; ptr = &i; }
  • 22. 100 1024 i ptr #include<iostream> usingnamespace::std; voidmain(void) { inti = 100 ; int*ptr = &i ; } memory 1024 1064 Pointers
  • 23. Code Cracking -Pointers #include<iostream> usingnamespace::std; voidmain(void) { inti = 100; int* ptr; ptr = &i; cout << i << " "<< &i << endl; cout << ptr << " "<< &ptr << endl; } 100 0020F180 0020F180 0020F184 Press any key to continue When printing the pointer “ptr” all by itself, the output will be the address of the variable in which the pointer “ptr” points to
  • 24. memory 002AF360 002AF364 100 002AF360 #include<iostream> usingnamespace::std; voidmain(void) { inti = 100 ; int* ptr ; ptr = &i ; cout << "the address of which the pointer points to ="<< ptr << endl ; cout<< "the address of the variable =" << &i<< endl; cout << "the address of the pointer ="<< &ptr << endl ; cout << "the value of the variable ="<< i << endl ; cout << "the value of the variable of which the pointer points to ="<< *ptr << endl; } the address of which the pointer points to =002AF360 the address of the variable =002AF360 the address of the pointer =002AF364 the value of the variable =100 the value of the variable of which the pointer points to =100 Press any key to continue . . . i ptr
  • 26. Pointers #include<iostream> #include<stdlib.h> usingnamespace::std; voidmain(void) { inti = 100; int* ptr; ptr = &i; if(ptr == NULL) { cout << "I'm NULL "<< endl; } else { cout << "I'm not a NULL! "<< endl; } } I'm not a NULL! Press any key to continue
  • 27. Pointers #include<iostream> #include<stdlib.h> usingnamespace::std; voidmain(void) { inti = 100; int* ptr; if(ptr == NULL) { cout << "I'm NULL "<< endl; } else { cout << "I'm not a NULL! "<< endl; } } I'm NULL Press any key to continue The pointer hasn’t been initialize yet so it’s usuallya NULL
  • 28. Pointers #include<iostream> #include<stdlib.h> usingnamespace::std; voidmain(void) { inti = 100; intj = 123; int* ptr; ptr = &i; ptr = &j; // dereferencing the pointer cout << &i << endl; cout << &j << endl; cout << ptr << endl; } 001DF2BC 001DF2C0 001DF2C0 Press any key to continue
  • 29. Pointers #include<iostream> #include<stdlib.h> usingnamespace::std; voidmain(void) { inti = 100; intj = 123; int* ptr; ptr = &i; ptr = &j; cout << &i << endl; cout << &j << endl; cout << ptr << endl; cout << &ptr << endl; } 0031EBBC 0031EBC0 0031EBC0 0031EBC4 Press any key to continue
  • 30. Pointers #include<iostream> #include<stdlib.h> usingnamespace::std; voidmain(void) { inti = 100; int* ptr = &i; ptr = NULL; if(ptr == NULL) { cout << "I'm NULL "<< endl; } else { cout << "I'm not a NULL! "<< endl; } cout << ptr << endl; cout << &ptr << endl; } I'm NULL 00000000 001FF284 Press any key to continue
  • 31. Pointers #include<iostream> #include<stdlib.h> usingnamespace::std; voidmain(void) { inti = 100; int* ptr; cout << *ptr << endl; } Runtime error It’s a run time error to print the value of a ptr that hasn’t has been initialized yet
  • 32. Pointers #include<iostream> #include<stdlib.h> usingnamespace::std; voidmain(void) { inti = 100; int* ptr = &i; ptr = NULL; if(ptr == NULL) { cout << "I'm NULL "<< endl; } else { cout << "I'm not a NULL! "<< endl; } cout << *ptr << endl; } Runtime error It’s a runtime error to print the value of a NULL ptr
  • 33. Pointers #include<iostream> #include<stdlib.h> usingnamespace::std; voidmain(void) { inti = 100; int* ptr = NULL; if(ptr == NULL) { cout << "I'm NULL "<< endl; } else { cout << "I'm not a NULL! "<< endl; } } I'm NULL Press any key to continue
  • 34. Pointers #include<iostream> #include<stdlib.h> usingnamespace::std; voidmain(void) { inti = 100; int* ptr; if(ptr = NULL) { cout << "I'm NULL "<< endl; } else { cout << "I'm not a NULL! "<< endl; } } I'm not a NULL! Press any key to continue Why?
  • 35. Pointers #include<iostream> #include<stdlib.h> usingnamespace::std; voidmain(void) { inti = 100; int* ptr = NULL; if(ptr = NULL) { cout << "I'm NULL "<< endl; } else { cout << "I'm not a NULL! "<< endl; } } I'm not a NULL! Press any key to continue Why?
  • 36. Pointers #include<iostream> #include<stdlib.h> usingnamespace::std; voidmain(void) { inti = 100; int* ptr = NULL; if(ptr = NULL) { cout << "I'm NULL "<< endl; } else { cout << "I'm not a NULL! "<< endl; } } I'm not a NULL! Press any key to continue Why?
  • 37. Pointers #include<iostream> #include<stdlib.h> usingnamespace::std; voidmain(void) { inti = 100; int* ptr = NULL; if(ptr = NULL) { cout << "I'm NULL "<< endl; } else { cout << "I'm not a NULL! "<< endl; } } I'm not a NULL! Press any key to continue Why?
  • 38. Pointers #include<iostream> #include<stdlib.h> usingnamespace::std; voidmain(void) { inti = 100; int* ptr = NULL; if(ptr = NULL) { cout << "I'm NULL "<< endl; } else { cout << "I'm not a NULL! "<< endl; } } I'm not a NULL! Press any key to continue Why?
  • 39. Pointers #include<iostream> #include<stdlib.h> usingnamespace::std; voidmain(void) { inti = 100; int* ptr = &i; *ptr = 300; cout << i << endl; cout << *ptr << endl; } #include<iostream> #include<stdlib.h> usingnamespace::std; voidmain(void) { inti = 100; int* ptr =&i; i = 400; cout << i << endl; cout << *ptr << endl; } 300 300 400 400
  • 41. Pointers •Rules for using pointers: –If the pointer is initialized •#1: make sure the value stored in pointer is a valid address before using it •#2: the type is the same between the address’s item and the pointer –If the pointer is not initialized •You can’t use “*” then.
  • 42. Pointers #include<iostream> usingnamespace::std; voidmain(void) { doublei; double*ptr; ptr =&i; } #include<iostream> usingnamespace::std; voidmain(void) { doublei; double*ptr; *ptr = 20; } #include<iostream> usingnamespace::std; voidmain(void) { doublei; int *ptr; ptr =&i; } voidmain(void) { doublei; double*ptr= &i; if(!ptr) { cout << “That's a bad address "<< endl; cout << "The address is "<< &ptr << endl; } else { cout << "That's a good address "<< endl; cout << "The address is "<< &ptr << endl; } } Compiler error, the pointer is on different type from the variable it’s pointing to That's a good address The address is 002DECE4 Compile & run Runtime error !ptr used for testing invalid address for ptr
  • 43. Pointers #include<iostream> usingnamespace::std; voidmain(void) { doublei; double*ptr= NULL; if(!ptr) { cout << “That's a bad address "<< endl; cout << "The address is "<< &ptr << endl; } else { cout << "That's a good address "<< endl; cout << "The address is "<< &ptr << endl; } } That's a bad address The address is 0020F0F4 Press any key to continue #include<iostream> usingnamespace::std; voidmain(void) { doublei; double*ptr; if(!ptr) { cout << “That's a bad address "<< endl; cout << "The address is "<< &ptr << endl; } else { cout << "That's a good address "<< endl; cout << "The address is "<< &ptr << endl; } } That's a good address The address is 0018EF74 Press any key to continue
  • 44. Pointers #include<iostream> usingnamespace::std; voidmain(void) { doublei; double*ptr; if(!ptr) { cout << “That's a bad address "<< endl; } else { cout << "That's a good address "<< endl; cout << "The address is "<< &ptr << endl; } } that's a bad address Press any key to continue #include<iostream> usingnamespace::std; voidmain(void) { floatx1 = 3; floatx2 = 4; float*ptr1, *ptr2; ptr1 = &x1; ptr2 = &x2; cout << “Code_1"<< endl; cout << x1 << " -"<< *ptr1 << endl; cout << x2 << " -"<< *ptr2 << endl; x1 = x1+2; x2 = *ptr1 * x2; cout << "Code_2"<< endl; cout << x1 << " -"<< *ptr1 << endl; cout << x2 << " -"<< *ptr2 << endl; } Code_1 3 -3 4 -4 Code_2 5 -5 20 –20
  • 45. Pointers p1 = i1;// *int!= int, not correct p1 = & i1;// *int = *int, correct p2 = & i2;// *int = *int, correct *p1 = i2 * 2;// int = int, correct *p2 = *p1 + 50;// int = int, correct cout << *p2 << endl;
  • 46. Pointers #include<iostream> usingnamespace::std; voidmain(void) { inti1=100, i2=300; int*p1,*p2; p1 = & i1;// *int = *int, correct p2 = & i2;// *int = *int, correct *p1 = i2 * 2; // int = int, correct *p2 = *p1 + 50; // int = int, correct cout << *p2 << endl; } 650
  • 47. Pointers #include<iostream> usingnamespace::std; voidmain(void) { chari1= 'c', i2= 's'; char*p1,*p2; p2 = &i1; i1 = 'd'; cout << *p2 << endl; } d #include<iostream> usingnamespace::std; voidmain(void) { chari1= 'c', i2= 's'; char*p1,*p2; p2 = &i1; i1 = 'd'; p1 = &i2; p2 = p1; cout << *p2 << endl; } s
  • 48. Pointers #include<iostream> usingnamespace::std; voidmain(void) { chari1= 'c', i2= 's'; char*p1,*p2; p2 = &i1; i1 = 'd'; p1 = &i2; p2 = p1; i2 = 'k'; cout << *p2 << endl; cout << *p1 << endl; } k k #include<iostream> usingnamespace::std; voidmain(void) { chari1= 'c', i2= 'j'; char*p1 = &i1, *p2 = &i2; p1 = p2; // not the same as *p1 = *p2 cout << p2 << endl; cout << p1 << endl; cout << *p1 << endl; cout << *p2 << endl; cout << "i1 = "<< i1 << endl; cout << "i2 = "<< i2 << endl; } j j j j i1 = c i2 = j Press any key to continue
  • 49. Pointers #include<iostream> usingnamespace::std; voidmain(void) { chari1= 'c', i2= 'j'; char*p1 = &i1, *p2 = &i2; *p1 = *p2;// not the same as p1 = p2 cout << p2 << endl; cout << p1 << endl; cout << *p1 << endl; cout << *p2 << endl; cout << "i1 = "<< i1 << endl; cout << "i2 = "<< i2 << endl; } j j j j i1 = j i2 = j Press any key to continue
  • 51. Pointers #include<iostream> usingnamespace::std; voidmain(void) { charstr1 [] = "GoGo"; char*str2 = "GoGo"; } G o G o 0 G o G o 0 str1 str2
  • 52. Pointers #include<iostream> usingnamespace::std; voidmain(void) { charstr1 [] = "GoGo1"; char*str2 = "GoGo2"; if(str1 == str2) { cout << "The same. "<< endl; } else { cout << "Not the same. "<< endl; } cout << str1 << endl; cout << str2 << endl; } Not the same. GoGo1 GoGo2 Press any key to continue #include<iostream> usingnamespace::std; voidmain(void) { charstr1 [] = "GoGo1"; char*str2 = "GoGo2"; if(str1 = str2) { cout << "The same. "<< endl; } else { cout << "Not the same. "<< endl; } cout << str1 << endl; cout << str2 << endl; } Can’t convert from char* to char[5]
  • 53. Pointers #include<iostream> usingnamespace::std; voidmain(void) { char*str1 = "GoGo1"; char*str2 = "GoGo2"; if(str1 == str2) { cout << "The same. "<< endl; } else { cout << "Not the same. "<< endl; } cout << str1 << endl; cout << str2 << endl; } Not the same. GoGo1 GoGo2 Press any key to continue #include<iostream> usingnamespace::std; voidmain(void) { char*str1 = "GoGo1"; char*str2 = "GoGo2"; if(str1 = str2) { cout << "The same. "<< endl; } else { cout << "Not the same. "<< endl; } cout << str1 << endl; cout << str2 << endl; } The same. GoGo2 GoGo2 Press any key to continue
  • 54. Pointers #include<iostream> usingnamespace::std; voidmain(void) { charstr1[] = "GoGo1"; charstr2[] = "GoGo2"; if(str1 == str2) { cout << "The same. "<< endl; } else { cout << "Not the same. "<< endl; } cout << str1 << endl; cout << str2 << endl; } Not the same. GoGo1 GoGo2 Press any key to continue #include<iostream> usingnamespace::std; voidmain(void) { charstr1[] = "GoGo1"; charstr2[] = "GoGo2"; str1 = str2; if(str1 == str2) { cout << "The same. "<< endl; } else { cout << "Not the same. "<< endl; } cout << str1 << endl; cout << str2 << endl; } Compiler error. Assigning memory locations (can’t do that for arrays! You can only dereference pointers!).
  • 55. Pointers #include<iostream> usingnamespace::std; voidmain(void) { charstr1[] = "GoGo1"; charstr2[] = "GoGo2"; if(str1 = str2) { cout << "The same. "<< endl; } else { cout << "Not the same. "<< endl; } cout << str1 << endl; cout << str2 << endl; } Compiler error #include<iostream> usingnamespace::std; voidmain(void) { charstr1[] = "GoGo1"; charstr2[] = "GoGo2"; str1 = "sdsd"; if(str1 == str2) { cout << "The same. "<< endl; } else { cout << "Not the same. "<< endl; } cout << str1 << endl; cout << str2 << endl; } Compiler error
  • 57. References •Alias –What does that means? •Creating another name for the variable, so that the Alias refers to the same memory address as does the original variable #include<iostream> usingnamespace::std; voidmain(void) { floatfoo; float&bar = foo; // must be initialized at definition time } Must be initialized at definition time
  • 58. References #include<iostream> usingnamespace::std; voidmain(void) { floatfoo; float&bar; bar = &foo; } Compiler error. Must be initialized when declared #include<iostream> usingnamespace::std; voidmain(void) { floatfoo; float&bar = foo; foo = 33; cout << bar << endl; cout << foo << endl; } 33 33
  • 59. References #include<iostream> usingnamespace::std; voidmain(void) { floatfoo; float&bar = foo; cout << bar << endl; cout << foo << endl; } 7.06997e+018 7.06997e+018 Press any key to continue #include<iostream> usingnamespace::std; voidmain(void) { floatfoo; float&bar = foo; floati =3; bar = i; cout << bar << endl; cout << foo << endl; } 3 3
  • 60. References #include<iostream> usingnamespace::std; voidmain(void) { floatfoo; float&bar = foo; floati =3; bar = &i; cout << bar << endl; cout << foo << endl; } Compiler error #include<iostream> usingnamespace::std; voidmain(void) { floatfoo; int&bar = foo; cout << bar << endl; cout << foo << endl; } Compiler error, types differ
  • 61. Passing Parameters to Functions-Very Important -
  • 62. Passing Parameters to Functions •By value •By reference –with reference parameters –with pointer parameters
  • 63. Passing Parameters to Functions #include<iostream> usingnamespace::std; voidBadSwap (inta, intb) { inttemp; temp = a; a = b; b = temp; } voidmain(void) { inta = 2; intb = 3; cout << "Before Bad Swap:"<< endl; cout << "a is = "<< a << endl; cout << "b is = "<< b << endl; BadSwap(a,b); cout << "After Bad Swap:"<< endl; cout << "a becomes = "<< a << endl; cout << "b becomes = "<< b << endl; } Before Bad Swap: a is = 2 b is = 3 After Bad Swap: a becomes = 2 b becomes = 3 #include<iostream> usingnamespace::std; voidSwap (int&a, int&b) { inttemp; temp = a; a = b; b = temp; } voidmain(void) { inta = 2; intb = 3; cout << "Before Swap:"<< endl; cout << "a is = "<< a << endl; cout << "b is = "<< b << endl; Swap(a,b); cout << "After Swap:"<< endl; cout << "a becomes = "<< a << endl; cout << "b becomes = "<< b << endl; } Before Swap: a is = 2 b is = 3 After Swap: a becomes = 3 b becomes = 2 Passing by value Passing by reference
  • 64. Passing Parameters to Functions #include<iostream> usingnamespace::std; voidSwap (int*a, int*b) { inttemp; temp = *a; *a = *b; *b = temp; } voidmain(void) { int*a; int*b; *a = 3; *b = 5; Swap(a,b); cout << *a << endl; cout << *b << endl; } Compile but runtimer error. The is because the pointers hasn’t been initialized (without new) #include<iostream> usingnamespace::std; voidSwap (int*a, int*b) { inttemp; temp = *a; a = b; *b = temp; } voidmain(void) { int*a = newint; int*b = newint; *a = 3; *b = 5; Swap(a,b); cout << *a << endl; cout << *b << endl; } 3 3
  • 65. Passing Parameters to Functions #include<iostream> usingnamespace::std; voidSwap (int*a, int*b) { int*temp = newint; *temp = *a; *a = *b; *b = *temp; } voidmain(void) { inta = 2; intb = 3; cout << "Before Swap:"<< endl; cout << "a is = "<< a << endl; cout << "b is = "<< b << endl; Swap(&a,&b); cout << "After Swap:"<< endl; cout << "a becomes = "<< a << endl; cout << "b becomes = "<< b << endl; } Before Swap: a is = 2 b is = 3 After Swap: a becomes = 3 b becomes = 2 #include<iostream> usingnamespace::std; voidSwap (int*a, int*b) { int*temp = newint; *temp = *a; *a = *b; *b = *temp; } voidmain(void) { inta = 2; intb = 3; cout << "Before Bad Swap:"<< endl; cout << "a is = "<< a << endl; cout << "b is = "<< b << endl; Swap(a,b); cout << "After Bad Swap:"<< endl; cout << "a becomes = "<< a << endl; cout << "b becomes = "<< b << endl; } Compiler error. Can’t convert int to *int
  • 66. Passing Parameters to Functions #include<iostream> usingnamespace::std; voidSwap (int*a, int*b) { int*temp = newint; temp = a; a = b; b = temp; } voidmain(void) { inta = 2; intb = 3; cout << "Before Bad Swap:"<< endl; cout << "a is = "<< a << endl; cout << "b is = "<< b << endl; Swap(&a,&b); cout << "After Bad Swap:"<< endl; cout << "a becomes = "<< a << endl; cout << "b becomes = "<< b << endl; } Before Bad Swap: a is = 2 b is = 3 After Bad Swap: a becomes = 2 b becomes = 3 Coz the pointers are returned to their original pointing locations after the method is done!
  • 67. Passing Parameters to Functions #include<iostream> usingnamespace::std; voidSwap (int*a, int*b) { inttemp; temp = *a; *a = *b; *b = temp; } voidmain(void) { int*a = newint; int*b = newint; *a = 3; *b = 5; cout << "Before Swap:"<< endl; cout << "a is = "<< *a << endl; cout << "b is = "<< *b << endl; Swap(a,b); cout << "After Swap:"<< endl; cout << "a becomes = "<< *a << endl; cout << "b becomes = "<< *b << endl; } Before Swap: a is = 3 b is = 5 After Swap: a becomes = 5 b becomes = 3 #include<iostream> usingnamespace::std; voidSwap (int*a, int*b) { inttemp; temp = *a; *a = *b; *b = temp; } voidmain(void) { int*a = newint; int*b = newint; *a = 3; *b = 5; Swap(&a,&b); cout << *a << endl; cout << *b << endl; } Compiler error Can’t convert **int to *int
  • 68. Passing Parameters to Functions #include<iostream> usingnamespace::std; int* f () { intc = 2; cout << "the address of c in f fuction "<< &c << endl; return(&c); } voidWrong() { int* p; cout << "The address of the p before calling f is = "<< p << endl; p = f(); cout << "The address of the p after calling f is = "<< p << endl; cout << "The value of the p is now!!! = "<< *p << endl; } voidmain(void) {Wrong();} The address of the p before calling f is = 00000000 the address of c in f fuction 0014F190 The address of the p after calling f is = 0014F190 The value of the p is now!!! = 1372776 Press any key to continue Function must not return a pointerto a localvariable in the function
  • 69. Passing Parameters to Functions #include<iostream> usingnamespace::std; intf () { intc = 2; cout << "the address of c in f fuction "<< &c << endl; return(c); } voidWrong() { int* p = newint; cout << "The address of the p before calling f is = "<< p << endl; *p = f(); cout << "The address of the p after calling f is = "<< p << endl; cout << "The value of the p is now!!! = "<< *p << endl; } voidmain(void) {Wrong();} The address of the p before calling f is = 00311DE0 the address of c in f fuction 0028EEF8 The address of the p after calling f is = 00311DE0 The value of the p is now!!! = 2 Press any key to continue
  • 70. Passing Parameters to Functions voidswap1 (intx, inty) { inttemp = x; x = y; y = temp;} voidswap2 (int*x, int*y) { inttemp = *x; *x = *y; *y = temp;} voidswap3 (int&x, int&y ) { inttemp = x; x = y; y = temp; } voidmain(void) { inti1 = 5; inti2 = 7; //How should we call //the function } void main(void) { inti1 = 5; inti2 = 7; swap1(i1,i2); swap2(&i1,&i2); swap3(i1,i2); } #include<iostream> usingnamespace::std; voidswap1 (intx, inty){ inttemp = x; x = y; y = temp;} voidswap2 (int*x, int*y){ inttemp = *x; *x = *y; *y = temp;} voidswap3 (int&x, int&y ) { inttemp = x; x = y;y = temp; } voidmain(void) {inti1 = 5, i2 = 7; swap1(i1,i2); cout << "After 1st sawp function "<< endl; cout << "i1 = "<< i1 <<endl; cout << "i2 = "<< i2 << endl; swap2(&i1,&i2); cout << "After 2nd sawp function "<< endl; cout << "i1 = "<<i1 <<endl; cout << "i2 = "<< i2 << endl; swap3(i1,i2); cout << "After 3rd sawp function "<< endl; cout << "i1 = "<< i1 <<endl; cout << "i2 = "<< i2 << endl;} After 1st sawp function i1 = 5 i2 = 7 After 2nd sawp function i1 = 7 i2 = 5 After 3rd sawp function i1 = 5 i2 = 7
  • 72. Dynamic Memory Allocation •What Dynamic means? •What Memory Allocation means? •What Dynamic Memory Allocation means?
  • 73. Dynamic Memory Allocation •When Dynamic Memory Allocation is used? •Array that its extent is not known till Runtime •Objects that needs to be created but not known till Runtime •What Dynamic Memory Allocation do? –Allocate and de-allocate memory at run time depending on the information at running time •new delete keywords (Dynamic)
  • 74. Dynamic Memory Allocation •Static Memory Allocation –Allocated at compile time •Dynamic Memory Allocation –Allocated at Run time
  • 75. Dynamic Memory Allocation Pointers and Arrays
  • 76. Pointers and Arrays #include<iostream> usingnamespace::std; voidmain(void) { inta[200]; // Static, Allocate at compile time } #include<iostream> usingnamespace::std; voidmain(void) { intArrLength; int*ptr; cin >> ArrLength; ptr = newint[ArrLength]; // Dynamic, allocate at Runtime } The name of “Array” is the address of the its first element in memory
  • 77. Pointers and Arrays #include<iostream> usingnamespace::std; voidmain(void) { int*p1, *p2; p1 = newint;// allocate but not initialized yet p2 = newint; // allocate but not initialized yet } p1 p2 -842150451 546546546
  • 78. Pointers and Arrays #include<iostream> usingnamespace::std; voidmain(void) { int*p1, *p2; p1 = newint;// allocate but not initialized yet p2 = newint(5);// allocate and initialized cout << *p1 << endl; cout << *p2 << endl; } 5 p1 p2 -842150451 5
  • 79. Pointers and Arrays #include<iostream> usingnamespace::std; voidmain(void) { int*p1, *p2; p1 = newint; p2 = newint[5]; cout << *p1 << endl; cout << *p2 << endl; } p1 p2 -842150451 -842150451
  • 80. Pointers and Arrays #include<iostream> usingnamespace::std; voidmain(void) { int*p1; p1 = newint[5]; *p1 = 2; *(p1+1) = 3; cout << *p1 << endl; cout << *(p1+1) << endl; } 2 3 #include<iostream> usingnamespace::std; voidmain(void) { int*p1; p1 = newint[5]; *p1 = 2; *(p1+1) = 3; cout << *p1 << endl; cout << *(p1+1) << endl; cout << *(p1+2) << endl; } 2 3 -842150451 Press any key to continue
  • 81. Pointers and Arrays #include<iostream> usingnamespace::std; voidmain(void) { int*p1; p1 = newint[5]; *p1 = 2; *(p1+1) = 4; cout << *p1 << endl; cout << *(p1+1) << endl; cout << *p1+1 << endl; // again, the array name is the address // of its first element in memory } 2 4 3 Press any key to continue #include<iostream> usingnamespace::std; voidmain(void) { int*p1; p1 = newint[5]; *p1 = 2; *p1+1 = 4; } Compiler error
  • 82. Pointers and Arrays #include<iostream> usingnamespace::std; voidmain(void) { int*p1; p1 = newint[5]; *p1 = 2; *(p1++) = 4; cout << *p1 << endl; cout << *(p1+1) << endl; cout << *(p1-1) << endl; } -842150451 -842150451 4 Press any key to continue #include<iostream> usingnamespace::std; voidmain(void) { int*p1; p1 = newint[5]; *p1 = 2; cout << *p1 << endl; cout << p1 << endl; deletep1; cout << *p1 << endl; cout << p1 << endl; } 2 006D1848 -572662307 006D1848 Press any key to continue
  • 83. Pointers and Arrays #include<iostream> usingnamespace::std; voidmain(void) { int*p1; p1 = newint[5]; *p1 = 2; cout << *p1 << endl; cout << p1 << endl; deletep1; cout << *p1 << endl; cout << p1 << endl; *p1 = 3; // Re-allocating without new!!!!!, it works!!! cout << *p1 << endl; cout << p1 << endl; } 2 00061848 -572662307 00061848 3 00061848
  • 84. Pointers and Arrays •Now, Memory is allocated –But not initialized #include<iostream> usingnamespace::std; voidmain(void) { intArr[10]; } ? ? ? ? ? ? ? ? ? ?
  • 85. Pointers and Arrays •Now, Memory is allocated –But not initialized ? ? ? ? ? ? ? ? ? 2 #include<iostream> usingnamespace::std; voidmain(void) { intArr[10]; Arr[9] = 2; cout << Arr[9] << endl; cout << Arr[2] << endl; }
  • 86. Pointers and Arrays •In C++, the array is really just a pointer to its first element #include<iostream> usingnamespace::std; voidmain(void) { intArr[10]; } Arr
  • 87. Pointers and Arrays #include<iostream> usingnamespace::std; voidmain(void) { intArr[8]= {1,2,4,78,9,2,7,3}; if(Arr == &Arr[0]) { cout << "This will ALWAYS be true!"<< endl; } } This will ALWAYS be true! Press any key to continue
  • 88. Pointers and Arrays Arr[0] *Arr #include<iostream> usingnamespace::std; voidmain(void) { intArr[8]; cout << Arr[0] << endl; cout << *Arr << endl; cout << *(&Arr[0]) << endl; } 1306992 1306992 1306992
  • 89. Pointers and Arrays #include<iostream> usingnamespace::std; voidmain(void) { intArr[] = {1,2,3}; int*ArrPtr = Arr; ArrPtr++; ArrPtr++; ArrPtr-=2; cout << *(ArrPtr++) << endl; ArrPtr--; ArrPtr = Arr; ArrPtr += 2; cout << ArrPtr -Arr << endl; cout << *ArrPtr << endl; cout << *Arr << endl; } 1 2 3 1
  • 90. Pointers and Arrays #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { intsum = 0; intArr[ArrSize] = {1,3}; for(inti=0; i<ArrSize; i++) { sum += Arr[i]; } cout << "The sum is = "<< sum << endl; } 4 #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { intsum = 0; intArr[ArrSize] = {1,3}; int*ptr; for(ptr = Arr; ptr < Arr+ArrSize-1; ++ptr) { sum += *ptr; } cout << "The sum is = "<< sum << endl; } 4
  • 91. Pointers and Arrays #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { intsum = 0; intArr[ArrSize] = {1,3}; int*ptr; for(ptr = Arr; ptr < &Arr[ArrSize]; ++ptr) { sum += *ptr; } cout << "The sum is = "<< sum << endl; } 4
  • 92. Pointers and Arrays #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { intsum = 0; intArr[ArrSize] = {1,3}; int* ptr = Arr; inti = 2; cout << ptr[1] << endl; cout << ptr[2] << endl; } 3 0 Press any key to continue #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { intArr[10]; for(inti = 0; i< 10; i++) { Arr[i] = i; } int* p = Arr; cout << *p << endl; cout << *p+1 << endl; cout << *p++ << endl; cout << *++p << endl; cout << *(p+1) << endl; cout << *(p+5) << endl; } 0 1 0 2 3 7
  • 93. Pointers and Arrays #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { intArr[10]; int*Iter = &Arr[0]; int*Iter_End = &Arr[10]; inti = 0; while(Iter!= Iter_End) { cout << "We're in elemet #"<< i << ", with value = "<< *Iter << endl; ++Iter; i++; } } We're in elemet #0, with value = 2429382 We're in elemet #1, with value = 1371168 We're in elemet #2, with value = 1518572499 We're in elemet #3, with value = 1 We're in elemet #4, with value = 3336456 We're in elemet #5, with value = 1371200 We're in elemet #6, with value = 2430657 We're in elemet #7, with value = 1371888 We're in elemet #8, with value = 1371336 We're in elemet #9, with value = 3336456 #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { intArr[10] = {}; int*Iter = &Arr[0]; int*Iter_End = &Arr[10]; inti = 0; while(Iter!= Iter_End) { cout << "We're in elemet #"<< i << ", with value = "<< *Iter << endl; ++Iter; i++; } } We're in elemet #0, with value = 0 We're in elemet #1, with value = 0 We're in elemet #2, with value = 0 We're in elemet #3, with value = 0 We're in elemet #4, with value = 0 We're in elemet #5, with value = 0 We're in elemet #6, with value = 0 We're in elemet #7, with value = 0 We're in elemet #8, with value = 0 We're in elemet #9, with value = 0
  • 94. Pointers and Arrays •Dynamic arrays –Size is not specified at compile time but at Run time! •By using new #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { int*ptr = newint[4]; // allocating 4 interger elements }
  • 95. Pointers and Arrays #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { int*ptr = newint[]; cout << *ptr << endl; cout << *(ptr+3) << endl; } -33686019 -1962934272 Press any key to continue #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { int*ptr = newint[]; cout << *ptr << endl; cout << *(ptr+3) << endl; delete[] ptr; cout << *ptr << endl; } -33686019 -1962934272 -572662307 Press any key to continue
  • 96. Pointers and Arrays #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { int*ptr = newint[6]; cout << *ptr << endl; cout << *(ptr+3) << endl; delete[] ptr; cout << *ptr << endl; } -842150451 -842150451 -572662307 Press any key to continue #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { int*ptr = newint[]; cout << *ptr << endl; cout << *(ptr+3) << endl; deleteptr []; cout << *ptr << endl; } Compiler error The syntax for deleting isdelete[] ptr;
  • 97. Pointers and Arrays #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { float* pArr; intLength; cin >> Length;// 5 pArr = newfloat[Length]; for(inti= 0; i < Length; i++ ) { pArr[i] = i; } cout << "The array is: "<< endl; for(inti= 0; i < Length; i++ ) { cout << pArr[i] << endl; } } 5 The array is: 0 1 2 3 4 #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { float* pArr; intLength; cin >> Length;// 5 pArr = newfloat[Length]; for(float i= 0; i < Length; i++ ) { pArr[i] = i; } cout << "The array is: "<< endl; for(inti= 0; i < Length; i++ ) { cout << pArr[i] << endl; } } Compiler error Counter must be an integral type 0 –0x000441C40 2 –0x000441C48 3 –0x000441C50 4 –0x000441C58
  • 99. Pointers and Strings #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { charKoKo[] = "Hello!"; // defining a string char*Ptr = KoKo; *Ptr = 'C'; *Ptr++ = 'V'; *Ptr = 'B'; cout << KoKo << endl; } VBllo! Press any key to continue #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { char KoKo[] = "Hello!"; // defining a string char *Ptr = KoKo; *Ptr = 'C'; *Ptr++ = "V"; *Ptr = 'B'; cout << KoKo << endl; } Compiler error, ””
  • 100. Pointers and Strings #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { char*p = newchar[10]; cout << *(p+9) << endl; } =
  • 101. Pointers and Strings #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { char*p = newchar[10]; cin >> p; cout << p << endl; cout << *(p+9) << endl; } 123456789 123456789 Press any key to continue #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { char*p = newchar[10]; cin >> p; cout << p << endl; cout << *(p+9) << endl; } 32423 32423 ═ Press any key to continue
  • 102. Pointers and Strings #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { char*p = newchar[10]; cin >> p; cout << p << endl; cout << *(p+9) << endl; } 123456789123456789 123456789123456789 1 Press any key to continue #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { char*p = newchar[10]; cin >> p; p[9]='0';// now used to cut down the string // heheheeee!!!:D cout << p << endl; cout << *(p+9) << endl; } 213124234324234 213124234 Press any key to continue
  • 103. Pointers and Strings #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { char*p = newchar[10]; cin >> p; p[9]='0';// now used to cut down the string // heheheeee!!!:D cout << p << endl; cout << *(p+9) << endl; } 234234 234234 Press any key to continue #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { char*p = "COOL MAN!"; cout << p << endl; cout << *p << endl; cout << &*p << endl; cout << *&p << endl; cout << *(p+3) << endl; } COOL MAN! C COOL MAN! COOL MAN! L
  • 105. Pointers and Multi-dimensional Arrays Z e Z e Z o o Z o o M o M o o #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { char* WoW[4] = {"MoMoo", "MeMe", “ZeZe", "ZooZoo"}; } What does that means?! M e M e
  • 106. Pointers and Multi-dimensional Arrays #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { char* WoW[4] = {"MoMoo", "MeMe", “ZeZe", "ZooZoo"}; } #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { char* WoW[] = {"MoMoo","MeMe", “ZeZe", "ZooZoo"}; } Z e Z e Z o o Z o o M o M o o Both are the same M e M e Size of the arrays is determined at compile time
  • 107. Pointers and Multi-dimensional Arrays What’s the difference?! int int int ThatIsCrazy1 int ThatIsCrazy2 int int int int #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { int*ThatIsCrazy1 [4]; int(*ThatIsCrazy2) [4]; }
  • 108. Pointers and Multi-dimensional Arrays What’s the difference?! int int int ThatIsCrazy1 int ThatIsCrazy2 #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { int*ThatIsCrazy1 [4]; int*(ThatIsCrazy2 [4]); } int int int int #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { int*ThatIsCrazy1 [4]; int(*ThatIsCrazy2) [4]; }
  • 110. constPointers •What does constfor pointers means? –ptris a pointer which points to a constinteger? –OR, ptris a pointer (not a constpointer)? #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { constint*ptr; }
  • 111. Pointers and Strings #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { inti = 2; constint*ptr; ptr = &i;// changing the pointer } Compile and run #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { inti = 2; constint*ptr; ptr = &i; *ptr = 3; } Compiler error Coz the thing which the pointer points to is const and shouldn’t be modified!
  • 112. Pointers and Strings #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { inti = 2; constint*ptr; ptr = &i; cout << *ptr << endl; } 2 #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { inti; int*constptr =&i; } What does this means? Ptr is a pointer which is const which points to integer
  • 113. Pointers and Strings #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { inti; int*constptr; } Compiler error #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { inti,j; int*constptr =&i; ptr = &j; } Compiler error
  • 114. Pointers and Strings #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { inti=2, j; int*constptr =&i; cout << *ptr << endl; } 2 #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { inti, j; int*constptr =&i; cout << *ptr << endl; } 2458743545
  • 115. constPointers •Pointer to const –constint*ptr; –intconst*ptr; •constpointer –int*constptr;
  • 116. constPointers •Const pointer to const #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { inti = 3; constint*ptr1;// pointer to const intconst*ptr2;// pointer to const int*constptr3 = &i;// const pointer constint*constptr4 = &i;// const pointer to const intconst*constptr5 = &i;// const pointer to const }
  • 117. Pointers and Strings #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { inti = 3; constint*ptr1;// pointer to const intconst*ptr2;// pointer to const int*constptr3;// const pointer constint*constptr4;//const pointer to const intconst*constptr5;//const pointer to const } Compiler error for: ptr3 ptr4 ptr5 #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { inti = 3, j = 4; // const pointer to const constint*constptr1 = &i; // const pointer to const intconst*constptr3 = &i; // attempting to dereferance // and change pointer ptr1 = &j; } Compiler error
  • 118. Pointers and Strings #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { inti = 3, j = 4; // const pointer to const constint*constptr1 = &i; // const pointer to const intconst*constptr3 = &i; *ptr1 = 3; // change the int } Compiler error #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { inti = 3, j = 4; // const pointer to const constint*constptr1 = &i; // const pointer to const intconst*constptr3 = &i; // Attempting to dereferance // and change the pointer ptr1 = &j; } Compiler error
  • 120. Pointers to Pointers #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { charc = 'c'; char*ptr; char**pptr; c = 'b'; ptr = &c; pptr = &ptr; cout << c << endl; cout << *ptr << endl; cout << ptr << endl; cout << &c << endl; cout << pptr << endl; cout << &ptr << endl; cout << &pptr << endl; } b b b b 0028EF54 0028EF54 0028EF4C ‘b’ pptr ptr c
  • 122. voidPointers #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) {int*iptr; void*vptr; inti = 3; floatf = 4; iptr = &i; //iptr = &f; // error vptr = &i; cout << *vptr << endl; cout << &vptr << endl; cout << vptr << endl; vptr = &f; cout << *vptr << endl; cout << &vptr << endl; cout << vptr << endl; } Compiler error. Why? #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { int*iptr; void*vptr; inti = 3; floatf = 4; iptr = &i; vptr = &i; cout << (*((int*)vptr)) << endl; cout << &vptr << endl; cout << vptr << endl; vptr = &f; cout << (*((float*)vptr)) << endl; cout << &vptr << endl; cout << vptr << endl; } 3 0016EEC4 0016EEB8 4 0016EEC4 0016EEBC
  • 124. Returning Pointers #include<iostream> usingnamespace::std; int[] GoToFun(int A[2]) { cout << A[0] << endl; A[0] = 1; returnA; } voidmain(void) { intArr[] = {3,4}; GoToFun(Arr); } Compiler error. Arrays can’t be used as a return type of functions. So, how can we do it?!?!!! #include<iostream> usingnamespace::std; int* GoToFun(int*p1) { cout << p1[0] << endl; p1[0] = 1; cout << p1[0] << endl; returnp1; } voidmain(void) { intArr[] = {3,4}; int*ptr = Arr; cout << "In main, "<< *ptr << endl; GoToFun(ptr); cout << "In main, "<< *ptr << endl; } In main, 3 3 1 In main, 1
  • 125. Returning Pointers #include<iostream> usingnamespace::std; int* GoToFun(int*p1) { cout << p1[0] << endl; p1[0] = 1; cout << p1[0] << endl; returnp1; } voidmain(void) { intArr[] = {3,4}; int*ptr = &Arr; cout << "In main, "<< *ptr << endl; GoToFun(ptr); cout << "In main, "<< *ptr << endl; } Error1error C2440: 'initializing': cannot convert from 'int (*)[2]' to 'int *' c:UsersZGTRDocumentsVisual Studio 2008ProjectsTempC++File++TempC++File++MyFile.cpp16TempC++File++ #include<iostream> usingnamespace::std; int& Reproducer(intA[], intindex) { returnA[index]; } voidmain(void) { inti = 5; intArr[] = {3,4,7}; cout << "Before"<< endl; for(i = 0; i < 3;++i) { cout << Arr[i] << endl;;;;; } Reproducer(Arr,1) = -10; cout << "After"<< endl; for(i = 0; i < 3;++i) { cout << Arr[i] << endl;;;;; } } Before 3 4 7 After 3 -10 7
  • 126. Returning Pointers #include<iostream> usingnamespace::std; int& Reproducer(intA[], intindex) { return&A[index]; } voidmain(void) { inti = 5; intArr[] = {3,4,7}; cout << "Before"<< endl; for(i = 0; i < 3;++i) { cout << Arr[i] << endl;;;;; } Reproducer(Arr,1) = -10; cout << "After"<< endl; for(i = 0; i < 3;++i) { cout << Arr[i] << endl;;;;; } } Compiler error. &A[index];
  • 128. Memory Problems •Memory leak –Loss of available memory space –Occurs when the dynamic memory space has been allocated but never de-allocated •Dangling pointer –A pointer that points to dynamic memory that has been de-allocated
  • 129. Memory Problems -LEAK •Now 4 is allocated but can never been reached or used #include<iostream> usingnamespace::std; voidmain(void) { int*p; p = newint; *p = 4; p = newint; *p = 5; } 4 5 p1 4 p1
  • 130. Memory Problems -LEAK •Now 6 is allocated but can never been reached or used 00401DE0 -4 p1 address 0028EEA4 00401DE0 -4 p2 address 0028EEA0 #include<iostream> usingnamespace::std; voidmain(void) { int*p1; p1 = newint(5); *p1 = 4; int*p2 = newint(6); p2 = p1; cout << p1 << " -"<< *p1 << endl; cout << "p1 address "<< &p1 << endl; cout << p2 << " -"<< *p2 << endl; cout << "p2 address "<< &p2 << endl; } 5 6 p1 5 p1 6 p2 p2
  • 131. Memory Problems -Dangling #include<iostream> usingnamespace::std; voidmain(void) { int*p1; p1 = newint(5); int*p2 = newint(6); p2 = p1; deletep1; cout << p1 << " -"<< *p1 << endl; cout << "p1 address "<< &p1 << endl; cout << p2 << " -"<< *p2 << endl; cout << "p2 address "<< &p2 << endl; } 6 p1 5 p1 6 p2 p2 00271DE0 --33686272 p1 address 0025F064 00271DE0 --33686272 p2 address 0025F060
  • 133. Code Cracking #include<iostream> usingnamespace::std; voidmain(void) { int*p; if(1) { intx= 343299; *p = x; } cout << *p << endl; } Compile but runtime error #include<iostream> usingnamespace::std; int*p; voidSthWrong() { intb = 3; p = &b; } voidmain(void) { cout << p << endl; cout << &p << endl; SthWrong(); cout << p << endl; cout << &p << endl; cout << *p << endl; } 00000000 00300974 0019EE68 00300974 1699624
  • 134. Code Cracking #include<iostream> usingnamespace::std; int*p; voidSthWrong() { intb = 3; *p = b; } voidmain(void) { cout << p << endl; cout << &p << endl; SthWrong(); cout << p << endl; cout << &p << endl; cout << *p << endl; } Compile but runtime error Missing new #include<iostream> usingnamespace::std; int*p; voidSthWrong() { intb = 3; *p = b; } voidmain(void) { p = newint; cout << p << endl; cout << &p << endl; SthWrong(); cout << p << endl; cout << &p << endl; cout << *p << endl; } 005B1DE0 00310978 005B1DE0 00310978 3
  • 135. Code Cracking #include<iostream> #include<stdlib.h> usingnamespace::std; voidmain(void) { inti = 100; int* ptr = NULL; if(ptr = NULL) { cout << "I'm NULL "<< endl; } else { cout << "I'm not a NULL! "<< endl; } } I'm not a NULL! Press any key to continue #include<iostream> usingnamespace::std; voidmain(void) { inti = 100; int*ptr =&i; *ptr = int(&i); cout << *ptr << endl; cout << &i << endl; } 2223152 0021EC30 Press any key to continue
  • 136. Code Cracking #include<iostream> usingnamespace::std; voidmain(void) { inti = 100; int*ptr; *ptr = &i; } Compiler error, can’t convert from *int to int
  • 137. Ever dreamed to pass a functionas a parameter?
  • 140. Function PointersAllows operations with pointers to functionsFunction pointers contains the address of the function in memory
  • 141. Function Pointers #include<iostream> usingnamespace::std; intAdd(inta, intb ){returna+b; } intSub(inta, intb ){returna-b; } int(*AddPtr) (int,int) = Add; int(*SubPtr) (int,int) = Sub; intOperationToDo (intx, inty, int(*func) (int,int)) {intResult; Result = (*func)(x,y); returnResult;} voidmain(void) {inta=3, b=4; intResult; Result = OperationToDo(a,b,AddPtr); cout << "a="<< a << endl; cout << "b="<< b << endl; cout << "Result ="<< Result << endl; Result = OperationToDo(b,a,SubPtr); cout << "a="<< a << endl; cout << "b="<< b << endl; cout << "Result ="<< Result << endl; } a=3 b=4 Result =7 a=3 b=4 Result =1
  • 142. Quiz
  • 143. Quiz 1, 2 #include<iostream> usingnamespace::std; voidmain(void) { chari1=100, i2=300; char*p1,*p2; p2 = &i1; cout << *p2 << endl; } d #include<iostream> usingnamespace::std; voidmain(void) { int*p1; p1 = newint[5]; *p1 = 2; *(++p1) = 4; cout << *p1 << endl; cout << *(p1+1) << endl; cout << *(p1-1) << endl; } 4 -842150451 2 Press any key to continue
  • 144. Quiz 3, 4 #include<iostream> usingnamespace::std; voidmain(void) { inti = 100; int*ptr; *ptr = 100; cout << ptr << endl; cout << *ptr << endl; } Runtime error. Printing *ptr without initializing it first #include<iostream> usingnamespace::std; int*p; voidSthWrong() { intb = 3; *p = b; } voidmain(void) { p = newint; cout << p << endl; cout << &p << endl; SthWrong(); cout << p << endl; cout << &p << endl; cout << *p << endl; } 005B1DE0 00310978 005B1DE0 00310978 3
  • 145. Quiz 5, 6 #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { intArr[10]; for(inti = 0; i< 10; i++) { Arr[i] = 2*i; } int* p = Arr; cout << *p << endl; cout << *p+1 << endl; cout << *p++ << endl; cout << *++p << endl; cout << *(p+1) << endl; cout << *(p+5) << endl; } 0 1 0 4 6 14 #include<iostream> usingnamespace::std; constintArrSize = 10; voidmain(void) { charKoKo[] = "WOWAIOE"; // defining a string char*Ptr = KoKo; *Ptr = 'C'; *Ptr++ = 'K'; *Ptr = 'B'; cout << KoKo << endl; } KBWAIOE Press any key to continue