Pointers in C Pointers in C
1
What is a pointer?
What is a pointer?
Variables that hold memory Variables that hold memory addresses are called
addresses are called pointer pointer variables
variables or pointers. or pointers.
A pointer variable contains A pointer variable contains an address which is a an address which is a location of another variable location of another variable
in memory.
in memory.
Type of the pointer is Type of the pointer is decided by the type of the decided by the type of the
value it is pointing to.
value it is pointing to.
A A pointer pointer varible varible is is distinguished by
distinguished by * *
Variable value address
quantity 180 5000
ptr 5000 5048
2
The The & & and and * * operators operators
& is used determine the address of a variable. & is used determine the address of a variable.
Use of address operator in scanf() function. Use of address operator in scanf() function.
ptr = &x; ptr = &x;
Ex: main() Ex: main()
{ {
int i=3;
int i=3;
printf(“address of i is =%u\n”, &i);
printf(“address of i is =%u\n”, &i);
printf(value of i is =%d\n”,i);
printf(value of i is =%d\n”,i);
} }
o/p is: address of i= 6485 o/p is: address of i= 6485
value of i = 3 value of i = 3
&
& is address operator, is address operator, * * is called indirection operator is called indirection operator
3
Declaration and Initialization of pointer Declaration and Initialization of pointer
variables variables
int quantity;
int quantity;
int *p;
int *p;
p=&quantity;
p=&quantity;
Or you can combine declaration and initialization as Or you can combine declaration and initialization as int quantity;
int quantity;
int *p=&quantity;
int *p=&quantity;
OR OR
int x, *p=&x;
int x, *p=&x;
Another ex:
Another ex:
int *p=NULL;
int *p=NULL;
int *p=0;
int *p=0;
4Using pointers in expressions Using pointers in expressions
main() main() { {
int i=3;
int i=3;
int *j;
int *j;
j=&i;
j=&i;
printf(“%u\n”, &i);
printf(“%u\n”, &i);
printf(“%u\n”, j);
printf(“%u\n”, j);
printf(%u\n”, &j);
printf(%u\n”, &j);
printf(“%d\n”, i);
printf(“%d\n”, i);
printf(“%d\n”,*(&i));
printf(“%d\n”,*(&i));
printf(“%d\n”,*j);
printf(“%d\n”,*j);
} }
33 6485 6485
i j
6485 3276
Output of each of the printf statements???
5
Check whether the following statements are Check whether the following statements are
correct or not!!!!!
correct or not!!!!!
int a=35;
int a=35;
int *b;
int *b;
b=&a;
b=&a;
(a)
(a) b contains the address of an int b contains the address of an int
(b)
(b) value at address contained in b is an int value at address contained in b is an int
(c)
(c) b is an int pointer b is an int pointer
(d)
(d) b points to an int b points to an int
(e)
(e) b is a pointer which points in the direction of an int b is a pointer which points in the direction of an int
6
char, int and float pointers char, int and float pointers
main() main() { {
char c, *cptr;
char c, *cptr;
int i, *iptr;
int i, *iptr;
float f, *fptr;
float f, *fptr;
c= ‘A’;
c= ‘A’;
i=54;
i=54;
f= 3.14;
f= 3.14;
cptr=&c;
cptr=&c;
iptr=&i;
iptr=&i;
fptr=&f;
fptr=&f;
printf(“address contained in cptr=
printf(“address contained in cptr=
%u\n”,cptr);
%u\n”,cptr);
printf(“address contained in iptr=
printf(“address contained in iptr=
%u\n”,iptr);
%u\n”,iptr);
printf(“address contained in fptr=%u\n”,fptr);
}
‘A’
54 3.14
1004
c i f
cptr iptr fptr
7006 2008
1962 7602 9118
1004 2008 2009
7006 7007 7008 7009The concept of base address
7
Pointer Expressions Pointer Expressions
Ex: y=
Ex: y=*p1 *p1 * * p2 * * p2 ; implies (*p1) * (*p2) ; implies (*p1) * (*p2)
sum=sum+*p1; sum=sum+*p1;
z=5*- *p2/ * z=5*- *p2 / * p1; don’t use /* p1; don’t use /* as it means different as it means different
*p2=*p2+10; *p2=*p2+10;
Pointer Arithmetic Pointer Arithmetic
a.a.
You can add integers to or subtract integers from pointers You can add integers to or subtract integers from pointers ex:p1+4, p2-2 etc
ex:p1+4, p2-2 etc
b.
b.
You can subtract one pointer from another (if p1 and p2 point to You can subtract one pointer from another (if p1 and p2 point to the same array,
the same array, p2-p1 p2-p1 gives the number of elements between p1 gives the number of elements between p1 and p2.
and p2.
c.c.
We can use short hand operators with pointers ex: p1++; We can use short hand operators with pointers ex: p1++;
d.
d.
Pointers cannot be used in division or multiplication or addition Pointers cannot be used in division or multiplication or addition among themselves
among themselves
8
Pointer increments and scale factor Pointer increments and scale factor
p1=p2+2;
p1=p2+2;
p1=p1+1;
p1=p1+1;
p1++;
p1++;
When we increment a pointer, its value is increased by the length of When we increment a pointer, its value is increased by the length of the data type that it points to. This length is called
the data type that it points to. This length is called scale factor scale factor
character
character 1 byte 1 byte integer
integer 2 bytes 2 bytes float
float 4 bytes 4 bytes long int
long int 4 bytes 4 bytes double
double 8 bytes 8 bytes
9
Pointers and arrays Pointers and arrays
10
Pointers and arrays Pointers and arrays
int x[5]={1,2,3,4,5};
int x[5]={1,2,3,4,5};
int *p;
int *p;
p=x;
p=x; // or p=&x[0]; or base address // or p=&x[0]; or base address
x[0] x[1] x[2] x[3] x[4]
x[0] x[1] x[2] x[3] x[4] elements elements value value
1000 1002 1004 1006 1008 1000 1002 1004 1006 1008 address address p=&x[0]=1000
p=&x[0]=1000
p+1=&x[1]=1000+
p+1=&x[1]=1000+ 2 2 =1002 so on……….. =1002 so on………..
Therefore, x[i]
Therefore, x[i]
*(x+i) *(x+i) *(i+x) *(i+x)
i[x] i[x]
refers same element.
refers same element.
1 2 3 4 5
11
Accessing 2D array elements using pointers Accessing 2D array elements using pointers
In the previous ex: *(p+3) would give the value of x[3]
In the previous ex: *(p+3) would give the value of x[3]
In general,
In general, *(p+i) *(p+i) or or *(x+i) *(x+i) would give the value at position i, if would give the value at position i, if the base pointer is p.
the base pointer is p.
Similarly, for a two dimensional array x[i][j], can be represented in Similarly, for a two dimensional array x[i][j], can be represented in pointer expression as
pointer expression as *(*(x+i)+j) or *(*(p+i)+j) *(*(x+i)+j) or *(*(p+i)+j)
Ex: int a[3][2]={
Ex: int a[3][2]={
{15,27}, {15,27}, {22,19}, {22,19}, {31,23} {31,23}
}; };
The element a[2][1] :- The element a[2][1] :-
12
Pointers and arrays Pointers and arrays
main() main() { {
int i;
int i;
int marks[]={55,65,75,56,78,78,90};
int marks[]={55,65,75,56,78,78,90};
for(i=0;i<=6;i++) for(i=0;i<=6;i++)
display(&marks[i]);
display(&marks[i]);
} }
display(int *n) display(int *n) { {
printf(“%d\n”,*n);
printf(“%d\n”,*n);
} }
What is the o/p?
13
Pointers and Strings Pointers and Strings
char str[5]=“good”;
char str[5]=“good”;
char *str=“good”;
char *str=“good”;
Or, char * str;
Or, char * str;str=“good”;str=“good”;
main() main() { {
char *name=“DELHI”; char *name=“DELHI”;
int length; int length;
char *cptr=name; char *cptr=name;
while(*cptr!=‘\0’) while(*cptr!=‘\0’) { {
printf(“%c is stored at the address %u\n”,*cptr, cptr); printf(“%c is stored at the address %u\n”,*cptr, cptr);
cptr++; cptr++;
} }
length=cptr-name; length=cptr-name;
printf(“the length of the string is=%d\n”, length); printf(“the length of the string is=%d\n”, length);
} }
g o o d \0
str
D E L H I \0
cptr name
14
Pointers and Functions Pointers and Functions
Pointers can be used to pass addresses of variables to called functions, Pointers can be used to pass addresses of variables to called functions, thus allowing the called function to alter the values stored there.
thus allowing the called function to alter the values stored there.
We looked earlier at a swap function that did not change the values We looked earlier at a swap function that did not change the values stored in the main program because only the values were passed to stored in the main program because only the values were passed to the function swap.
the function swap.
This is known as "call by value". This is known as "call by value".
16
Pointers and Functions Pointers and Functions
If instead of passing the values of the variables to the If instead of passing the values of the variables to the called function, we pass their addresses, so that the called function, we pass their addresses, so that the
called function can change the values stored in the called function can change the values stored in the
calling routine. This is known as "call by reference"
calling routine. This is known as "call by reference"
since we are
since we are referencing referencing the variables. the variables.
The following shows the swap function modified from a The following shows the swap function modified from a
"call by value" to a "call by reference". Note that the
"call by value" to a "call by reference". Note that the values are now actually swapped when the control is values are now actually swapped when the control is
returned to main function.
returned to main function.
17
Pointers with Functions Pointers with Functions
(example) (example)
#include <stdio.h>
#include <stdio.h>
void swap ( int *a, int *b ) ; void swap ( int *a, int *b ) ;
int main ( ) int main ( ) { {
int a = 5, b = 6; int a = 5, b = 6;
printf("a=%d b=%d\n",a,b) ; printf("a=%d b=%d\n",a,b) ; swap (&a, &b) ; swap (&a, &b) ;
printf("a=%d b=%d\n",a,b) ; printf("a=%d b=%d\n",a,b) ; return 0 ; return 0 ;
} }
void swap( int *a, int *b ) void swap( int *a, int *b ) { {
int temp; int temp;
temp= *a; *a= *b; *b = temp ; temp= *a; *a= *b; *b = temp ; printf ("a=%d b=%d\n", *a, *b); printf ("a=%d b=%d\n", *a, *b);
} }
Results:
Results:
a=5 b=6 a=5 b=6 a=6 b=5 a=6 b=5 a=6 b=5 a=6 b=5
18
Arithmetic and Logical Arithmetic and Logical Operations on Pointers Operations on Pointers
A pointer may be incremented or decremented. A pointer may be incremented or decremented.
An integer may be added to or subtracted from a An integer may be added to or subtracted from a pointer.
pointer.
Pointer variables may be subtracted from one Pointer variables may be subtracted from one another.
another.
Pointer variables can be used in comparisons, but Pointer variables can be used in comparisons, but usually only in a comparison to NULL.
usually only in a comparison to NULL.
19
Arithmetic Operations on Arithmetic Operations on
Pointers Pointers
When an integer is added to or subtracted from a When an integer is added to or subtracted from a
pointer, the new pointer value is changed by the integer pointer, the new pointer value is changed by the integer
times the number of bytes in the data variable the times the number of bytes in the data variable the
pointer is pointing to.
pointer is pointing to.
20