Menu BAR

FEEL FREE TO ASK ANY PROGRAM and please mention any error if you find it

25 Jan 2013

Swapping using MALLOC FUNCTION

 Q.) Write a C program for swapping of two values through malloc function.

#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("\n Name of P : %s", p->next->nam) ;
printf("\n Name of Q : %s", q->next->nam) ;
return 0 ;
}

        :: OUTPUT of above PROGRAM ::

Enter NAME p : BUNNY
Enter NAME q : SONU

Name of P : SONU
Name of Q : BUNNY

No comments:

Post a Comment