Sunday, September 18, 2011

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("\nName of p : %s",p->next->nam);
 printf("\nName 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

No comments:

Post a Comment