Monday 23 February 2015

Pointers in C


What is pointer?

Pointer is a variable which holds the address of another variable.  By using this pointer variable we may call that particular memory location.

Malloc()
Realloc()
Freealloc()
Calloc()


#include<stdio.h>
#include<conio.h>

void main()
{
int x,*ptr;
ptr=&x;
clrscr();
printf(“Enter an integer:”);
scanf(“%d”,ptr);
printf(“The value of %d\n”,x);
getch();
}

%u to find out the address of the pointer

#include<stdio.h>
#include<conio.h>
void main()
{
int x,*ptr;
ptr=&x;
clrscr();
printf(“Enter a number:”);
scanf(“%d”,&x);
printf(“Value of the number=%d\n”,x);
printf(“Address of the pointer ptr=%u\n”,ptr);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int x,y,big;
int *ptr,*ptr1;
ptr=&x;
ptr1=&y;
clrscr();
printf(“Enter two numbers:\n”);
scanf(“%d%d”,&x,&y);
if(*ptr>*ptr1)
big=*ptr;
else
big=*ptr1;
printf(“Biggest number is:%d\n”,big);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
char *name;
char name1[15];
clrscr();
printf(“Enter a string:”);
scanf(“%s”,name1);
*name=name1;
printf(“%s”,*name);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int *c,*d;
clrscr();
printf(“Enter two numbers:”);
scanf(“%d%d”,c,d);
printf(“\n The sum of two number=%d”,*c+*d);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int *x,*y,p1,p2;
x=&p1;
y=&p2;
clrscr();
printf(“Enter two numbers:”);
scanf(“%d%d”,&p1,&p2);
printf(“Address=\n”);
printf(“%d\n”,*x);
printf(“%d\n”,*y);
printf(“%d\n”,p1);
printf(“%d\n”,p2);
getch();
}

#include<stdio.h>
#include<conio.h>
void main()
{
int a,b;
clrscr();
printf(“Enter two numbers:”);
scanf(“%d%d”,&a,&b);
exchange(&a,&b);
getch();
}
exchange(int *aa,int *bb)
{
int *t;
*t=*aa;
*aa=*bb;
*bb=*t;
printf(“A value:%d\n”,*aa);
printf(“B value:%d\n”,*bb);
}

#include<stdio.h>
#include<conio.h>
void main()
{
int sum(int *,int *)
int a,b,s;
clrscr();
printf(“\n Enter A:”);
scanf(“%d”,&a);
printf(“Enter B:”);
scanf(“%d”,&b);
s=sum(&a,&b);
printf(“The sum:%d”,s);
getch();
return;
}
int sum(int *x,int *y)
{
return(*x+*y);
}

#include<stdio.h>
#include<conio.h>
void main()
{
int a[100],temp,*pa,*pb;
pa=&a[0];
pb=&a[1];
clrscr();
printf(“Enter the values:”);
for(pa=a;pa<a+10;pa++)
{
scanf(”%d”,&pa);
}
for(pa=a;pa<a+10;pa++)
{
for(pb=a;pb<a+10;pb++)
{
if(*pa>*pb)
{
temp=*pa;
*pa=*pb;
*pb=temp;
}
}
}
printf(“The sorted list\n”);
for(pa=a;pa<a+10;pa++)
printf(“\n %d\n”,*pa);
getch();
}

No comments:

Post a Comment