-
Notifications
You must be signed in to change notification settings - Fork 0
/
105_struct.c
48 lines (40 loc) · 1010 Bytes
/
105_struct.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
/* K & R */
#include <stdio.h>
#define ARR_SZ(x) (sizeof(x) / sizeof(x[0]))
struct { /* structure tag is optional */
int x;
int y;
} svar;
typedef struct { /* structure tag is optional */
int x;
int y;
} point_t;
/* To find the sizeof array */
point_t arr[20];
char carr[25];
struct usz {
point_t p;
char a;
};
struct asz { /* Align */
char a;
point_t p;
} __attribute__((aligned (8)));
int main(int argc, char*argv[])
{
svar.x = 10;
point_t p1 = {11,22};
point_t p2 = {33,44};
printf("svar %d x, %d y\n", svar.x, svar.y);
printf(" p1 %d x, %d y\n", p1.x, p1.y);
printf(" p2 %d x, %d y\n", p2.x, p2.y);
p2 = p1;
printf(" p1 %d x, %d y\n", p1.x, p1.y);
printf(" p2 %d x, %d y\n", p2.x, p2.y);
printf("Array size is %lu\n", ARR_SZ(arr));
printf("Array size is %lu\n", ARR_SZ(carr));
printf("sizeof char is %lu\n", sizeof(char));
printf("sizeof unaligned struct sz is %lu\n", sizeof(struct usz));
printf("sizeof aligned struct sz is %lu\n", sizeof(struct asz));
return 0;
}