Pointers in C Pro
Pointers in C Pro
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
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;
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
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;
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 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
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 }
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
p ? 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
+ - * / %
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 make p to hold 204; p-- will make p to hold 196
A Rahman Pointers 33
List of unary operators
char *p, a;
p = &a;
Following both uniray operators are applicable to p:
++ --
p++ will make p to hold 201; p-- will make p to hold 199
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
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
A Rahman Pointers 41
Algorithm
Hello 5
Your Program
p p
q
0 1 2 3 4 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
A Rahman Pointers 44
Algorithm
Hello HELLO
Your Program
p p p
0 1 2 3 4 5
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
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(){
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