-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathstruct.c
More file actions
51 lines (37 loc) · 1.28 KB
/
struct.c
File metadata and controls
51 lines (37 loc) · 1.28 KB
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct Person
{
char name[20];
int age;
float height;
};
int main()
{
struct Person presidentUSA;
struct Person *presidentRussia = NULL;
printf("sizeof presidentUSA.name: %d \n", sizeof(presidentUSA.name));
printf("sizeof presidentUSA.age: %d \n", sizeof(presidentUSA.age));
printf("sizeof presidentUSA.height: %d \n", sizeof(presidentUSA.height));
printf("sizeof presidentUSA: %d \n", sizeof(presidentUSA));
sprintf(presidentUSA.name, "Obama");
presidentUSA.age = 53;
presidentUSA.height = 1.85f;
printf("\n");
printf("presidentUSA.name = %s \n", presidentUSA.name);
printf("presidentUSA.age = %d \n", presidentUSA.age);
printf("presidentUSA.height = %f \n", presidentUSA.height);
presidentRussia = (struct Person*)malloc(sizeof(struct Person));
if(!presidentRussia) goto _exit;
sprintf(presidentRussia->name, "Putin");
presidentRussia->age = 62;
presidentRussia->height = 1.7f;
printf("\n");
printf("presidentRussia.name = %s \n", presidentRussia->name);
printf("presidentRussia.age = %d \n", presidentRussia->age);
printf("presidentRussia.height = %f \n", presidentRussia->height);
free(presidentRussia);
_exit:
return 0;
}