Pointer
Pointer is a variable
whose value is the address of another variable I.e direct address of the memory location
As you know, every variable is a
memory
location and every memory location has its address defined
which can be accessed
the dereference operator and can be read as “value pointed by”
General form
Type
*var-name;
Here type is the pointer’s base type; it must be a validC data type and var-name is the name of the pointervariable. The asterisk * you used to declare a pointer is the same asterisk that you use for multiplication. however , in this statements the astric is being used to designate a variable as a pointer
Following are the
valid pointer declaration :
Int *p; /*pointer
to an integer*/
Double
*dp; /*pointer to a double*/
Float *fp; /*pointer
to a float*/
Char *ch; /*pointer
to a character*/
The actual data type of the value of all pointers, whether integer, float, character, or otherwise,is the same a long hexadecimal number that represents a memory address. The only deference between pointer of different data types is the data type of variable or constant that the pointer pointer to.Variable referenced with & can be dereference with *
Sandy=30;
Sam=&sandy;
All expressions below are true:
Sandy ==30 //true
&sandy ==12334 //true
Sam == 12334 //true
*Sam ==30 //true
Declaration and accessing
values through a pointer:
There are few important operation which we will do with the help of pointer very frequently We defined a pointer variable Assign the address of a variable to a pointer Finally access the value at the address available in the pointer variable
This
is done by using unary operator * that returns
the
value of the variable located at the address
specified
by its operand. Following example makes
use of
these operation:
#include<stdio.h>
int main ()
{
int var=20;
int*ip;
ip=&var;
printf(“\n
address of var variable :%x”,&var);
printf(“\n
address stored in ip variable : %x ” ,ip);
printf(“\n
value of *ip variable :%d”,*ip);
returen();
}
When
the above code is compiled and executed,
it
produces result something as follows:
Address
of var variable:bffd8b3c
Address
store in ip variable: :bffd8b3c
Value
of *ip variable:20
Null pointer in c
It
always a good practice to assign a null value to a
pointer
variable in case you do not have exact
address
to be assigned. this is done at the time of
variable
declaration. A pointer that is assigned NULL
is
called a null pointer
The NULL pointer is a constant with a
value of
#include<stdio.h>
int main()
{
int
*ptr=NULL;
printf
(“the value of ptr is :%x \n”,ptr);
return
0;
}
Operations on pointers
You know that address in a memory is
a numeric value
we can perform arithmetic operation
on the pointers
values some of the different
operations that can be
performed On pointer are
Incrementing / decrementing a pointer
Any
type of pointer variable when incremented
they points to the next memory
location of its type
eg
int a=20,*x;
x=&
a;
x++
locations.
ex
#include<stdio.h>
#include<conio.h>
Void main ()
{
int
num [ ] ={ 11,12,13,14,15}; *p,i;
clrscr(
);
P=&num [ 0 ];
for ( i = 0; i <
5; i++ );
{
printf
(“\n %d=%u”,*p,p);
p++;
}
getch();
}
Decrement operator
#include<stdio.h>
int main( )
{
int
i=0;
while(
i- - > 5)
{
printf
(“%d”,i);
}
return 0;
}
Additions /subtraction of a pointer
#include<stdio.h>
int subtraction (int *x, int *y
);
int main()
{
int
num 1,num 2,res;
printf
(“\n first number : \t”);
scanf
(“ %d”,&num1);
printf
(“ \n second number :\t ”);
scanf
( “%d”,&num2 );
res
= subtraction ( &num1,num2 );
printf
(“subtraction of two numbers: %d,res”);
printf
(“\n”);
return
0;
}
int substract(int *x, int* y)
{
int
temp;
temp=*x-
*y;
return
temp;
}
Output
first number : 50
second number: 30
substraction of two number :20
Addition
#include<stdio.h>
int main()
{
int
first, second,*p,*q,sum;
printf
(“enter two integers to add \n”);
scanf(“%d%d”,&first,&second);
p=&first;
q=&second;
Sum=*p+*
q;
printf(“sum
of the numbers=%d \n”,sum);
return
0;
}
Out put
Enter two integer to add
5
5
Sum of entered number=10
Function and pointer
#include<stdio.h>
#include<conio.h>
Void swap(int *a , int *b );
Void main ()
{
int
m=10, n=20;
clrscr();
printf(“m=%d
\n”,m);
printf(“n=%d\n\n”,n);
swap(&m
, &n);
printf(“after
swapping: \n \n”);
printf(“m=%d\n”,m);
printf(“n=%d”,n);
return 0;
}
Void swap (int *a, int*b );
{
int
t;
t=*a;
*a=*b;
*b=t;
}
Output:
m=10
n=20
After swapping:
m=20
n=10
Call by value
#include<stdio.h>
#include<conio.h>
Void swap (int a,int b)
{
int temp;
temp =a;
a=b;
b=temp;
}
Void main ()
int a=100,b=200;cClrscr();
swap(a,b);
printf(“\n value of a:%d”,a);
printf(“\nvalue of b:%d”,b);
getch();
}
Output
Value of a: 200
Value of b: 100
Pointer to pointer (double pointer)
Syntax:
Data-
type ** pointer name;
Pointer
pointer
variable
address
-------->address --------->value
int i =10;
int *ip = &i;
int **iip = &p;
Here ip is
of type ( int *) or
pointer to int ,iip is of
type (int **)or pointer to pointer to int
Address of
iip address of ip
address of i
8000
5000
1000
5000
-----> 1000 ------->10
Iip
ip
i
(int **)or
(int*) or
Pointer to pointer
pointer to int
To int
We know that *ip will give value at address ip i.e
value of i can you guess what value will **iip will
return? =>**iip
We know that indirection operator
evaluated from
right to left so **iip can also be written as
*(*iip)
*iip mean value at address iip or
address stored at
ip on dereferencing address stored at ip we
will get
the value stored in the variable i
1. *(iip)
2. *ip
3. i
Therefore **iip gives the value stored in the
variable i
Ex
#include<stdio.h>
int main ()
{
int a = 10;iInt *p1;
Int **p2;
p1=&a;
p2=&p1;
printf (“address of a
=%u\n”, &a);
Printf (“address of p1 =
%u\n”,&p1);
Printf (“address of p1 =%u\n”
&p2);
Printf (“value at the address stored
by p2= %u\n”,*p2);
Printf (“value at the address stored
by p1= %d\n\n”,*p1);
Printf (“value of **p2=%d\n”,**p2);
return 0 ;
}
Output:
Address of a = 6356788
Address of p1= 6356784
Address of p2 =6356780
Value of the address stored by
p2=6356788
Value of the address stored by p1
=10
Value of **p2=10
Array of pointer
We can declare in program an array
of int, float , or
char etc.we can also declare an
array of pointer
Syntax
Data –type *array –name [size];
eg
int *a[3];
here a is an array of 3 integer
pointers. It mean that
array can hold the address of
3 integer variables or in
other words you can assign 3 pointer
variable of type
pointer to int to the elements of
this array
eg
#include<stdio.h>
#define SIZE10
int main ()
{
int *a [3];
int b =10,c = 20,d
= 50 , i ;
a [ 0 ] =&b;
a [1]=&c;
a [2]=&d;
for ( i = 0 ; i<3;
i++)
{
Printf (“address=%d\t
value=%d\n”,a[ i ],*a[ i ] );
}
return 0;
}
Output
Address
=387130656
value = 10
Address =
387130660 value =
20
Address =
387130664 value =
50
No comments:
Post a Comment