D
Darklight
Question the program below is taken from a book which i have been
told is bad, but it gives me the basics which is what i need, and
yes i know the gets function should not be used, but i would like
to see how other people would get the program below to work.
I have already got it to work by changing some thing in the
typedef struct, but i would like to see how the more experienced
programmer would get it to work
The program is used to demonstrate "unions in structures"
the book it's taken from is "c programming in easy steps"
#include<stdio.h>
typedef struct
{
union{ int num; char letter; };
char *name;
} info;
int main(void)
{
info stored; /* create a struct of the info type */
printf("Please enter your first name: ");
gets(stored.name);
printf("Enter a number to convert to hex? [Y or N]: ");
scanf("%c",&stored.letter);
if(stored.letter == 'y' || stored.letter == 'Y')
{
printf("OK, enter the number to be converted; ");
scanf("%d",&stored.num);
printf("Thanks %s, ",stored.name);
printf("%d in hex is 0x%X\n", stored.num, stored.num);
}
return 0;
}
told is bad, but it gives me the basics which is what i need, and
yes i know the gets function should not be used, but i would like
to see how other people would get the program below to work.
I have already got it to work by changing some thing in the
typedef struct, but i would like to see how the more experienced
programmer would get it to work
The program is used to demonstrate "unions in structures"
the book it's taken from is "c programming in easy steps"
#include<stdio.h>
typedef struct
{
union{ int num; char letter; };
char *name;
} info;
int main(void)
{
info stored; /* create a struct of the info type */
printf("Please enter your first name: ");
gets(stored.name);
printf("Enter a number to convert to hex? [Y or N]: ");
scanf("%c",&stored.letter);
if(stored.letter == 'y' || stored.letter == 'Y')
{
printf("OK, enter the number to be converted; ");
scanf("%d",&stored.num);
printf("Thanks %s, ",stored.name);
printf("%d in hex is 0x%X\n", stored.num, stored.num);
}
return 0;
}