You are on page 1of 26

CS250 - Data Structures and

Algorithm
BESE-2A/B

Lecture 02
Aasma Zahid
Outline
• Pointer to variable
• Pointers and Copy Constructors
• Function call by value
• Function call by reference
• Pointer to Function
• Pointer to UDTs

9/13/2012 DSA - Fall 2012 - SEECS, NUST 2


Pointer to Variable
• Variable
– Container that holds specific data value
• Pointer
– Container that holds reference to specific data value

9/13/2012 DSA - Fall 2012 - SEECS, NUST 3


Pointer to Variable
• Pointer Dereference
– Operation: get the value of pointee using asterisk
– Restriction: Pointer must have correct pointee

• Null Pointer
– Points to nothing
– Language specific value
• Usually zero

9/13/2012 DSA - Fall 2012 - SEECS, NUST 4


Pointer to Variable
• Pointer Assignment
– Operation: between two
pointers makes them point
to the same pointee

second = numPtr;

• Comparison (==)
– if(second == numPtr) returns true
– Both points to same pointee

• Bad Pointers
– Uninitialized
– Dereferencing bad pointer
• Lucky: if you get runtime error
• Unlucky: if it corrupts random area of memory leading to malfunctioned
program execution later
9/13/2012 DSA - Fall 2012 - SEECS, NUST 5
Example
void PointerTest() {
int a = 1;
int b = 2;
int c = 3;
int* p;
int* q;

p = &a; // set p to refer to a


q = &b; // set q to refer to b

c = *p; //retrieve p's pointee value (1) and put it in c


p = q; //change p to share with q (p's pointee is now b)
*p = 13; // dereference p to set its pointee (b) to 13 (*q is
now 13)
}

9/13/2012 DSA - Fall 2012 - SEECS, NUST 6


SHALLOW /DEEP COPY

9/13/2012 DSA - Fall 2012 - SEECS, NUST 7


Shallow / Deep Copy

9/13/2012 DSA - Fall 2012 - SEECS, NUST 8


