0% found this document useful (0 votes)
10 views

Pointers in C Pro

C pointers

Uploaded by

maisha.ayman.75
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Pointers in C Pro

C pointers

Uploaded by

maisha.ayman.75
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 54

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 to
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
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
List of operators

Pointers hold addresses which are nothing but numbers (integers)

int *p, *q, a, b;


p = &a;
q = &b;

What binary operators are applicable between p and q ?

p ? q = ??

+ - * / %

ANS: Whatever operator makes sense is applicable between p and q

A Rahman Pointers 26
Lets think about real life scenario
3 + 12 = 15

12
3

A Rahman Pointer 27
Lets think about real life scenario
3 * 12 = 36

12
3

A Rahman Pointer 28
Lets think about real life scenario
3 / 12 = 0

12
3

A Rahman Pointer 29
Lets think about real life scenario
3 % 12 = ?

12
3

A Rahman Pointer 30
Lets think about real life scenario
12 - 3 = 9

12
3

A Rahman Pointer 31
List of operators

Pointers hold addresses which are nothing but numbers

int *p, *q, a, b;


P = &a;
q = &b;

What binary operators are applicable between p and q ?

+ - * / %

Whatever operator makes sense is applicable between p and q

ONLY minus (-) operator is applicable between p and q

A Rahman Pointers 32
List of unary operators
Pointers hold addresses which are nothing but numbers (integers)

int *p, a;
p = &a;
Following both uniray operators are applicable to p:

++ --

p++; Will increase the value by 1 unit


p--; Will decrease the value by 1 unit

Suppose a’s address is 200, so that p = &a makes p= 200.

p++ will make p to hold 204; p-- will make p to hold 196

p = p + 5; will make p to hold 220 as integers are 4 bytes

A Rahman Pointers 33
List of unary operators

Pointers hold addresses which are nothing but numbers (integers)

char *p, a;
p = &a;
Following both uniray operators are applicable to p:

++ --

p++; Will increase the value by 1 unit


p--; Will decrease the value by 1 unit
Suppose a’s address is 200, so that p = &a makes p= 200.

p++ will make p to hold 201; p-- will make p to hold 199

p = p + 5; will make p to hold 205 as characters are 1 byte


For float and double the result will be similar
A Rahman Pointers 34
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
Arrays and pointers
void main(){
int a[5] = {3,8,2,7,6};
int *p;
200 3 a[0] p = &a[0]; // p = a;
204 8 a[1] Prints 200 printf(“%d\n”, p);

208 2 a[2] a
Prints 3 printf(“%d\n”, *p);

212 7 a[3]
Prints 212 printf(“%d\n”, p+3);
216 6 a[4]
Prints 7 printf(“%d\n”, *(p+3));
200 p
Prints 6 printf(“%d\n”, *p+3);
}

A Rahman Pointers 36
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 program that will find average


of array elements using pointers.

A Rahman Pointers 38
The program
void main(){
int a[5] = {3,8,2,7,6};
int *p;
200 3 a[0] p = &a[0]; // p = a;
204 8 a[1] float sum = 0;
int i;
208 2 a[2] a
for(i=0; i < 5; i++){
sum = sum + *p;
212 7 a[3]
p++;
216 6 a[4] }
sum = sum/5;
200
204 p
printf(“%f”,sum);
}

A Rahman Pointers 39
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 program that will find length of


a string using pointer. The string will be
input to your program.

A Rahman Pointers 41
Algorithm

Hello 5
Your Program

p p
q
0 1 2 3 4 5

100 101 102 103 104 105

Length: p-q = 105-100 = 5

A Rahman Pointers 42
Program

void main(){
char t[50];
char *p,*q;
gets(t);
p = q = &t[0]; // p = q = t;
int n = 0;
while(*p != ‘\0’)
p++;
n = p – q;
printf(“Length: %d”, n);
}

A Rahman Pointers 43
Question

Write down a program that will convert a


string to uppercase using pointer. The string
will be input to your program.

A Rahman Pointers 44
Algorithm

Hello HELLO
Your Program

p p p
0 1 2 3 4 5

100 101 102 103 104 105

HE L L O

A Rahman Pointers 45
Program

void main(){
char t[50];
char *p;
gets(t);
p = &t[0]; // p = t;
while(*p != ‘\0’){
*p = toupper(*p);
p++;
}
printf(“%s”, t);
}

A Rahman Pointers 46
Question

Write down a program that will reverse a


string using pointers. The string will be input
to your program. Solve it in two approaches:
(1) Using an additional array
(2) Without using any additional array

A Rahman Pointers 47
1st Algorithm

Hello olleH
Your Program

p p p p
0 1 2 3 4 5

q
q
100 101 102 103 104 105

o l l e H

A Rahman Pointers 48
void main(){

1st Program char s[50],t[50];


char *p, *q;
gets(s);
p = &s[0]; // p = s;
while(*p != ‘\0’){
p++;
}
p--;
q = t;
while(p >= &s[0]){
*q = *p;
q++;
p--;
}
p = s;
q = t;
while(*p != ‘\0’){
*p = *q;
p++;
q++;
}
printf(“%s”, s);
}
A Rahman Pointers 49
2nd Algorithm

Hello olleH
Your Program

p p p p
q q
0 1 2 3 4 5

o l e H
100 101 102 103 104 105

A Rahman Pointers 50
2nd Program
void main(){
char s[50],t;
char *p, *q;
gets(s);
p = &s[0]; // p = s;
while(*p != ‘\0’){
p++;
}
p--;
q = s;
while(p > q){
t = *p;
*p = *q;
*q = t;
q++;
p--;
}
printf(“%s”, s);
}

A Rahman Pointers 51
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
Dynamic memory allocation
#include <stdlib.h>
void main(){
int *p;
p = malloc(4); p = malloc(sizeof(int));
*p = 37;
printf(“%d”,*p); Prints 37
scanf(“%d”,p);
printf(“%d”,*p); Prints whatever the input is
free(p);
}

A Rahman Pointers 53
Dynamic memory allocation: Array
#include <stdlib.h>
void main(){
int *p, N, i;
printf(“How Many students?”);
scanf(“%d”,&N);
p = malloc(N*sizeof(int));
for(i = 0; i < N; i++){
scanf(“%d”,&p[i]);
….
….

free(p);
}

A Rahman Pointers 54

You might also like