Friday, April 4, 2014

malloc swap function program

/*swapping two values through malloc function C program*/
#include<stdio.h>
#include<conio.h>
struct stud
{
 char nam[30];
 struct stud *next;
};
struct stud *p,*q;
int main()
{
 p=(struct stud *)malloc(sizeof(struct stud));
 q=(struct stud *)malloc(sizeof(struct stud));
 printf("Enter name p : ");
 gets(p->nam);
 //fflush(stdin);
 printf("Enter name q : ");
 gets(q->nam);
 p->next=q;
 q->next=p;
 printf("
Name of p : %s"
,p->next->nam);
 printf("
Name of q : %s"
,q->next->nam);

 getch();
 return 0;
}

       Output of above program : 

Enter name p : Peter
Enter name q : Jhon

Name of p : Jhon
Name of q : Peter

Related Post:

0 comments:

Post a Comment

 
Copyright 2009 Information Blog
Powered By Blogger