Shallow / Deep Copy
struct Node{
char *name;
int age;
Node(char * n=“”, int a=0) {
name = strdup(n);
strcpy(name, n);
age = a;
}

Node node1(“Roger”, 20);


Node node2(node1);

strcpy(node2.name, “Wendy”);
node2.age = 30;

9/13/2012 DSA - Fall 2012 - SEECS, NUST 9


Shallow / Deep Copy
struct Node{
char *name;
int age;
Node(char * n=“”, int a=0) {
name = strdup(n);
strcpy(name, n);
age = a;
}

Node(const Node &n){


name = strdup(n.name);
age = n.age;
}

9/13/2012 DSA - Fall 2012 - SEECS, NUST 10


Shallow / Deep Copy
• Same problem arises in case of assignment operator
i.e. member by member copy
n2 = n1
• Solution
– Overload assignment operator

Node& operator =(const Node &n) {


if(this != n) {
delete [] name;

name = strdup(n.name);
age = n.age;
}
return * this;
}
9/13/2012 DSA - Fall 2012 - SEECS, NUST 11
FUNCTION CALL BY VALUE AND BY
REFERENCE

9/13/2012 DSA - Fall 2012 - SEECS, NUST 12


Function Call by Value
1 /*
2 Cube a variable using call-by-value
3 */
4
5 #include <stdio.h>
6
7 int cubeByValue( int ); /* prototype */
8
9 int main()
10 {
11 int number = 5;
12
13 printf( "The original value of number is %d", number );
14 number = cubeByValue( number );
15 printf( "\nThe new value of number is %d\n", number );
16
17 return 0;
18 }
19
20 int cubeByValue( int n )
21 {
22 return n * n * n; /* cube number in main */
23 }

9/13/2012 DSA - Fall 2012 - SEECS, NUST 13


Function Call by Value
int main(){ int cubeByValue( int n ) { n
int number = 5; number
return n * n * n;
number = cubeByValue( number ); } undefined
} 5

int main(){ int cubeByValue( int n ){ n


number
int number = 5; return n * n * n;
number = cubeByValue( number ); 5
5 }
}

int cubeByValue( int n ){ n


int main(){ number return n * n * n;
int number = 5; 5
number = cubeByValue( number ); 5 } 125
}
int main(){ int cubeByValue( int n ) {
int number = 5; number n
number = cubeByValue( number ); return n * n * n;
undefined
} 5 }

int main() { int cubeByValue( int n ){


int number = 5; number
number = cubeByValue( number ); return n * n * n; n
} }
125 undefined

9/13/2012 DSA - Fall 2012 - SEECS, NUST 14


Function Call by Reference
• Call by reference with pointer arguments
– Pass address of argument using & operator
– Allows you to change actual location in memory
– Arrays are not passed with & because the array name is already a
pointer

9/13/2012 DSA - Fall 2012 - SEECS, NUST 15


Function Call by Reference
1 /*
2 Cube a variable using call-by-reference
3 with a pointer argument */
4
5 #include <stdio.h>
6
7 void cubeByReference( int * ); /* prototype */
8
9 int main()
10 {
11 int number = 5;
12
13 printf( "The original value of number is %d", number );
14 cubeByReference( &number );
15 printf( "\nThe new value of number is %d\n", number );
16
17 return 0;
18 }
19
20 void cubeByReference( int *nPtr )
21 {
22 *nPtr = *nPtr * *nPtr * *nPtr; /* cube number in main */
23 }

9/13/2012 DSA - Fall 2012 - SEECS, NUST 16


Function Call by Reference

int main() void cubeByReference( int *nPtr )


{ { nPtr
int number = 5; number *nPtr = *nPtr * *nPtr * *nPtr;
undefined
cubeByReference( &number ); }
} 5

After call by reference to cubeByReference and before *nPtr is cubed:


n
int main() void cubeByReference( int *nPtr )
{ {
int number = 5; nPtr
number
cubeByReference( &number ); *nPtr = *nPtr * *nPtr * *nPtr;
address of
} 5 }
number

After *nPtr is cubed :

int main() void cubeByReference( int *nPtr )


{ {
int number = 5; number nPtr
*nPtr = *nPtr * *nPtr * *nPtr;
cubeByReference( &number ); } address of
} 125 number

9/13/2012 DSA - Fall 2012 - SEECS, NUST 17


ARRAYS

9/13/2012 DSA - Fall 2012 - SEECS, NUST 18


Pointer and Arrays
int word [5] = {0,1,2,3,4};
int * ptr = word; ptr word &word[0]
(ptr + n) word + n &word[n]
*ptr *word word[0]
*(ptr + n) *(word + n) word[n]

• Multi-dimensional array

9/13/2012 DSA - Fall 2012 - SEECS, NUST 19


POINTER TO FUNCTION

9/13/2012 DSA - Fall 2012 - SEECS, NUST 20


Pointer to Function
• Pointer to Function OR Function Pointer
– Points to address of function just like we’ve pointer to
integer or character
– A.k.a delegates

• Example int main(void) {


// Function pointer
int func (int a, int b) { int(*fptr)(int,int);
cout<<a<<endl; // Assign address to function pointer
cout<<b<<endl; fptr = func;
func(2,3);
return 0;
fptr(2,3); OR (*fptr)(2, 3)
} return 0;
}

9/13/2012 DSA - Fall 2012 - SEECS, NUST 21


Pointer to Functions
• Declaration:
– <return type of function> (*<name of pointer>)
(type of function arguments)
• Examples pointer to a function that takes an
integer argument and a float
– int(*g)(int, float); argument and returns an integer

pointer to a function that takes an


integer argument and a float
argument and returns a pointerto an
integer
– int*(*g[])(int, float);
An arrayof pointers to functions –
Each function takes an integer
argument and a float argument and
returns a pointer to an integer
9/13/2012 DSA - Fall 2012 - SEECS, NUST 22
Do you know them?
• Sizeof()
• Malloc/new
• Delete
• Garbage collection

9/13/2012 DSA - Fall 2012 - SEECS, NUST 23


Constant pointer vs. pointer constant

Constant Pointer Pointer Constant


char ch = 'c'; char ch = 'c';
char c = 'a';
// A constant pointer 'ptr' pointing
// A constant pointer to 'ch'
char *const ptr = &ch; const char *ptr = &ch;

ptr = &c; *ptr = 'a';


// Trying to assign new address to a // WRONG!!! Cannot change the
constant pointer. WRONG!!!! value at address pointed by 'ptr'.

9/13/2012 DSA - Fall 2012 - SEECS, NUST 24


Pointer to UDTs
struct complexNumber {
double real;
double imaginary;
complexNumber(r=0.0, i=0.0) {
real = r;
imaginary = i;
}
complexNumber c1(5.0, 3.0), c2(7.5, 9.0);
complexNumber *p1, *p2;
complexNumber *ptrs[];

p1 = &c1;
p2 = &c2;
*p1= * p2;
p1 = p2;
(*p1).real;
p1->real;
9/13/2012 DSA - Fall 2012 - SEECS, NUST 25
Questions?

You might also like