Anda di halaman 1dari 25

Def: A pointer is a variable that contains the address of a

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

65524 location number

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.

The other pointer operator available in C is *,


called value at address operator. It gives the
value stored at a particular address. The value
at address operator is also called indirection
operator.
main( )
{
int i = 3 ;
printf ( "\n Address of i = %u", &i ) ;
printf ( "\n Value of i = %d", i ) ;
printf ( "\n Value of i = %d", *( &i ) ) ;
}
Output:
Address of i = 65524
Value of i = 3
Value of i = 3

Note that printing the value of *( &i ) is same as


printing the value of i.
The expression &i gives the address of the
variable i. This address can be collected in a
variable, by saying,
j = &i ;
But remember that j is not an ordinary variable
like any other integer variable. It is a variable
that contains the address of other variable.
I j=&i
location name
3

65524

values at location
65524 65522 Address
As , is value is 3 and js value is is address.

since j is a variable that contains the address of i, it is


declared as,
int *j ;
main( )
{
int i = 3 ;
int *j ;
j = &i ;
printf ( "\nAddress of i = %u", &i ) ;
printf ( "\nAddress of i = %u", j ) ;
printf ( "\nAddress of j = %u", &j ) ;
printf ( "\nValue of j = %u", j ) ;
printf ( "\nValue of i = %d", i ) ;
printf ( "\nValue of i = %d", *( &i ) ) ;
printf ( "\nValue of i = %d", *j ) ;
}

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 ) ;
}

The output of the above program would be:


Address of i = 65524
Address of i = 65524
Address of i = 65524
Address of j = 65522
Address of j = 65522
Address of k = 65520
Value of j = 65524
Value of k = 65522
Value of i = 3
Value of i = 3
Value of i = 3
Value of i = 3

Pointers and 1-D Arrays


When an array is declared, the compiler allocates
a base address and sufficient amount of storage
to contain all the elements of the array in
contiguous memory locations.
The base address is the location of thr first
element(index 0) of the array.The compiler also
defines the array name as a constant pointer to
the first element.
int a[5]={1,2,3,4,5};
Elements
a[3]
a[4]
Values
Address
1000
1008

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)

The address of an element is


calculated using its index and the
scale factor of the data type.
address of a[3]=base address +
(3 * scale factor of int)
= 1000 + (3 * 2)=1006
In order to access the value stored at
a[0],
*ptr= *a = * (& a[0])=a[0]=1
2nd element:
*(ptr+1)= * (a+1)=*(&a[0]+1)=2
So on

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.

Passing an entire array to a function


main()
{
int num[]={24,34,12,44,56,17};
display(&num[0],6); // //display(num,6);
}
display(int *j,int n)
{
int i=1;
while(i<=n)
{
printf(%d,*j);
i++;
j++; //increment pointer to next location
}
}

Pointers and 2-D Arrays


main()
{
int a[5][2]={
{ 1234,56},
{1212,33},
{1434,80},
{1312,78},
{1203,75}
};
int I,j;
for(i=0;i<=4;i++)
{
for (j=0;j<=1;j++)
printf(%d, *(*(a +i) + j);
}
}

a[0][0]

1234
1000
1018

a[0][1] a[1][0] a[1][1] a[2][0] a[2][1] a[3][0] a[3][1] a[4][0] a[4][1]

56

1212

1002

1004

33

1434

1006

1008

80
1010

To access element a[i][j]: *(*(a+i)+j)


(*(a+i)+j) gives address

1312
1012

78
1014

1203

75

1016

or *(*(ptr +i)+j)

To access element a[2][1]:


We know a[2] would give the address 1008,the address of 2 nd
1-D array.
(1008 + 1) would give the address 1010.
a[0][0]

1234

1000

a[0][1]

56

1002

a[1][0]

a[1][1]

1004 1006

Base add of a[0] Base add of a[1]

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

Passing 2-D array to a function

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);
}

display( int *q,int row,int col)


{ int i,j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
printf(%d *(q+I * col +j));
}}
show(int (*q)[4],int row,int col)
{ int I , j ; int *p;
for(i=0;i<row;i++)
{
p=q+i;
for(j=0;j<col;j++)
printf(%d,*(p+j));
}}

print(int q[][4],int row,int col) //int q[][4]==int(*q)[4]


{
int i,j;
for(i=0;i<row;i++)
{
for(j=0;j<col;j++)
printf(%d,q[i][j])
}}
Output:
1234
5678
9016
so on.

Pointers and character strings


main()
{
char *name;
int length;
D
E
char * ptr=name;
name=DELHI;
printf(%s,name); 1000 1001
1002
while(*ptr !=\0)
{ ptr
printf(%c is at location %u,*ptr,ptr);
ptr++;
}
length= ptr-name;
5000
printf(length is %d,length);
}

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.

To call the function mul,we may now


use the pointer p with the list of
parameters.
(*p) (x,y) //func calli
is equivalent to:
mul (x,y);
#include<math.h>
#define PI 3.1415
double y(double);
double cos(double);
double table (double(*f) (),double, double,
double);

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));
}

Anda mungkin juga menyukai