Pointer Notation: Variable
Pointer Notation: Variable
variable.
Pointer Notation
Consider the declaration,
int i = 3 ;
This declaration tells the C compiler to:
(a) Reserve space in memory to hold the integer value.
(b) Associate the name i with this memory location.
(c) Store the value 3 at this location.
is location in memory:
I
location name
3
Value at location
main( )
{
int i = 3 ;
printf ( "\n Address of i = %u", &i ) ;
printf ( "\n Value of i = %d", i ) ;
}
Output:
Address of i = 65524
Value of i = 3
& used in this statement is Cs address of
operator. The expression &i returns the
address of the variable i, which in this case
happens to be 65524.
65524
values at location
65524 65522 Address
As , is value is 3 and js value is is address.
Output:
Address of i = 65524
Address of i = 65524
Address of j = 65522
Value of j = 65524
Value of i = 3
Value of i = 3
Value of i = 3
Pointer, we know is a variable that contains address
of another variable. Now this variable itself might
be another pointer. Thus, we now have a pointer
that contains another pointers address.
int i, *j, **k ;
variable I
j=&i
3
value
address 65524 65522
65524
k=&j
65520
65522
main( )
{
int i = 3, *j, **k ;
j = &i ;
k = &j ;
printf ( "\nAddress of i = %u", &i ) ;
printf ( "\nAddress of i = %u", j ) ;
printf ( "\nAddress of i = %u", *k ) ;
printf ( "\nAddress of j = %u", &j ) ;
printf ( "\nAddress of j = %u", k ) ;
printf ( "\nAddress of k = %u", &k ) ;
printf ( "\nValue of j = %u", j ) ;
printf ( "\nValue of k = %u", k ) ;
printf ( "\nValue of i = %d", i ) ;
printf ( "\nValue of i = %d", * ( &i ) ) ;
printf ( "\nValue of i = %d", *j ) ;
printf ( "\nValue of i = %d", **k ) ;
}
a[0]
1000
1002
a[1]
1004
a[2]
1006
int * ptr;
ptr=a;
This is equivalent to
ptr=&a[0];
Now we can access every value of a
using ptr++ to move from one element
to another.
ptr
= &a[0] (=1000)
ptr+1 = &a[1] (=1002)
ptr+2 = &a[2] (=1004)
ptr+3 = &a[3] (=1006)
ptr+4 = &a[4] (=1008)
main()
{
int *p, sum ,i;
int x[5]={5,9,6,3,7};
i=0;
p=x;
printf(Element value address);
While(i<5)
{
printf(x[%d] %d %u,i,*p,p);
Sum=sum+ *p;
i++,p++;
}
printf(sum= %d ,sum);
printf(&x[0]=%u, &x[0]);
printf(p=%u, p);
}
Remember:
1. Addition of a number to a pointer;
int i=4,*j;
j=&I;
j=j+1; J=J+9;
2. Subtraction of a number from a pointer;
int i=4,*j;
j=&I;
j=j-1; J=J-4;
3.You can never perform:
a)Addition of two pointers.
b)Multiplying a pointer with a number.
c)Dividing a pointer with a number.
4. A pointer when incremented always points to an
immediately next location of its type.
a[0][0]
1234
1000
1018
56
1212
1002
1004
33
1434
1006
1008
80
1010
1312
1012
78
1014
1203
75
1016
or *(*(ptr +i)+j)
1234
1000
a[0][1]
56
1002
a[1][0]
a[1][1]
1004 1006
so on.
Pointer to an array
declaration: int (*q)[4] means that q is a pointer to an array of 4 integers.
main()
{
int a[][4]={
5,7,5,9,
4,6,3,1,
2,9,0 6
};
int *p; //p is integer pointer
int (*q)[4]; // q is a pointer to an array
p=(int *)a;
q=a;
printf(%u %u p,q);
p++;
q++;
printf(% u %u,p,q);
}
Output:
65500
65500
65502
65502
Three ways:
main()
{
int a[3][4]={
1,2,3,4,
5,6,7,8,
9,0 ,1,6
};
display(a,3,4);
show(a,3,4);
print(a,3,4);
}
1003
1004
1000
\0
1005
Pointers to Functions
A function,like a variable,has a type and an address
location in the memory,Thus it is possible to declare a
pointer to a function,which can then be used as an
argument in another function.
type (*fptr) ();
This tells the compiler that fptr is a pointer to a function
which returns type value.
We can make a function pointer to point to a
specific function by simply assigning the name of the
function to the pointer.
double mul (int, int);
double (*p) ();
p=mul;
This declares p as a pointer to a function and mul as a
function and then make p to point to the function mul.
main()
{
printf(Tablemof y(x)=2*x*x-x+1);
table(y,0.0,2.0,0.5);
printf(Table of cos(x));
table(cos,0.0,PI,0.5);
}
doule table(double (*f) (),double min,double max,double step)
{
double a,value;
for(a=min;a<=max;a+=step)
{
value=(*f) (a);
printf(%f %f,a,value);
}}
double y(double x)
{
return (2*x*x-x+1);
}
Genric Pointer
main()
{
void *ptr;
int a=5;
float b=9.7;
ptr=&a;
printf(%d,(*(int *)ptr));
ptr=&b;
printf(%d,(*(float *)ptr));
}