-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStructure_Union_Pointer.cpp
64 lines (56 loc) · 1.72 KB
/
Structure_Union_Pointer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include<stdio.h>
#include<conio.h>
#include<string.h>
typedef struct
{
int id;
char name[20];
float percentage;
}student;
typedef union {
int id;
char name[20];
float percentage;
}str;
void DisplayDetails(student *);
int main()
{
student s1, s2, *s4; /*Declaration of the Structure Variables*/
s4 = &s2;
s1.id = 1;
strcpy_s(s1.name, "John Smith");
s1.percentage = 62.5;
printf("\nEnter the Details for Student 2 ::"); /*User Entering the Details into the Structure Variables*/
printf("\nID/Roll No :: ");
scanf_s("%d", &s4->id); /*&s2.id*/
getchar(); /*To Consume the unwanted new line character*/
printf("Name :: ");
gets_s(s4->name, 20); /*s2.name*/
printf("Percentage :: ");
scanf_s("%f", &s4->percentage);
DisplayDetails(&s1); /*Displaying the Details of the Student through a Funtion*/
printf("\nStudent Details are as follows :: ");
printf("\n ID :: %d", s2.id);
printf("\n Name :: %s", s2.name);
printf("\n Percentage :: %f", s2.percentage);
str u1, u2;
u1.id = 5;
strcpy_s(u1.name, "John Edgar");
u1.percentage = 78;
printf("\n\n\n************* Union Details are as follows :: ***************");
printf("\n ID :: %d", u1.id);
printf("\n Name :: %s", u1.name);
printf("\n Percentage :: %f", u1.percentage);
printf("\n*******************************************");
printf("\nSize of the Struture Variable is %d", sizeof(s1));
printf("\nSize of the Union Variable is %d", sizeof(u1));
_getch();
return 0;
}
void DisplayDetails(student *s)
{
printf("\nStudent Details are as follows :: ");
printf("\n ID :: %d", s->id);
printf("\n Name :: %s", s->name);
printf("\n Percentage :: %f", (*s).percentage);
}