A pointer is a variable it
holds the value and address of variable, i.e.,
direct address of the memory location. Like any variable or constant, you must
declare a pointer before you can use it
Syntax:-
Datatype
*pointername;
to store any
variable value and address.
int *a,x;
x=10;
a=&x;
x,*a values and
addresses are same. Because if you remove the * pointer holds
the value and address of the variable.
&x means it
sends the value and address also.
int *b,y;
y=20;
*b=y;
y,*b values are
same. Because if you use * for pointer it holds
the value but not the address.
y means it sends
the value it willnot sends the address.
For printing the
address we use %u.
#include<stdio.h>
main()
{
int *a,*b,x,y;
x=10;
y=20;
clrscr();
a=&x;
*b=y;
printf("\nThe
*a value is %d and address is %u",*a,a);
printf("\nThe
x value is %d and address is %u",x,&x);
printf("\nThe
*b value is %d and address is %u",*b,b);
printf("\nThe
y value is %d and address is %u",y,&y);
getch();
}
Every pointer
occupies 2 bytes.
#include<stdio.h>
main()
{
int *a;
char *b;
float *c;
double *d;
clrscr();
printf("\nThe
Integer pointer occupies %d bytes",sizeof(a));
printf("\nThe
Character pointer occupies %d bytes",sizeof(b));
printf("\nThe
float pointer occupies %d bytes",sizeof(c));
printf("\nThe
double pointer occupies %d bytes",sizeof(d));
getch();
}
WAP findout hcf
of two numbers with using pointers.
#include<stdio.h>
main()
{
int
*num1,*num2,i,*hcf;
clrscr();
printf("Enter
two numbers");
scanf("%d%d",num1,num2);
for(i=1;i<=*num1||i<=*num2;i++)
{
if(*num1%i==0
&& *num2%i==0)
*hcf=i;
}
printf("H.C.F
of %d and %d is %d",*num1,*num2,*hcf);
getch();
}
WAP findout lcm
of two numbers with using pointers.
#include<stdio.h>
main()
{
int
*num1,*num2,i,*hcf,*lcm;
clrscr();
printf("Enter
two numbers");
scanf("%d%d",num1,num2);
for(i=1;i<=*num1||i<=*num2;i++)
{
if(*num1%i==0
&& *num2%i==0)
*hcf=i;
}
*lcm=(((*num1)*(*num2))/(*hcf));
printf("L.C.M
of %d and %d is %d",*num1,*num2,*lcm);
getch();
}
Pointer
Arithmetic:-
There are four
arithmetic operators that can be used on pointers: ++,--, +, -
#include<stdio.h>
main()
{
int *p,a,b,*q;
clrscr();
printf("Enter
a,b values");
scanf("%d%d",&a,&b);
*p=a;
q=&b;
printf("\nThe
a value is %d address is %u",a,&a);
printf("\nThe
*P value is %d address is %u",*p,p);
printf("\nThe
b value is %d address is %u",b,&b);
printf("\nThe
*q value is %d address is %u",*q,q);
a++;
b++;
printf("\nThe
a value is %d address is %u",a,&a);
printf("\nThe
*P value is %d address is %u",*p,p);
printf("\nThe
b value is %d address is %u",b,&b);
printf("\nThe
*q value is %d address is %u",*q,q);
p++;
q++;
printf("\nThe
a value is %d address is %u",a,&a);
printf("\nThe
*P value is %d address is %u",*p,p);
printf("\nThe
b value is %d address is %u",b,&b);
printf("\nThe
*q value is %d address is %u",*q,q);
getch();
}
If you increment
a variable it increments the value if you increment a pointer it increments the
address.
Pointers using
arithmetic operators
#include<stdio.h>
main()
{
int *a,*b,p,q;
clrscr();
printf("Enter
two numbers");
scanf("%d%d",&p,&q);
a=&p;
*b=q;
printf("\nThe
addition of two nubmers is %d",*a+*b);
printf("\nThe
difference of two numbers is %d",*a-q);
printf("\nThe
product of two numbers is %d",p**b);
printf("\nThe
divisiob of two numbers is %d",(*a)/(*b));
getch();
}
Pointers with
Arrays:- Array itself is converted to a pointer.
a[i] is
represented as *(a+i)
&a[i] is
represented as (a+i)
Array reading and
printing with pointer style
#include<stdio.h>
main()
{
int a[10],i;
clrscr();
printf("Enter
an array values:");
for(i=0;i<5;i++)
scanf("%d",(a+i));
printf("\nThe
array elements are:");
for(i=0;i<5;i++)
printf("\n%d\t%u",*(a+i),(a+i));
}
#include
<stdio.h>
const int MAX = 3;
main ()
{
int var[] = {10,
100, 200};
int i, *ptr;
/* let us have
array address in pointer */
ptr = var;
for ( i = 0; i
< MAX; i++)
{
printf("Address
of var[%d] = %u\n", i, ptr );
printf("Value
of var[%d] = %d\n", i, *ptr );
/* move to the
next location */
ptr++;
}
}
wap to findout
the smallest number using pointers.
#include<stdio.h>
main()
{
int a[10],i,*p;
clrscr();
printf("Enter
an array values:");
for(i=0;i<5;i++)
scanf("%d",(a+i));
*p=*(a+0);
for(i=1;i<5;i++)
{
if(*(a+i)<*p)
*p=*(a+i);
}
printf("\nThe
smallest element in the array list is %d",*p);
getch();
}
NULL Pointers in C:-
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 zero defined in
several standard libraries.
#include
<stdio.h>
void main ()
{
int *ptr = NULL;
printf("The
value of ptr is : %d\n", *ptr );
}
Pointers with
strings:-
String itself is
converted to a pointer.
a[i] is
represented as *(a+i)
&a[i] is
represented as (a+i)
Wap to count the
no.of characters in the string without counting spaces.
#include<stdio.h>
main()
{
char a[20];
int i,c=0;
clrscr();
printf("Enter
a string:");
gets(a);
for(i=0;*(a+i)!='\0';i++)
{
if(*(a+i)!=32)
c++;
}
printf("\nThe
no.of characters in the given string without spaces is %d",c);
getch();
}
Wap to print the
given string in vertical.
#include<stdio.h>
main()
{
char a[20];
int i;
clrscr();
puts("Enter
a string");
gets(a);
printf("\nThe
string in vertical is:");
for(i=0;*(a+i)!='\0';i++)
printf("\n%c",*(a+i));
}
WAP read a string
print the no.of characters, no.of capitals, no.of smalls, no.of digits, no.of
vowels, no.of consonents, no.of words.
#include<stdio.h>
main()
{
char a[50];
int
i,nc,ns,nw,nd,tc,nv,nco;
clrscr();
nc=ns=nd=tc=nw=nv=nco=0;
puts("Enter
a string");
gets(a);
for(i=0;*(a+i)!='\0';i++)
{
if(*(a+i)=='a'||*(a+i)=='e'||*(a+i)=='i'||*(a+i)=='o'||*(a+i)=='u')
nv++;
else
nco++;
if(*(a+i)>=65
&& *(a+i)<=90)
{
nc++;
tc++;
}
else
if(*(a+i)>=97 && *(a+i)<=122)
{
ns++;
tc++;
}
else
if(*(a+i)>=48 && *(a+i)<=57)
{
nd++;
tc++;
}
else if(*(a+i)=='
')
{
nw++;
tc++;
}
}
printf("\nThe
no.of characters in the string is %d",tc);
printf("\nThe
no.of capitals in the string is %d",nc);
printf("\nThe
no.of smalls in the string is %d",ns);
printf("\nThe
no.of vowels in the string is %d",nv);
printf("\nThe
no.of consonents in the string is %d",nco);
printf("\nThe
no.of digits in the string is %d",nd);
printf("\nThe
no.of words in the string is %d",nw+1);
getch();
}
Call by value:- When formal
parameters are modified actual parameters will not get any effect. That is
nothing but call by value. Called function parameters are actual parameters.
Implementation function parameters are formal parameters.
Wap to perform
addition by adding 10.
#include<stdio.h>
void add(int x)
{
x=x+10;
}
main()
{
int p;
printf("Enter
one value");
scanf("%d",&p);
printf(“Before
calling p=%d”,p);
add(p);
printf(“After
calling p=%d”,p);
getch();
}
Pointer with
Functions:-
Call by reference:- When formal
parameters are modified actual parameters will get any effect. That is nothing
but call by Reference. Called function parameters are actual parameters.
Implementation function parameters are formal parameters.
Wap to perform
addition by adding 10.
#include<stdio.h>
void add(int *x)
{
*x=*x+10;
}
main()
{
int p;
printf("Enter
one value");
scanf("%d",&p);
printf(“Before
calling p=%d”,p);
add(&p);
printf(“After
calling p=%d”,p);
getch();
}
WAP to perform
swapping
#include<stdio.h>
void swap(int *x,int
*y)
{
*x+=*y;
*y=*x-*y;
*x-=*y;
}
main()
{
int p,q;
printf("Enter
two values");
scanf("%d%d",&p,&q);
printf("\nBefore
swapping p=%d and q=%d",p,q);
swap(&p,&q);
printf("\nAfter
swapping p=%d and q=%d",p,q);
getch();
}
Pointer with
pointer:- variable value and address can hold by pointer. Pointer value
and address can hold by pointer with pointer.
#include<stdio.h>
main()
{
int x,*p,**px;
clrscr();
x=10;
p=&x;
px=&p;
printf("\nx
value is %d and address is %u",x,&x);
printf("\n*p
value is %d and address is %u",*p,p);
printf("\n**px
value is %d and address is %u",**px,*px);
getch();
}A pointer is a variable it
holds the value and address of variable, i.e.,
direct address of the memory location. Like any variable or constant, you must
declare a pointer before you can use it
Syntax:-
Datatype
*pointername;
to store any
variable value and address.
int *a,x;
x=10;
a=&x;
x,*a values and
addresses are same. Because if you remove the * pointer holds
the value and address of the variable.
&x means it
sends the value and address also.
int *b,y;
y=20;
*b=y;
y,*b values are
same. Because if you use * for pointer it holds
the value but not the address.
y means it sends
the value it willnot sends the address.
For printing the
address we use %u.
#include<stdio.h>
main()
{
int *a,*b,x,y;
x=10;
y=20;
clrscr();
a=&x;
*b=y;
printf("\nThe
*a value is %d and address is %u",*a,a);
printf("\nThe
x value is %d and address is %u",x,&x);
printf("\nThe
*b value is %d and address is %u",*b,b);
printf("\nThe
y value is %d and address is %u",y,&y);
getch();
}
Every pointer
occupies 2 bytes.
#include<stdio.h>
main()
{
int *a;
char *b;
float *c;
double *d;
clrscr();
printf("\nThe
Integer pointer occupies %d bytes",sizeof(a));
printf("\nThe
Character pointer occupies %d bytes",sizeof(b));
printf("\nThe
float pointer occupies %d bytes",sizeof(c));
printf("\nThe
double pointer occupies %d bytes",sizeof(d));
getch();
}
WAP findout hcf
of two numbers with using pointers.
#include<stdio.h>
main()
{
int
*num1,*num2,i,*hcf;
clrscr();
printf("Enter
two numbers");
scanf("%d%d",num1,num2);
for(i=1;i<=*num1||i<=*num2;i++)
{
if(*num1%i==0
&& *num2%i==0)
*hcf=i;
}
printf("H.C.F
of %d and %d is %d",*num1,*num2,*hcf);
getch();
}
WAP findout lcm
of two numbers with using pointers.
#include<stdio.h>
main()
{
int
*num1,*num2,i,*hcf,*lcm;
clrscr();
printf("Enter
two numbers");
scanf("%d%d",num1,num2);
for(i=1;i<=*num1||i<=*num2;i++)
{
if(*num1%i==0
&& *num2%i==0)
*hcf=i;
}
*lcm=(((*num1)*(*num2))/(*hcf));
printf("L.C.M
of %d and %d is %d",*num1,*num2,*lcm);
getch();
}
Pointer
Arithmetic:-
There are four
arithmetic operators that can be used on pointers: ++,--, +, -
#include<stdio.h>
main()
{
int *p,a,b,*q;
clrscr();
printf("Enter
a,b values");
scanf("%d%d",&a,&b);
*p=a;
q=&b;
printf("\nThe
a value is %d address is %u",a,&a);
printf("\nThe
*P value is %d address is %u",*p,p);
printf("\nThe
b value is %d address is %u",b,&b);
printf("\nThe
*q value is %d address is %u",*q,q);
a++;
b++;
printf("\nThe
a value is %d address is %u",a,&a);
printf("\nThe
*P value is %d address is %u",*p,p);
printf("\nThe
b value is %d address is %u",b,&b);
printf("\nThe
*q value is %d address is %u",*q,q);
p++;
q++;
printf("\nThe
a value is %d address is %u",a,&a);
printf("\nThe
*P value is %d address is %u",*p,p);
printf("\nThe
b value is %d address is %u",b,&b);
printf("\nThe
*q value is %d address is %u",*q,q);
getch();
}
If you increment
a variable it increments the value if you increment a pointer it increments the
address.
Pointers using
arithmetic operators
#include<stdio.h>
main()
{
int *a,*b,p,q;
clrscr();
printf("Enter
two numbers");
scanf("%d%d",&p,&q);
a=&p;
*b=q;
printf("\nThe
addition of two nubmers is %d",*a+*b);
printf("\nThe
difference of two numbers is %d",*a-q);
printf("\nThe
product of two numbers is %d",p**b);
printf("\nThe
divisiob of two numbers is %d",(*a)/(*b));
getch();
}
Pointers with
Arrays:- Array itself is converted to a pointer.
a[i] is
represented as *(a+i)
&a[i] is
represented as (a+i)
Array reading and
printing with pointer style
#include<stdio.h>
main()
{
int a[10],i;
clrscr();
printf("Enter
an array values:");
for(i=0;i<5;i++)
scanf("%d",(a+i));
printf("\nThe
array elements are:");
for(i=0;i<5;i++)
printf("\n%d\t%u",*(a+i),(a+i));
}
#include
<stdio.h>
const int MAX = 3;
main ()
{
int var[] = {10,
100, 200};
int i, *ptr;
/* let us have
array address in pointer */
ptr = var;
for ( i = 0; i
< MAX; i++)
{
printf("Address
of var[%d] = %u\n", i, ptr );
printf("Value
of var[%d] = %d\n", i, *ptr );
/* move to the
next location */
ptr++;
}
}
wap to findout
the smallest number using pointers.
#include<stdio.h>
main()
{
int a[10],i,*p;
clrscr();
printf("Enter
an array values:");
for(i=0;i<5;i++)
scanf("%d",(a+i));
*p=*(a+0);
for(i=1;i<5;i++)
{
if(*(a+i)<*p)
*p=*(a+i);
}
printf("\nThe
smallest element in the array list is %d",*p);
getch();
}
NULL Pointers in C:-
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 zero defined in
several standard libraries.
#include
<stdio.h>
void main ()
{
int *ptr = NULL;
printf("The
value of ptr is : %d\n", *ptr );
}
Pointers with
strings:-
String itself is
converted to a pointer.
a[i] is
represented as *(a+i)
&a[i] is
represented as (a+i)
Wap to count the
no.of characters in the string without counting spaces.
#include<stdio.h>
main()
{
char a[20];
int i,c=0;
clrscr();
printf("Enter
a string:");
gets(a);
for(i=0;*(a+i)!='\0';i++)
{
if(*(a+i)!=32)
c++;
}
printf("\nThe
no.of characters in the given string without spaces is %d",c);
getch();
}
Wap to print the
given string in vertical.
#include<stdio.h>
main()
{
char a[20];
int i;
clrscr();
puts("Enter
a string");
gets(a);
printf("\nThe
string in vertical is:");
for(i=0;*(a+i)!='\0';i++)
printf("\n%c",*(a+i));
}
WAP read a string
print the no.of characters, no.of capitals, no.of smalls, no.of digits, no.of
vowels, no.of consonents, no.of words.
#include<stdio.h>
main()
{
char a[50];
int
i,nc,ns,nw,nd,tc,nv,nco;
clrscr();
nc=ns=nd=tc=nw=nv=nco=0;
puts("Enter
a string");
gets(a);
for(i=0;*(a+i)!='\0';i++)
{
if(*(a+i)=='a'||*(a+i)=='e'||*(a+i)=='i'||*(a+i)=='o'||*(a+i)=='u')
nv++;
else
nco++;
if(*(a+i)>=65
&& *(a+i)<=90)
{
nc++;
tc++;
}
else
if(*(a+i)>=97 && *(a+i)<=122)
{
ns++;
tc++;
}
else
if(*(a+i)>=48 && *(a+i)<=57)
{
nd++;
tc++;
}
else if(*(a+i)=='
')
{
nw++;
tc++;
}
}
printf("\nThe
no.of characters in the string is %d",tc);
printf("\nThe
no.of capitals in the string is %d",nc);
printf("\nThe
no.of smalls in the string is %d",ns);
printf("\nThe
no.of vowels in the string is %d",nv);
printf("\nThe
no.of consonents in the string is %d",nco);
printf("\nThe
no.of digits in the string is %d",nd);
printf("\nThe
no.of words in the string is %d",nw+1);
getch();
}
Call by value:- When formal
parameters are modified actual parameters will not get any effect. That is
nothing but call by value. Called function parameters are actual parameters.
Implementation function parameters are formal parameters.
Wap to perform
addition by adding 10.
#include<stdio.h>
void add(int x)
{
x=x+10;
}
main()
{
int p;
printf("Enter
one value");
scanf("%d",&p);
printf(“Before
calling p=%d”,p);
add(p);
printf(“After
calling p=%d”,p);
getch();
}
Pointer with
Functions:-
Call by reference:- When formal
parameters are modified actual parameters will get any effect. That is nothing
but call by Reference. Called function parameters are actual parameters.
Implementation function parameters are formal parameters.
Wap to perform
addition by adding 10.
#include<stdio.h>
void add(int *x)
{
*x=*x+10;
}
main()
{
int p;
printf("Enter
one value");
scanf("%d",&p);
printf(“Before
calling p=%d”,p);
add(&p);
printf(“After
calling p=%d”,p);
getch();
}
WAP to perform
swapping
#include<stdio.h>
void swap(int *x,int
*y)
{
*x+=*y;
*y=*x-*y;
*x-=*y;
}
main()
{
int p,q;
printf("Enter
two values");
scanf("%d%d",&p,&q);
printf("\nBefore
swapping p=%d and q=%d",p,q);
swap(&p,&q);
printf("\nAfter
swapping p=%d and q=%d",p,q);
getch();
}
Pointer with
pointer:- variable value and address can hold by pointer. Pointer value
and address can hold by pointer with pointer.
#include<stdio.h>
main()
{
int x,*p,**px;
clrscr();
x=10;
p=&x;
px=&p;
printf("\nx
value is %d and address is %u",x,&x);
printf("\n*p
value is %d and address is %u",*p,p);
printf("\n**px
value is %d and address is %u",**px,*px);
getch();
}
No comments:
Post a Comment