//linux, 64bit, gcc, -Wall -Wextra -Wpedantic -Wshadow #include #include #include #define ST_STR_LEN 256 char first_str[] = "Andromeda"; char second_str[] = "Zeppelin"; struct st0 { char c; int i; float f; double d; }; struct st1 { char c; int i; float f; double d; } s1,s2; typedef struct st2 { char c; int i; float f; double d; } St2; typedef struct { char c; int i; float f; double d; } St3; typedef struct { char c; char *p_str; } St4; typedef struct { char c; char str[ST_STR_LEN]; } St5; typedef struct { char c; St3 s; } St6; typedef struct { char c; St3 s[14]; } St7; typedef struct { char c; char ca[12]; } St8; typedef struct { int a; int b __attribute__ ((packed)); int c __attribute__ ((packed)); int *p __attribute__ ((packed)); } S9; typedef struct { int a; int b __attribute__ ((aligned (16))); int c __attribute__ ((aligned (16))); int *p __attribute__ ((aligned (16))); } S10; int main() { struct st0 s0; s0.c = 'o'; s0.i = 1; s0.f = 1.2f; s0.d = 12.1; printf( "s0.c: %c\n", s0.c ); printf( "s0.i: %d\n", s0.i ); printf( "s0.f: %f\n", s0.f ); printf( "s0.d: %f\n\n", s0.d ); s1.c = 'a'; s1.i = 123; s1.f = 1.23f; s1.d = 12.3; printf( "s1.c: %c\n", s1.c ); printf( "s1.i: %d\n", s1.i ); printf( "s1.f: %f\n", s1.f ); printf( "s1.d: %f\n\n", s1.d ); s2 = s1; printf( "s2.c: %c\n", s2.c ); printf( "s2.i: %d\n", s2.i ); printf( "s2.f: %f\n", s2.f ); printf( "s2.d: %f\n\n", s2.d ); printf( "sizeof (struct st1): %zu\n", sizeof (struct st1) ); printf( "sizeof s2: %zu\n", sizeof s2 ); printf( "sizeof &s2: %zu\n", sizeof &s2 ); printf( "sizeof s2.c: %zu\n", sizeof s2.c ); printf( "sizeof s2.i: %zu\n", sizeof s2.i ); printf( "sizeof s2.f: %zu\n", sizeof s2.f ); printf( "sizeof s2.d: %zu\n\n", sizeof s2.d ); struct st1 *p_s3; p_s3 = &s1; printf( "p_s3.c: %c\n", p_s3->c ); printf( "p_s3.i: %d\n", p_s3->i ); printf( "p_s3.f: %f\n", p_s3->f ); printf( "p_s3.d: %f\n", p_s3->d ); printf( "sizeof p_s3: %zu\n", sizeof p_s3 ); printf( "sizeof &p_s3: %zu\n", sizeof &p_s3 ); printf( "sizeof *p_s3: %zu\n", sizeof *p_s3 ); printf( "sizeof p_s3.c: %zu\n", sizeof p_s3->c ); printf( "sizeof p_s3.i: %zu\n", sizeof p_s3->i ); printf( "sizeof p_s3.f: %zu\n", sizeof p_s3->f ); printf( "sizeof p_s3.d: %zu\n", sizeof p_s3->d ); printf( "&s1: %p\n", (void *) &s1 ); printf( "p_s3: %p\n", (void *) p_s3 ); printf( "&p_s3: %p\n", (void *) &p_s3 ); printf( "HEX: p_s3: %#lx\n\n", (long unsigned int) p_s3 ); printf("s1 before:\n\n"); printf( "s1.c: %c\n", s1.c ); printf( "s1.i: %d\n", s1.i ); printf( "s1.f: %f\n", s1.f ); printf( "s1.d: %f\n\n", s1.d ); p_s3->c = 'b'; p_s3->i = 321; p_s3->f = 3.21f; p_s3->d = 32.1; printf("s1 after:\n\n"); printf( "s1.c: %c\n", s1.c ); printf( "s1.i: %d\n", s1.i ); printf( "s1.f: %f\n", s1.f ); printf( "s1.d: %f\n\n", s1.d ); struct st2 test_s_1; test_s_1.c = 'c'; test_s_1.i = 22; test_s_1.f = 2.2f; test_s_1.d = 22.0; printf( "test_s_1.c: %c\n", test_s_1.c ); printf( "test_s_1.i: %d\n", test_s_1.i ); printf( "test_s_1.f: %f\n", test_s_1.f ); printf( "test_s_1.d: %f\n\n", test_s_1.d ); St2 test_s_2; test_s_2.c = 'd'; test_s_2.i = 222; test_s_2.f = 2.22f; test_s_2.d = 222.0; printf( "test_s_2.c: %c\n", test_s_2.c ); printf( "test_s_2.i: %d\n", test_s_2.i ); printf( "test_s_2.f: %f\n", test_s_2.f ); printf( "test_s_2.d: %f\n\n", test_s_2.d ); St3 test_s_3; test_s_3.c = 'd'; test_s_3.i = 333; test_s_3.f = 3.33f; test_s_3.d = 333.0; printf( "test_s_3.c: %c\n", test_s_3.c ); printf( "test_s_3.i: %d\n", test_s_3.i ); printf( "test_s_3.f: %f\n", test_s_3.f ); printf( "test_s_3.d: %f\n\n", test_s_3.d ); St3 *p_s_3; p_s_3 = malloc( sizeof p_s_3 ); if ( !p_s_3 ) { printf("Error: cannot allocate memory.\n"); } else { p_s_3->c = 'e'; p_s_3->i = 111; p_s_3->f = 1.11f; p_s_3->d = 111.0; printf( "p_s_3->c: %c\n", p_s_3->c ); printf( "p_s_3->i: %d\n", p_s_3->i ); printf( "p_s_3->f: %f\n", p_s_3->f ); printf( "p_s_3->d: %f\n\n", p_s_3->d ); free( p_s_3 ); } St4 s4; s4.c = 'f'; s4.p_str = first_str; printf( "s4.c: %c\n", s4.c ); printf( "s4.p_str: %s\n\n", s4.p_str ); St4 s4a; s4a = s4; printf( "s4a.c: %c\n", s4a.c ); printf( "s4a.p_str: %s\n\n", s4a.p_str ); s4.p_str = second_str; printf( "(2) s4.c: %c\n", s4.c ); printf( "(2) s4.p_str: %s\n\n", s4.p_str ); printf( "(2) s4a.c: %c\n", s4a.c ); printf( "(2) s4a.p_str: %s\n\n", s4a.p_str ); St5 s5 = {0}; s5.c = 'g'; if ( strlen(first_str) < ST_STR_LEN - 1 ) { printf("\"%s\", length: %ld\n\n", first_str, strlen(first_str)); strcpy( s5.str, first_str ); } else { printf("Error: first string too long.\n"); } printf( "(1) s5.c: %c\n", s5.c ); printf( "(1) s5.str: %s\n\n", s5.str ); if ( strlen(second_str) < ST_STR_LEN - 1 ) { printf("\"%s\", length: %ld\n\n", second_str, strlen(second_str)); strcpy( s5.str, second_str ); } else { printf("Error: second string too long.\n"); } printf( "(2) s5.c: %c\n", s5.c ); printf( "(2) s5.str: %s\n\n", s5.str ); St5 s5a = { .c = 'h', .str = "Orion" }; printf( "s5a.c: %c\n", s5a.c ); printf( "s5a.str: %s\n\n", s5a.str ); St5 s5b = { .str = "Capricorn", .c = 'r' }; printf( "s5b.c: %c\n", s5b.c ); printf( "s5b.str: %s\n\n", s5b.str ); St6 s6; printf( "sizeof s6: %zu\n", sizeof s6 ); printf( "sizeof &s6: %zu\n", sizeof &s6 ); printf( "sizeof s6.c: %zu\n", sizeof s6.c ); printf( "sizeof s6.s: %zu\n\n", sizeof s6.s ); s6.s.c = 'j'; printf( "s6.s.c: %c\n\n", s6.s.c ); St7 s7; printf( "sizeof s7: %zu\n", sizeof s7 ); printf( "sizeof &s7: %zu\n", sizeof &s7 ); printf( "sizeof s7.c: %zu\n", sizeof s7.c ); printf( "sizeof s7.s: %zu\n\n", sizeof s7.s ); s7.s[0].c = 'k'; s7.s[1].c = 'l'; printf( "s7.s[0].c: %c\n", s7.s[0].c ); printf( "s7.s[1].c: %c\n\n", s7.s[1].c ); St8 s8; printf( "sizeof s8: %zu\n", sizeof s8 ); printf( "sizeof &s8: %zu\n", sizeof &s8 ); printf( "sizeof s8.c: %zu\n", sizeof s8.c ); printf( "sizeof s8.ca: %zu\n\n", sizeof s8.ca ); s8.ca[0] = 'm'; s8.ca[1] = 'n'; printf( "s8.ca[0]: %c\n", s8.ca[0] ); printf( "s8.ca[1]: %c\n\n", s8.ca[1] ); S9 strc_9; S9 strc_9a[10]; S10 strc_10; S10 strc_10a[10]; printf("sizeof (S9): %zu\n", sizeof (S9)); printf("sizeof strc_9: %zu\n", sizeof strc_9); printf("sizeof strc_9a: %zu\n", sizeof strc_9a); printf("sizeof *strc_9a: %zu\n", sizeof *strc_9a); printf("sizeof strc_9a[0]: %zu\n\n", sizeof strc_9a[0]); printf("sizeof (S10): %zu\n", sizeof (S10)); printf("sizeof strc_10: %zu\n", sizeof strc_10); printf("sizeof strc_10a: %zu\n", sizeof strc_10a); printf("sizeof *strc_10a: %zu\n", sizeof *strc_10a); printf("sizeof strc_10a[0]: %zu\n\n", sizeof strc_10a[0]); return 0; } /* output: s0.c: o s0.i: 1 s0.f: 1.200000 s0.d: 12.100000 s1.c: a s1.i: 123 s1.f: 1.230000 s1.d: 12.300000 s2.c: a s2.i: 123 s2.f: 1.230000 s2.d: 12.300000 sizeof (struct st1): 24 sizeof s2: 24 sizeof &s2: 8 sizeof s2.c: 1 sizeof s2.i: 4 sizeof s2.f: 4 sizeof s2.d: 8 p_s3.c: a p_s3.i: 123 p_s3.f: 1.230000 p_s3.d: 12.300000 sizeof p_s3: 8 sizeof &p_s3: 8 sizeof *p_s3: 24 sizeof p_s3.c: 1 sizeof p_s3.i: 4 sizeof p_s3.f: 4 sizeof p_s3.d: 8 &s1: 0x555ef2857040 p_s3: 0x555ef2857040 &p_s3: 0x7ffc5cdffac0 HEX: p_s3: 0x555ef2857040 s1 before: s1.c: a s1.i: 123 s1.f: 1.230000 s1.d: 12.300000 s1 after: s1.c: b s1.i: 321 s1.f: 3.210000 s1.d: 32.100000 test_s_1.c: c test_s_1.i: 22 test_s_1.f: 2.200000 test_s_1.d: 22.000000 test_s_2.c: d test_s_2.i: 222 test_s_2.f: 2.220000 test_s_2.d: 222.000000 test_s_3.c: d test_s_3.i: 333 test_s_3.f: 3.330000 test_s_3.d: 333.000000 p_s_3->c: e p_s_3->i: 111 p_s_3->f: 1.110000 p_s_3->d: 111.000000 s4.c: f s4.p_str: Andromeda s4a.c: f s4a.p_str: Andromeda (2) s4.c: f (2) s4.p_str: Zeppelin (2) s4a.c: f (2) s4a.p_str: Andromeda "Andromeda", length: 9 (1) s5.c: g (1) s5.str: Andromeda "Zeppelin", length: 8 (2) s5.c: g (2) s5.str: Zeppelin s5a.c: h s5a.str: Orion s5b.c: r s5b.str: Capricorn sizeof s6: 32 sizeof &s6: 8 sizeof s6.c: 1 sizeof s6.s: 24 s6.s.c: j sizeof s7: 344 sizeof &s7: 8 sizeof s7.c: 1 sizeof s7.s: 336 s7.s[0].c: k s7.s[1].c: l sizeof s8: 13 sizeof &s8: 8 sizeof s8.c: 1 sizeof s8.ca: 12 s8.ca[0]: m s8.ca[1]: n sizeof (S9): 20 sizeof strc_9: 20 sizeof strc_9a: 200 sizeof *strc_9a: 20 sizeof strc_9a[0]: 20 sizeof (S10): 64 sizeof strc_10: 64 sizeof strc_10a: 640 sizeof *strc_10a: 64 sizeof strc_10a[0]: 64 */