PROGRAMMI
DEPARTMENT OF
NG
ELECTRICAL
ENGINEERING
FUNDAMENTA
LS
COURSE CODE: CS02118
OMER KHAN
LECTURER ELECTRICAL ENGINEERING
DEPARTMENT
BEST FOR You
THE UNIVERSITY OF LAHORE
O R G A N I C S C O M PA N Y
Programming Fundamentals
This course provides in-depth coverage of both
procedural programming principles and techniques using
C++. Topics includes fundamental data types, abstract
data types, selection statements, repetition statements.
Functions, arrays and matrices, pointers and records
Main Topics to be Covered
Introduction to Course, Starting to “C / C++”
Programming Language
Selection Statements / Decisions
Repetition Structures
Switch Statement
Functions
Arrays, Strings
Pointers, String Manipulation Functions, Bit
Manipulation
Structures
File Handling
BEST FOR You 2
O R G A N I C S C O M PA N Y
Course Learning Outcomes
Taxonomy Mapped
CLO # CLO Description Domain Level PLO
Design and Implement the solution of problem using condition, loops, array,
CLO 1 functions, structures and file handling 0 Cognitive 6 PLO 3
CLO 2 Apply programing concepts to dry run and troubleshoot programming problems Cognitive 3 PLO 1
Assess societal, health, safety, legal and cultural issues while designing solutions
CLO 3 Cognitive 5 PLO 6
of complex engineering problems using modern Programming techniques.
Develop Computer programs in lab for hands on experience for students using
CLO 4 C++ language. Cognitive 6 PLO 3
CLO 5 Behave according to professional ethics and responsibilities with fellow Affective 3 PLO 8
students, lab staff and instructor during lab time
CLO 6 Exemplify working efficiently in a group and contributing in the lab tasks Affective 5 PLO 9
BEST FOR You 3
O R G A N I C S C O M PA N Y
Topic of Presentation
POINTERS
BEST FOR You 4
O R G A N I C S C O M PA N Y
Flow Of
Presentation
• Pointers
• What are Pointers?
• Using Pointers in C++
• Important Pointer Concepts
• Null Pointers
• Pointer Arithmetic
• Pointers vs. Array
• Array of Pointers
• Pointer to Pointer
• Passing Pointers to Functions
• Return Pointer from Function BEST FOR You 5
O R G A N I C S C O M PA N Y
Pointers
What are Pointers?
Using Pointers in C+
+
BEST FOR You 6
O R G A N I C S C O M PA N Y
Pointers
» Some C++ tasks are performed more easily with pointers, and other C++ tasks,
• Such as dynamic memory allocation, cannot be performed without them.
» As you know every variable is a memory location and every memory location has its
address defined which can be accessed using ampersand (&) operator which denotes an
address in memory
BEST FOR You 7
O R G A N I C S C O M PA N Y
Pointers
BEST FOR You 8
O R G A N I C S C O M PA N Y
Pointers
Consider the following which will print the address When the above code is compiled and executed, it
of the variables defined − produces the following result −
#include <iostream> Address of var1 variable: 0xbfebd5c0
using namespace std; Address of var2 variable: 0xbfebd5b6
int main () {
int var1;
char var2[10];
cout << "Address of var1 variable: ";
cout << &var1 << endl;
cout << "Address of var2 variable: ";
cout << &var2 << endl;
return 0;
}
BEST FOR You 9
O R G A N I C S C O M PA N Y
What are Pointers?
» A pointer is a variable whose value is the address of another variable.
» Like any variable or constant, you must declare a pointer before you can work with it.
» The general form of a pointer variable declaration is −
type *var_name;
BEST FOR You 10
O R G A N I C S C O M PA N Y
What are Pointers?
» Here, type is the pointer's base type; it must be a valid C++ type and var-name is the
name of the pointer variable.
» The sign you used to declare a pointer is the same sign that you use for multiplication.
However, in this statement the sign is being used to designate a variable as a pointer.
Following are the valid pointer declaration −
int *ip; // pointer to an integer
double *dp; // pointer to a double
float *fp; // pointer to a float
char *ch // pointer to character
BEST FOR You 11
O R G A N I C S C O M PA N Y
What are Pointers?
» The actual data type of the value of all pointers, whether integer, float, character, or
otherwise, is the same, a long hexadecimal number that represents a memory address.
» The only difference between pointers of different data types is the data type of the
variable or constant that the pointer points to.
BEST FOR You 12
O R G A N I C S C O M PA N Y
Using Pointers in C++
» There are few important operations, which we will do with the pointers very frequently.
• We define a pointer variable.
• Assign the address of a variable to a pointer.
• Finally access the value at the address available in the pointer variable.
» This is done by using unary operator * that returns the value of the variable located at
the address specified by its operand.
BEST FOR You 13
O R G A N I C S C O M PA N Y
Using Pointers in C++
» Following example makes use of these operations −
#include <iostream> // print the address stored in ip pointer
using namespace std; variable
int main () { cout << "Address stored in ip variable: ";
int var = 20; // actual variable declaration. cout << ip << endl;
int *ip; // pointer variable // access the value at the address available in
ip = &var; // store address of var in pointer pointer
variable cout << "Value of *ip variable: ";
cout << "Value of var variable: "; cout << *ip << endl;
cout << var << endl; return 0;
}
» When the above code is executed, it produces result something as follows −
Value of var variable: 20
Address stored in ip variable: 0xbfc601ac
Value of *ip variable: 20 BEST FOR You 14
O R G A N I C S C O M PA N Y
Pointers in C++
» There are few important pointer concepts which should be clear to a C++ programmer −
Sr. No Concept & Description
Null Pointers: C++ supports null pointer, which is a constant with a value of zero defined in several
1 standard libraries.
2 Pointer Arithmetic: There are four arithmetic operators that can be used on pointers: ++, --, +, -
3 Pointers vs. Arrays: There is a close relationship between pointers and arrays.
4 Array of Pointers: You can define arrays to hold a number of pointers.
5 Pointer to Pointer: C++ allows you to have pointer on a pointer and so on.
6 Passing Pointers to Functions: Passing an argument by reference or by address both enable the passed
argument to be changed in the calling function by the called function.
7 Return Pointer from Functions: C++ allows a function to return a pointer to local variable, static
variable and dynamically allocated memory as well.
BEST FOR You 15
O R G A N I C S C O M PA N Y
Important Pointer
Concepts
Null Pointers
Pointer Arithmetic
Pointers vs. Array
Array of Pointers
Pointer to Pointer
Passing Pointers to Functions
Return Pointer from Function BEST FOR You
O R G A N I C S C O M PA N Y
16
Null Pointers:
» It is always a good practice to assign the pointer NULL to a pointer variable in case you
do not have exact address to be assigned.
» This is done at the time of variable declaration.
• A pointer that is assigned NULL is called a null pointer.
BEST FOR You 17
O R G A N I C S C O M PA N Y
Null Pointers:
#include <iostream>
using namespace std;
int main () {
int *ptr = NULL;
cout << "The value of ptr is " << ptr ;
return 0;
Output:
} The value of ptr is 0
• To check for a null pointer you can use an if statement as follows −
• if(ptr) // succeeds if p is not null
• if(!ptr) // succeeds if p is null
BEST FOR You 18
O R G A N I C S C O M PA N Y
Pointer Arithmetic:
» As you understood pointer is an address which is a numeric value; therefore, you can
perform arithmetic operations on a pointer just as you can a numeric value.
» There are four arithmetic operators that can be used on pointers: ++, --, +, and –
» Let us consider that ptr is an integer pointer which points to the address 1000.
» Assuming 32-bit integers, let us perform the following arithmetic operation on the pointer −
ptr++
» The ptr will point to the location 1004 because each time ptr is incremented, it will point to the
next integer.
BEST FOR You 19
O R G A N I C S C O M PA N Y
Pointer Arithmetic:
» Pointer Comparisons:
• Pointers may be compared by using relational operators, such as ==, <, and >.
• If p1 and p2 point to variables that are related to each other, such as elements of the same array, then
p1 and p2 can be meaningfully compared.
BEST FOR You 20
O R G A N I C S C O M PA N Y
Pointers vs Arrays
» Pointers and arrays are strongly related. In fact, pointers and arrays are interchangeable
in many cases.
» For example, a pointer that points to the beginning of an array can access that array by
using either pointer arithmetic or array-style indexing
BEST FOR You 21
O R G A N I C S C O M PA N Y
Pointers vs Arrays
#include <iostream> cout << ptr << endl;
using namespace std;
const int MAX = 3; cout << "Value of var[" << i << "] = ";
int main () { cout << *ptr << endl;
int var[MAX] = {10, 100, 200}; // point to the next location
int *ptr; ptr++;
// let us have array address in pointer. }
ptr = var; return 0;
for (int i = 0; i < MAX; i++) { } Output:
Address of var[0] = 0xbfa088b0
cout << "Address of var[" << i << "] = "; Value of var[0] = 10
Address of var[1] = 0xbfa088b4
Value of var[1] = 100
Address of var[2] = 0xbfa088b8
Value of var[2] = 200
BEST FOR You 22
O R G A N I C S C O M PA N Y
Array of Pointers
» There may be a situation, when we want to maintain an array, which can store pointers
to an int or char or any other data type available.
» Following is the declaration of an array of pointers to an integer −
int *ptr[MAX];
» This declares ptr as an array of MAX integer pointers. Thus, each element in ptr, now
holds a pointer to an int value.
BEST FOR You 23
O R G A N I C S C O M PA N Y
Array of Pointers
#include <iostream> }
using namespace std; for (int i = 0; i < MAX; i++) {
const int MAX = 3; cout << "Value of var[" << i << "] = ";
int main () { cout << *ptr[i] << endl;
int var[MAX] = {10, 100, 200}; }
int *ptr[MAX]; return 0;
for (int i = 0; i < MAX; i++) { }
ptr[i] = &var[i]; // assign the address of
integer. Output:
Value of var[0] = 10
Value of var[1] = 100
Value of var[2] = 200
BEST FOR You 24
O R G A N I C S C O M PA N Y
Pointer to Pointer
» A pointer to a pointer is a form of multiple indirection or a chain of pointers.
» Normally, a pointer contains the address of a variable.
• When we define a pointer to a pointer, the first pointer contains the address of the second
pointer, which points to the location that contains the actual value as shown below.
» A variable that is a pointer to a pointer must be declared as such
int **var;
BEST FOR You 25
O R G A N I C S C O M PA N Y
Pointer to Pointer
#include <iostream> // take the address of ptr using address of
using namespace std; operator &
int main () { pptr = &ptr;
int var; // take the value using pptr
int *ptr; cout << "Value of var :" << var << endl;
int **pptr; cout << "Value available at *ptr :" << *ptr <<
var = 3000; endl;
cout << "Value available at **pptr :" <<
// take the address of var
**pptr << endl;
Output:
ptr = &var; return Value
0; of var :3000
} Value available at *ptr :3000
Value available at **pptr :3000
BEST FOR You 26
O R G A N I C S C O M PA N Y
Passing Pointers to Functions
» C++ allows you to pass a pointer to a function. To do so, simply declare the function
parameter as a pointer type.
» Following a simple example where we pass an unsigned long pointer to a function and
change the value inside the function which reflects back in the calling function −
BEST FOR You 27
O R G A N I C S C O M PA N Y
Passing Pointers to Functions
#include <iostream> return 0;
#include <ctime> }
using namespace std;
void getSeconds(unsigned long *par); void getSeconds(unsigned long *par) {
int main () { // get the current number of seconds
unsigned long sec; *par = time( NULL );
getSeconds( &sec ); return;
// print the actual value }
cout << "Number of seconds :" << sec << endl; Output:
Number of seconds :1294450468
BEST FOR You 28
O R G A N I C S C O M PA N Y
Return Pointer from Functions
» As we have seen in last Lecture how C++ allows to return an array from a function,
similar way C++ allows you to return a pointer from a function
int * myFunction() {
.
.
.
}
» Now, consider the following function, which will generate 10 random numbers and
return them using an array name which represents a pointer i.e., address of first array
element.
BEST FOR You 29
O R G A N I C S C O M PA N Y
Return Pointer from Functions
#include <iostream> }
#include <ctime> // main function to call above defined function.
using namespace std; int main () {
int * getRandom( ) { // a pointer to an int.
static int r[10]; int *p;
// set the seed p = getRandom();
srand( (unsigned)time( NULL ) ); for ( int i = 0; i < 10; i++ ) {
for (int i = 0; i < 10; ++i) { cout << "*(p + " << i << ") : ";
r[i] = rand(); cout << *(p + i) << endl;
cout << r[i] << endl; }
} return 0;
return r; }
BEST FOR You 30
O R G A N I C S C O M PA N Y
Return Pointer from Functions
When the above code is executed, it produces result something as follows
624723190 *(p + 0) : 624723190
1468735695 *(p + 1) : 1468735695
807113585 *(p + 2) : 807113585
976495677 *(p + 3) : 976495677
613357504 *(p + 4) : 613357504
1377296355 *(p + 5) : 1377296355
1530315259 *(p + 6) : 1530315259
1778906708 *(p + 7) : 1778906708
1820354158 *(p + 8) : 1820354158
667126415 *(p + 9) : 667126415
BEST FOR You 31
O R G A N I C S C O M PA N Y
Practice Problem
1. Write a C program to create, initialize and use pointers.
2. Write a C program to add two numbers using pointers.
3. Write a C program to swap two numbers using pointers.
4. Write a C program to input and print array elements using pointer.
5. Write a C program to copy one array to another using pointers.
6. Write a C program to swap two arrays using pointers.
7. Write a C program to reverse an array using pointers.
8. Write a C program to search an element in array using pointers.
9. Write a C program to access two dimensional array using pointers.
10.Write a C program to add two matrix using pointers.
BEST FOR You 32
O R G A N I C S C O M PA N Y
Practice Problem
1. Write a C program to multiply two matrix using pointers.
2. Write a C program to find length of string using pointers.
3. Write a C program to copy one string to another using pointers.
4. Write a C program to concatenate two strings using pointers.
5. Write a C program to compare two strings using pointers.
6. Write a C program to find reverse of a string using pointers.
7. Write a C program to sort array using pointers.
8. Write a C program to return multiple value from function using pointers.
BEST FOR You 33
O R G A N I C S C O M PA N Y
DEPARTMENT OF
ELECTRICAL
ENGINEERING
Thank You
OMER KHAN
+92 336 8425345
omer.khan@ee.uol.edu.pk
https://faculty.uol.edu.pk/Faculty/8816/Omer%20Khan
BEST FOR You
O R G A N I C S C O M PA N Y