0% found this document useful (0 votes)
26 views46 pages

Programming For Engineers Lecture 09

In the previous lecture, we learned about functions and how they break complex problems into smaller pieces using a top-down approach. Pointers were introduced, which store the address of a variable in memory. Some key points about pointers are that they must be declared to point to a specific type, can be used to pass arguments by reference into functions, and pointer arithmetic allows accessing elements in arrays using pointers.

Uploaded by

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

Programming For Engineers Lecture 09

In the previous lecture, we learned about functions and how they break complex problems into smaller pieces using a top-down approach. Pointers were introduced, which store the address of a variable in memory. Some key points about pointers are that they must be declared to point to a specific type, can be used to pass arguments by reference into functions, and pointer arithmetic allows accessing elements in arrays using pointers.

Uploaded by

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

In Previous lecture

• We used functions for breaking complex problems into smaller


pieces, which is a top-down structured approach.
• Each function should be a small module, self contained and it
should solve a well defined problem.
• Variable names and function names should be self explanatory.
• Always comment your code
Train your mind……….!!!

2
Pointers
Pointers
Location
x
60000 10
Address of x
Declaring Pointer to Integer

int *myptr ;

myptr is pointer to an integer


Declaring Pointers

double *x ;
char *c ;
Example
int *ptr ;
int x ;
x = 10 ;
ptr = &x ;
Dereferencing Operator *

*ptr is read as
“The value of what ever ptr points to”
z = *ptr * 2 ;
Initializing Pointers
ptr = &var ;

ptr = 0 ;
ptr = NULL ;

0 and NULL points to nothing


Example
main ( )
{
int numEmp ;
….
funct ( &numEmp ) ;
….
}

void funct ( int *numEmp )


{
cin >> *numEmp ;
}
Declaring pointers

int *ptr1 , *ptr2 , *ptr3 ;


Declaring pointers

int *ptr , x ;
Declaring pointers

int *ptr , x , a [ 10 ] ;
Bubble Sort
5 1 1 1
1 5 3 2
3 3 5 3
6
6 6 4
2
9 2 5
9
2 4 6
4
4 8 8
8
8 9 9
Swapping
Swap
temp = x ;
x=y;
y = temp ;
Example
main ( )
{
int x = 10 , y = 20 , * yptr , * xptr ;
yptr = &y ;
xptr = &x ;
swap ( yptr , xptr ) ;
}
Example
swap ( int *yptr , int *xptr )
{
………
}
const
int *const myptr = &x ;

myptr is a constant pointer to an integer


const
const int x = 10 ;
const
const int *myptr = &x ;

myptr is a pointer to a constant integer


Array
int a [ 10 ] ;
Starting Address of Array a 1
2
3
4
5
6
7
8
9
10
Pointers and Arrays
int y [ 10 ] ; Starting Address of Array y 0
[0]
1
[1]
2 [2]
3 [3]
4 [4]
5 [5]
6 [6]
7 [7]
8 [8]
9 [9]
The name of the array is like a
pointer which contain the
address of the first element.
Declaration of a Pointer Variable
int y [ 10 ] ;
int *yptr ;

yptr is a pointer to integer


yptr = y ;
Declaration of a Pointer Variable
0
[0]
1
[1]
2 [2]
y[3] 3 [3]
4 [4]
5 [5]
6 [6]
7 [7]
8 [8]
9 [9]
int y [ 10 ] ;
int *yptr ;
yptr = y ;
yptr ++ ;
location
3000 3004 3008 3012 3016

y[0] y[1] y[2] y[3] y[4]

pointer variable yPtr


In this case yptr is a pointer
to integer so now when we
increment yptr it points to
the next integer
Example 1
#include<iostream.h>
main ( )
{
int y [ 10 ] ;
int *yptr = y ;
yptr = y ;
cout << yptr ;
yptr ++ ;
cout << yptr ;
}
yptr = y ;

is same as

yptr = &y [ 0 ] ;
……..
yptr = &y [ 2 ] ;
Example 2
#include<iostream.h>
main ( )
{
int y [ 10 ] ;
int *yptr ;
yptr = y ;
cout << yptr ;
yptr ++ ;
cout << *yptr ;
}
Example 3
main ( )
{
int x = 10 ;
int *yptr ;
yptr = &x ;
cout << yptr ;
cout << *yptr ;
*yptr ++ ;
increment whatever yptr points to
}
Pointer Arithmetic
*yptr + 3 ; This Is an Expression

cout << *yptr ;


*yptr += 3 ;
Pointer Arithmetic

yptr = &x ;
yptr ++ ;
Pointer Arithmetic
int x =10 ;
int *yptr ;
yptr = &x ;
*yptr += 3 ;
yptr += 3 ;
Decrementing

*yptr --
Pointer Arithmetic
int *p1 ,*p2;
…..
p1 + p2 ;

Error
Pointer Arithmetic
int y [ 10 ] , *y1 , *y2 ;
y1 = &y [ 0 ] ;
y2 = &y [ 3 ] ;
cout << y2 - y1 ;
Pointer Arithmetic
int y [ 10 ] ;
int *yptr ;
yptr = y [ 5 ] ;
cout << *( yptr + 5 ) ;
Pointer Comparison
if ( y1 > y2 )
if ( y1 >= y2 )
if ( y1 == y2 )
Pointer Comparison

if ( *y1 > *y2 )


Example
int y [ 10 ] ;
int *yptr ;
yptr = y ;
cout << y [ 5 ] ;
cout << ( yptr + 5 ) ;
cout << *( yptr + 5 ) ;
Example
int que [ 10 ] ;
int y [ 10 ];
int *yptr ;
yptr = y ;
yptr = que ;
location
3000 3004 3008 3012 3016

v[0] v[1] v[2] v[3] v[4]

pointer variable vPtr

You might also like