Unit 1pointers
Unit 1pointers
int a = 5 a 6
int *a_ptr;
a_ptr = &a;
/* ‘&’ : address of operator */ a_ptr address
of ‘a’
(*a_ptr) ++ ;
/* ‘*’ : dereference operator */
Pointer example
Pointer Expression
■ j=&i;
Here j is not an ordinary variable like any other integer
variable.
It is a variable which contain the address of another
variable. ( i )
Ex.
i=3;
j=&i; // j’s value is address of i.
j is a variable which contains the address of i.
int *j;
Means value at the address contained in j as an integer.
i j
3 6485
6485 3276
Ex.
■ Main()
{
int i=3;
int *j;
j=&i;
printf(“\n Address of i=%u”,&i);
printf(“\n Address of j=%u”,&j);
printf(“\n Value of j=%d” ,j);
printf(“\n Value of i=%d”, i);
printf(“\n Value of i=%d”,*(&i));
printf(“\n Value of i=%d”,*j);
}
■ OUTPUT:
Address of i 6485
Address of j 3276
Value of j 6485
Value of i 3
Value of i 3
Value of i 3
i j k
3 6485 3276
int **k;
j=&i;
k=&j;
printf(\n Address of i=%u” , &i);
printf(\n Address of i=%u” , j);
printf(\n Address of i=%u” , *k);
printf(\n Address of j=%u” , &j);
printf(\n Address of j=%u” , k);
printf(\n Address of k=%u” , &k);
printf(“\n ----------------------”);
printf(\n value of j=%u” , j);
printf(\n value of k=%u” , k);
printf(\n value of i=%d” , i);
printf(\n value of i=%d” ,*(&i));
printf(\n value of i=%d” , *j);
printf(\n value of i=%d” , **k);
}
■ Address of i= 6485
Address of i= 6485
Address of i= 6485
Address of j= 3276
Address of j= 3276
Address of k=7234
----------------------
value of j= 6485
value of k= 3276
value of i=3
value of i=3
value of i=3
value of i=3
Pointer expression
■ float *s;
■ char *ch;
means value at address stored in ch is
going to be a char.
Passing pointer as argument to a
function
main()
{
int a=5;
void sum (int *b);
printf(“ Now value of a will be incremented”);
sum (&a);
}
void sum (int *x)
{
*x=*x+5;
printf(“%d”,*x);
}
Pointer to function
int * sum (int , int)
main()
{
int a,b;
int *x;
printf(“\n Enter the value of a”);
scanf(“%d”,&a);
printf(“\n Enter the value of b”);
scanf(“%d”, &b);
x=sum(a,b);
printf(“\n The addition of two no.s is %d”, *x);
}
int *sum(int x, int y)
{
int *c, p;
p=x+y;
c=&p;
return c;
}
Pointer to Array
■ Accessing array element using pointer
main()
{
int num[ ]={24,34,12,44,56,17};
int i=0,*j;
j=&num[0];
while(i<=5)
{
printf(“\n Address = %u”,&num[i]);
printf(“Element=%d”, *j);
i++;
j++;
}
}
Address = 100 Element =24
Address = 102 Element =34
Address = 104 Element =12
Address = 106 Element =44
Address = 108 Element =56
Address = 110 Element =17
Passing Entire Array to function:
main()
{
int num[ ]={24,34,12,44,56,17};
display(&num[0],6);
}
display(int *j,int n)
{
int I;
for(i=0;i<n;i++)
{
printf(“element= %d”,*j);
j++;
}
■ int num[]={24,34,12,44,56,17}
■ When we refer name of array we get it’s base
address.
■ *num:refer to zeroth element of the array
■ *num=*(num+0)
■ *(num +1):refer to first element
■ When we will write num[i] compiler converts it
to *(num+i)
■ So
■ num[i]
■ *(num+i)
■ *(i+num)
■ i[num] all are same.
Real Thing
main()
{
int num[ ] ={23,34,12,44,56,17};
int i=0;
while(i < =5)
{
printf (“\n address =%u “ ,&num[i]);
printf (“Elements = %d”, num[i]);
printf(“%d”,*(num+i);
printf(“%d”,*(i+num);
printf(“%d” ,i [num]);
i++;
}
}
o/p:
■ Address=100 Elements 23 23 23 23
■ Address=102 elements 34 34 34 34
■ Address=104 Elements 12 12 12 12
■ Address=106 elements 44 44 44 44
■ Address=108 Elements 56 56 56 56
Address=110 elements 17 17 17 17