The document discusses functions and pointers, explaining how to declare and use pointers to pass values between functions and access memory locations indirectly. Pointers allow values to be modified by functions by storing the address of a variable. Proper initialization of pointer variables is important to avoid potentially corrupting memory.
1 of 26
Downloaded 227 times
More Related Content
Computer Programming- Lecture 11
1. Lecture 11
Functions and
Pointers
TCP1231 Computer Programming I 1
2. Objectives
To learn more about functions
Explore how to declare and manipulate
pointers with arrays, structure, and functions.
To become familiar with pointers.
To illustrate the applications of pointer
variables and pointer operators.
To understand what is pointer arithmetic
TCP1231 Computer Programming I 2
3. The Need for Pointers
How can we return more than one values from a
function?
How can we write a function that modifies the values
pass to it as parameters?
We also know that when we pass an arrays to a
function, the function may change the values stored in
the array, but how does it work actually?
TCP1231 Computer Programming I 3
4. Introduction to Pointers Value Addr
00000000
00000001
int a; a [ int ] 00000010
00000011
00000100
If I want to store the address of a, what do I do? 00000101
00000111
p [ int* ] 00001000
int* p;
00001001
00001010
To store an integer, we can store it in an integer 00001011
00001110
data type (int).
00001111
To store an address, then we must have some kind 00000101
00000111
of data type specially for storing address.
00001111
00010000
TCP1231 Computer Programming I 4
5. How to Declare Pointers Value Addr
00000000
00000001
int a=2; a [ int ] 2 00000010
00000011
int * p;
00000100
00000101
00000111
p= &a; p [ int* ] 00000010 00001000
00001001
This symbol is called The address of a is stored here 00001010
ampersand, it is an operator 00001011
which returns the address
00001110
00001111
The value of variable p is the address of variable 00000101
a. 00000111
00001111
Note: 00010000
Since p is a variable, it has its own address too.
TCP1231 Computer Programming I 5
6. How to Declare Pointers Value Addr
00000000
00000001
int a=2; a 2 00000010
00000011
int * p;
00000100
p= &a; 00000101
00000111
p is called a pointer, just as an address points p 00000010 00001000
to a physical location 00001001
00001010
p actually points to the value of variable a. 00001011
00001110
p is pointing to the value of variable a, to 00001111
access (retrieve or to modify the value) the 00000101
value of variable a indirectly using the pointer, 00000111
we can use a special operator called 00001111
indirection operator (or dereference operator) 00010000
TCP1231 Computer Programming I 6
7. Value Addr
00000000
int a=2; 00000001
int * p; a [ int ] 2 5 00000010
00000011
p= &a; 00000100
00000101
cout << a << *p; 00000111
p [ int* ] 00000010 00001000
a= 5; 2 2 00001001
cout << a << *p; 5 5
00001010
00001011
00001110
Here we are accessing the value of a indirectly 00001111
through the pointer, thus the '*' operator is called: 00000101
(indirection operator or dereferencing operator) as 00000111
00001111
we are accessing the value by referencing another
00010000
variable (pointer).
TCP1231 Computer Programming I 7
8. Value Addr
00000000
int a=2; 00000001
int * p; a [ int ] 2 5
27 & 00000010
00000011
p= &a; 00000100
00000101
cout << a << *p; 00000111
p [ int* ] 00000010 00001000
a= 5; 2 2 00001001
cout << a << *p; 5 5
00001010
00001011
*p= 7; 7 7 00001110
00001111
cout << a << *p; 00000101
00000111
00001111
00010000
TCP1231 Computer Programming I 8
9. Note: Value Addr
00000000
int main() p [ int* ] ??? 00000001
00000010
{ 00000011
00000100
int *p;
? 00000101
*p = 10; 00000111
00001000
} 00001001
00001010
Where does the value 10 end up? Since we did not initialize p to
00001011
point to anything, the value in p is undetermined and
unpredictable, the above code may end up corrupting some 00001110
crucial part of the operating system and it may hang your 00001111
system. Therefore always save your programs regularly when 00000101
programming with pointers, if you make some mistake, it may 00000111
very well hang your whole system. 00001111
00010000
TCP1231 Computer Programming I 9
10. int* p1, p2; int *p1;
is equivalent to int p2;
int *p1;
int *p1, *p2, m, n, *q;
int *p2;
is equivalent to
int m,n;
int *q;
int n; int n;
int *p = &n; is equivalent to int *p;
p = &n;
The C++ compiler will give you a
int n; syntax error saying this is invalid
int *p = n; conversion. In C, the compiler will
most likely give you a warning only
TCP1231 Computer Programming I 10
11. #include <iostream> a b c *p1 *p2 *p3
using namespace std; 2 3 4 2 3 2
int main() { 6 12 6 6 12 6
int a= 2;; int b= 3; int c= 4; 6 12 1 6 6 6
int *p1= &a; int *p2= &b; int *p3= &a; 1 12 1 1 1 1
cout << a << b << c << *p1 << *p2 << *p3 << endl;
a= a * 3;
c= *p1;
*p2= *p1 + a;
cout << a << b << c << *p1 << *p2 << *p3 << endl;
p2= p1;
c= 1;
cout << a << b << c << *p1 << *p2 << *p3 << endl;
p3=p1;
*p2= c;
cout << a << b << c << *p1 << *p2 << *p3 << endl;
return 0;
} TCP1231 Computer Programming I 11
12. Call by value Call by Pointer Call by Reference
#include <iostream> #include <iostream> #include <iostream>
using namespace std; using namespace std; using namespace std;
void swap( int A, int B ) void swap( int *A, int *B ) void swap( int &A, int &B )
{ { {
int temp; int temp; int temp;
temp = A; temp = *A; temp = A;
A = B; *A = *B; A = B;
B = temp; *B = temp; B = temp;
} } }
int main() { int main() { int main() {
int a=2, b=7; int a=2, b=7; int a=2, b=7;
cout << a << b << endl; cout << a << b << endl; cout << a << b << endl;
swap( a, b ); swap( &a, &b ); swap( a, b );
cout << a << b << endl; cout << a << b << endl; cout << a << b << endl;
return 0; a b return 0; a b return 0; a b
} 2 7 } 2 7 } 2 7
2 7 7 2 7 2
TCP1231 Computer Programming I 12
13. Delete #include <iostream>
using namespace std;
int main() {
int a=2, b=7;
int * p1= &a;
*p1= a * b;
cout << a << 't' << b << 't << *p1 << endl;
delete p1;
system(pause);
return 0;
}
TCP1231 Computer Programming I 13
14. #include <iostream>
Pointers using namespace std;
and Array int main() {
int i, b[] = {10,20,30,40};
int *bPtr = b;
//pointing to the first elements of array
cout << "The first element : " << *bPtr <<endl;
cout << "The 3rd element : " << *(bPtr + 2)<< endl;
//accessing array elements using pointer
cout << "Accessing array elements using pointer : " << endl;
for(i=0; i < 4; i++){
cout << bPtr[i] <<" ";
}
cout << endl;
system("PAUSE");
return 0;
}
TCP1231 Computer Programming I 14
15. b[] 10 20 30 40 bPtr = b;
bPtr points to the first
element in array b[]
bPtr
*(bPtr + 2)
b[] 10 20 30 40
bPtr moves two to the 3rd
element in array b[]
bPtr + 2
TCP1231 Computer Programming I 15
16. for(i=0; i < 4; i++){
b[] 10 20 30 40 cout << bPtr[i] <<" ";
}
i = 0, bPtr[0]
i = 1, bPtr[1]
bPtr[i]
bPtr
i = 2, bPtr[2]
i = 3, bPtr[3]
Output
10 20 30 40
TCP1231 Computer Programming I 16
17. Dynamic arrays
int main() {
int *a;
int size;
cout<<"Enter the size of the dynamic array==>>";
cin >> size;
a = new int[size];
cout<<" Enter "<<size<<" value"<<endl;
for(int i=0; i<size; i++)
cin>>a[i];
cout<<"The values you have entered are :"<<endl;
for(int i=0; i<size; i++)
cout<<a[i]<<" ";
delete [] a;
system(pause);
return 0;
}
TCP1231 Computer Programming I 17
19. Inline Functions
The use of macros(#) in C allows short functions to be called without
the normal overhead associated with function calls
There are several characteristics of macros that makes their use unsuitable
for C++
Thus, C++ introduced the notion of inline functions to allow users to
avoid the overhead of function calls
With inline functions, the compiler controls the process (as opposed to the
preprocessor, as was the case in C)
How function calls work
Recall that the result of compiling a computer program is a set of
machine instructions (an executable program)
When the program is run, the OS loads the instructions into memory
(associating each instruction with a memory address) and then
executes the instructions in order
When a function call is encountered, the program jumps to the
address of the function and then jumps back when the function has
completed
TCP1231 Computer Programming I 19
20. How functions work
Each time the program jumps to execute a function, there
is associated overhead:
Loading the function:
The instruction immediately following the function
call is stored
The function arguments are copied to a reserved
region of the stack
Load the instruction referenced by the function call
Terminating the function call:
(Possibly) store a return value in a register
Load the return instruction stored when the function
was first called
With inline functions, the compiler replaces each instance
of the function call with the corresponding code
TCP1231 Computer Programming I 20
21. #include <iostream> #include <iostream>
using namespace std; using namespace std;
inline int add( int A, int B ) int add( int A, int B )
{ {
return A+B; 9 return A+B;
} 9 }
9
int main() 9 int main()
{ 9 {
int a=2, b=7; int a=2, b=7;
for (int i=1; i<=5; i++) for (int i=1; i<=5; i++)
cout << add(a,b) << endl; cout << add(a,b) << endl;
system ("pause"); system ("pause");
return 0; return 0;
} }
TCP1231 Computer Programming I 21
22. Function Overloading
Function overloading (aka function polymorphism) allows functions in
C++ to share the same name
For example, imagine that we have a sorting algorithm that we wish to
use to implement functions for several types, such as int and char
Traditionally, we would have to use different names for these functions
void sortint(int a[]) {}
void sortchar(char a[]) {}
Overloaded functions are distinguished by their argument list
void sort(int a[]) {}
void sort(char a[]) {}
TCP1231 Computer Programming I 22
23. #include <iostream>
using namespace std;
int main()
int add( int A, int B ) {
{ int i1=2, i2=7;
return A+B; float f1=3, f2=5;
}
int add( int A ) cout << add(i1, i2) << endl;
{ cout << add(i1) << endl;
return A; cout << add(f1, f2) << endl;
}
int add( float A, float B ) system ("pause");
{ return 0;
return A+B; }
}
9
2
8
TCP1231 Computer Programming I 23
24. #include <iostream> Recursion
using namespace std;
A function definition may contain a call
int f( int a) to the function being defined.
{
if (a<1)
return 1;
else
return a * f(a-1);
}
int main()
{
int k=5;
cout << f(k);
return 0;
}
TCP1231 Computer Programming I 24
25. #include <iostream> #include <iostream>
using namespace std; Function Headers using namespace std;
int sum(int , int );
int sum(int x, int y) { void print(int );
int s;
s=x+y; int main () {
return s; int v;
} v=sum (3, 5);
void print(int x) { print(v);
cout << x; return 0;
} }
int main () {
int v; int sum(int x, int y) {
v=sum (3, 5); int s;
print(v); s=x+y;
return s;
return 0; }
} void print(int x) {
cout << x;
}
TCP1231 Computer Programming I 25