Tweet
/* Program to implement union in structure */
#include<stdio.h>
#include<malloc.h>
#define new (nd *)malloc(sizeof(nd))
struct link
{
char ch;
union
{
int a;
char b[30];
float c;
};
struct link *next;
};
typedef struct link nd;
void create(nd *ptr)
{
int ch1;
char ch2;
do
{
printf("\nEnter data to insert : ");
printf("\n[1] Enter integer ");
printf("\n[2] Enter character ");
printf("\n[3] Enter floating no. ");
printf("\n\nEnter your choice : ");
scanf("%d",&ch1);
ptr->ch=ch1;
switch(ch1)
{
case 1:
printf("\nEnter data : ");
scanf("%d",&ptr->a);
break;
case 2:
printf("\nEnter data : ");
scanf("%s",&ptr->b);
break;
case 3:
printf("\nEnter data : ");
scanf("%f",&ptr->c);
break;
}
printf("\nWant to continue ?(y/n) : ");
scanf("%s",&ch2);
if(ch2=='y'||ch2=='Y')
{
ptr->next=new;
ptr=ptr->next;
}
}while(ch2=='y'||ch2=='Y');
}
void display(nd *h)
{
printf("\nList : ");
nd *ptr=h;
char ch;
while(ptr!=NULL)
{
ch=ptr->ch;
switch(ch)
{
case 1:
printf(" %d --> ",ptr->a);
break;
case 2:
printf(" %s --> ",ptr->b);
break;
case 3:
printf(" %f --> ",ptr->c);
break;
}
ptr=ptr->next;
}
printf("\b\b\b\b ");
}
main()
{
printf("\nProgram to implement union within a structure : \n");
nd *h;
h=new;
printf("\n");
create(h);
display(h);
printf("\n\n");
}
Implement union within a structure in C Programming
Posted by
LAHAUL SETH
~
Implement union within a structure in C Programming
2011-11-29T11:02:00+05:30
LAHAUL SETH
Linked List in C
|
Programming in C
|
Comments
Implement union within a structure in C Programming
2011-11-29T11:02:00+05:30
LAHAUL SETH
Linked List in C
|
Programming in C
|