2. int main( )
{
int x = 10;
printf(âx = %d â , x); //Prints the value of x.
printf(â&x = %p â , &x);
}
mohammed.sikander@cranessoftware.com 2
& operator gets the address of variable.
%p is the correct format specifier to print address.
3. 1. int main( )
2. {
3. int x = 10;
4. printf(âx = %d nâ , x); //Prints the value of x.
5. printf(â&x = %p nâ , &x);
6. int *ptr; //Declaration of pointer
7. ptr = &x;
8. printf(âptr = %p nâ , ptr);
9. printf(â*ptr = %d nâ , *ptr);
10. }
mohammed.sikander@cranessoftware.com 3
6. * is used to declare a pointer
9. * is used to get value at address (dereference)
4. 1. int main( )
2. {
3. int x = 5, y = 8;
4. int *p1 , p2;
5. p1 = &x;
6. p2 = &y;
7. printf(â *p1 = %d â , *p1);
8. printf(â *p2 = %d â , *p2);
9. }
mohammed.sikander@cranessoftware.com 4
6. Pointer size is same irrespective of type.
Pointers are like shorcuts to files (in windows).
A size of shortcut to a 1MB file, 1GB file ,
200MB file is same
mohammed.sikander@cranessoftware.com 6
9. int main( )
{
int x;
printf(â x = %d â , x); //garbage value.
int *ptr;
printf(â ptr = %p â , ptr); //garbage address.
printf(â *ptr = %d â ,*ptr);
}
mohammed.sikander@cranessoftware.com 9
10. int main( )
{
int *ptr1; // unitialized pointer
int *ptr2 = NULL; // NULL pointer
if(ptr2 != NULL) // Can be safeguarded
printf(â *ptr2 = %d nâ, *ptr2);
if(ptr1 != NULL) // Cannot be safeguarded
printf(â *ptr1 = %d nâ, *ptr1);
}
mohammed.sikander@cranessoftware.com 10
11. void update(int x)
{
x = x + 5;
printf(âUpdate x = %d â , x);
}
int main( )
{
int x = 2;
update( x );
printf(âMain x = %d â , x);
}
mohammed.sikander@cranessoftware.com 11
12. void update(int *px)
{
*px = *px + 5;
printf(âUpdate *px = %d â , *px);
}
int main( )
{
int x = 2;
update( &x );
printf(âMain x = %d â , x);
}
mohammed.sikander@cranessoftware.com 12
15. a = 3 , b = 7
a = a + b; //10
b = a â b; //3
a = a â b; //7
mohammed.sikander@cranessoftware.com 15
a = 7 , b = 10
a = a * b; //70
b = a / b; //7
a = a / b; //10
a = 5 , b = 10
a = a ^ b; //
b = a ^ b; //5
a = a ^ b; //10
16. int main( )
{
int arr[ ] = {12,23,34,54};
printf(â %p â , &arr[0]);
printf(â %p â , arr);
}
mohammed.sikander@cranessoftware.com 16
Array name gives the base address (address of first
element of array )
17. int main( )
{
int arr[ ] = {12,23,34,54};
int *p1 = &arr[0];
int *p2 = arr;
printf(â %d â , *p1);
printf(â %d â , *p2);
}
mohammed.sikander@cranessoftware.com 17
18. The following arithmetic operations with pointers are legal:
âą add an integer to a pointer (+ and +=)
âą subtract an integer from a pointer(- and -=).
âą use a pointer as an operand to the ++ and -- operators.
âą subtract one pointer from another pointer, if they point
to objects of the same type.
âą compare two pointers
âą Operations meaningless unless performed on an array
âą All other arithmetic operations with pointers are illegal.
19. ïĄ Ptr = ptr + int
ïĄ When you add or subtract an integer to or from a
pointer, the compiler automatically scales the
integer to the pointer's type. In this way, the integer
always represents the number of objects to jump,
not the number of bytes.
ïĄ int x; // Assume address of x is 1000
ïĄ int *ptr = &x; // ptr = 1000
ïĄ ptr = ptr +1; // ptr-> 1004 and not 1001
31. ïĄ Constant Pointer : Pointer is fixed to one
location.
ïĄ Pointer to const : Pointer has read-only
access.
mohammed.sikander@cranessoftware.com 31
ïĄ int * const cp;
ïĄ const int * pc;
ïĄ Constant pointer should be initialized.
32. int main( )
{
int x = 10 ;
const int y = 20;
const int * pc1 ;
pc1 = &x;
pc1 = &y;
}
mohammed.sikander@cranessoftware.com 32
int main( )
{
int x = 10 ;
const int y = 20;
int * const cp1;
int * const cp2 = &x;
int * const cp3 = &y;
const int * const cp4 = &y;
cp2 = &y;
}
#include <stdio.h>
int main( )
{
char c , *pc;
short int si , *psi;
int i , *pi;
double d , *pd;
printf(" c %d pc %d \n" , sizeof(c) , sizeof(pc));
printf(" si %d psi %d \n" , sizeof(si) , sizeof(psi));
printf(" i %d pi %d \n" , sizeof(i) , sizeof(pi));
printf(" d %d pd %d \n" , sizeof(d) , sizeof(pd));
return 0;
}
Use typecasting to remove warnings
#include <stdio.h>
void swap( int *pa , int *pb)
{
int temp = *pa;
*pa = *pb;
*pb = temp;
printf(" *pa = %d *pb = %d \n" ,*pa , *pb);
}
int main( )
{
int a = 5 , b = 10;
swap( &a , &b );
printf("a = %d b = %d \n" , a , b);
}
#include <stdio.h>
void swap( int *pa , int *pb)
{
int *temp = pa;
pa = pb;
pb = temp;
printf(" *pa = %d *pb = %d \n" ,*pa , *pb);
}
int main( )
{
int a = 5 , b = 10;
swap( &a , &b );
printf("a = %d b = %d \n" , a , b);
}