Pointers PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 24

Pointers in C

Outline

Pointers
What is a Pointer
Creating pointers in C
& operator, * operator, printf, scanf used for pointers
A detail example using pointers
Basic of pointer manipulations
Why we need pointers
Operators applicable to pointers
Arrays and pointers
Problem solving with pointers and arrays
Problem solving with pointers and strings
Pointers and dynamic memory allocation

A Rahman Pointers
What is a Pointer?
0 void main(){
1 int a;
int b;
a = 5;
b = 78;
printf(“%d\n”, a);
printf(“%d\n”, b);
200 5 a
RAM }
204 78 b

Pointer is a special variable


that holds memory address
of another variable.

A Rahman Pointers 3
Outline

Pointers
What is a Pointer
Creating pointers in C
& operator, * operator, printf, scanf used for pointers
A detail example using pointers
Why we need pointers
Basic of pointer manipulation
Operators applicable to pointers
Arrays and pointers
Problem solving with pointers and arrays
Problem solving with pointers and strings
Pointers and dynamic memory allocation

A Rahman Pointers
How to create pointers?

datatype *variable_name;

char *cp;
int *ip;
float *fp;
double *dp;

Pointer variables do not have any data type. The


datatype in front indicates the data type of the
variable whose address the pointer is going to hold

A Rahman Pointers 5
Outline

Pointers
What is a Pointer
Creating pointers in C
& operator, * operator, printf, scanf used for pointers
A detail example using pointers
Basic of Pointer Manipulations
Why we need pointers
Operators applicable to pointers
Arrays and pointers
Problem solving with pointers and arrays
Problem solving with pointers and strings
Pointers and dynamic memory allocation

A Rahman Pointers
Example of creating pointers?
0 void main(){
1 int a, b;
int *p,*q;
a = 5;
b = 78;
p = 200;
q = 204;
200 5 a
RAM
204 78 b }
208 200 p
212 204 q But a and b may not have the address
200 and 204 respectively in your computer
Use & operator to get address of a
And address of b

A Rahman Pointers 7
Example of creating pointers?
void main(){
0 int a, b;
1 int *p,*q;
a = 5;
b = 78;
p = &a;
q = &b;
printf(“%d\n”, p);
200 5 a RAM printf(“%d\n”,q);
204 78 b printf(“%d\n”, *p);
printf(“%d\n”, *q);
208 200 p }
212 204 q But a and b may not have the address
200 and 204 respectively in your computer
Use & operator to get address of a
And address of b
Use * operator to get value at the
location stored in a pointer
A Rahman Pointers 8
It’s DEMO time………..

A Rahman Pointers 9
Question

Write down a program that will take two


integers as input using pointers. Then using
pointers multiply the two values and display
the result.

A Rahman Pointers 10
Example of creating pointers?
0
void main(){
1 int a, b,r;
int *p,*q;
p = &a;
q = &b;
390 r scanf(“%d”,&a); scanf(“%d\n”,p);
200 5 a scanf(“%d”,&b); scanf(“%d\n”,q);
RAM
r = (*p) * (*q);
204 78 b printf(“%d\n”,r);
208 200 p }

212 204 q
Prints 390

A Rahman Pointers 11
Outline

Pointers
What is a Pointer
Creating pointers in C
& operator, * operator, printf, scanf used for pointers
A detail example using pointers
Basic of Pointer Manipulations
Why we need pointers
Operators applicable to pointers
Arrays and pointers
Problem solving with pointers and arrays
Problem solving with pointers and strings
Pointers and dynamic memory allocation

A Rahman Pointers
Pointer Basic Operations

int i = 5;
int *ip = &i;
printf("%d\n", *ip); Prints 5

*ip = 7;

A Rahman Pointers 13
Pointer Basic Operations

int i = 5;
int *ip = &i;
printf("%d\n", *ip);

*ip = 7;

printf("%d\n", i); Prints 7

int j = 3;
ip = &j;

A Rahman Pointers 14
Pointer Basic Operations

int i = 5;
int *ip = &i;
printf("%d\n", *ip);

*ip = 7;

printf("%d\n", i);

int j = 3;
ip = &j;
printf("%d\n", *ip); Prints 3

int *ip2;
ip2 = ip;

A Rahman Pointers 15
Pointer Basic Operations

int i = 5;
int *ip = &i;
printf("%d\n", *ip);

*ip = 7;

printf("%d\n", i);

int j = 3;
ip = &j;
printf("%d\n", *ip);

int *ip2;
ip2 = ip;
printf("%d\n", *ip2); Prints 3
ip = &i;

A Rahman Pointers 16
Pointer Basic Operations

int i = 5;
int *ip = &i;
printf("%d\n", *ip);

*ip = 7;

printf("%d\n", i);

int j = 3;
ip = &j;
printf("%d\n", *ip);

int *ip2;
ip2 = ip;
printf("%d\n", *ip2);
ip = &i;
printf("%d\n", *ip); Prints 7
A Rahman Pointers 17
Solve an exercise………..

A Rahman Pointers 18
Assume address of a is 200 and address of b is 300.

a b a_p b_p
int a = 50, b = 0; 50 0 - -
int *a_p = &a, *b_p = &b; 50 0 200 300
b = a+*b_p; 50 50 200 300
a_p = b_p 50 50 300 300
a = (*a_p) * (*b_p); 2500 50 300 300
*b_p = a/b; 2500 50 300 300

*a_p = a%b; 2500 0 300 300

A Rahman Pointers 19
Outline

Pointers
What is a Pointer
Creating pointers in C
& operator, * operator, printf, scanf used for pointers
A detail example using pointers
Basic of Pointer Manipulations
Why we need pointers
Operators applicable to pointers
Arrays and pointers
Problem solving with pointers and arrays
Problem solving with pointers and strings
Pointers and dynamic memory allocation

A Rahman Pointers
Question

Write down a void function that will take two


integers as parameters and will interchange
their values. From your main function take
two integers as input and use this function
two interchange their values. Finally print
their values.

A Rahman Pointers 21
Interchanging content using
function?
void swap(int x, int y){
x y
int t;
t = x;
x = y;
y = t;
}
void main(){
int a, b;
scanf(“%d%d”,&a,&b);
swap(a,b);
printf(“%d %d”,a,b);
}

A Rahman Pointers 22
Interchanging content using
function?
0 void swap(int x, int y){
1 int t;
t = x;
x = y;
y = t;
}
void main(){
200 25 a
RAM int a, b;
204 50 b scanf(“%d%d”,&a,&b);
208 25 50 swap(a,b);
x
printf(“%d %d”,a,b);
212 50 25 y }

25 t

A Rahman Pointers 23
Interchanging content using
function?
0 void swap(int *x, int *y){
1 int t;
t = *x;
*x = *y;
*y = t;
}
void main(){
200 25 50 a
RAM int a, b;
204 50 25 b scanf(“%d%d”,&a,&b);
208 200 swap(&a,&b);
x
Prints 50 25 printf(“%d %d”,a,b);
212 204 y }

25 t Passing address instead of values


solved the problem
scanf stores the value at a variable
So it needs the address of the variable
A Rahman Pointers 24

You might also like