-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathStructs vs Unions.c
61 lines (45 loc) · 1.54 KB
/
Structs vs Unions.c
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
#include <stdio.h>
#include <string.h>
// Define a struct to represent a person
struct Person {
char name[50];
int age;
};
// Define a union to represent a person's contact information
union ContactInfo {
char email[50];
char phone[15];
};
int main() {
// Create an instance of the struct
struct Person person;
// Initialize the struct
strcpy(person.name, "John");
person.age = 30;
// Access and print struct members
printf("Person's name: %s\n", person.name);
printf("Person's age: %d\n", person.age);
// Create an instance of the union
union ContactInfo contact;
// Initialize the union's email member
strcpy(contact.email, "john@example.com");
// Access and print the union's email member
printf("Contact Email: %s\n", contact.email);
// Now, change the union's phone member
strcpy(contact.phone, "123-456-7890");
// Access and print the union's phone member
printf("Contact Phone: %s\n", contact.phone);
// The size of the struct and union
printf("Size of struct Person: %lu bytes\n", sizeof(struct Person));
printf("Size of union ContactInfo: %lu bytes\n", sizeof(union ContactInfo));
return 0;
}
/* Result:
Person's name: John
Person's age: 30
Contact Email: john@example.com
Contact Phone: 123-456-7890
Size of struct Person: 56 bytes
Size of union ContactInfo: 50 bytes
*/
/* a struct allocates memory for all of its members simultaneously, whereas a union shares memory among its members, allowing only one member to be valid at a time. */