Advertisement
RicardasSim

trying c structures

Nov 27th, 2022 (edited)
825
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.52 KB | None | 0 0
  1. //linux, 64bit, gcc, -Wall -Wextra -Wpedantic -Wshadow
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. #define ST_STR_LEN 256
  8.  
  9. char first_str[] = "Andromeda";
  10. char second_str[] = "Zeppelin";
  11.  
  12. struct st0 {
  13.     char c;
  14.     int i;
  15.     float f;
  16.     double d;
  17. };
  18.  
  19. struct st1 {
  20.     char c;
  21.     int i;
  22.     float f;
  23.     double d;
  24. } s1,s2;
  25.  
  26. typedef struct st2 {
  27.     char c;
  28.     int i;
  29.     float f;
  30.     double d;
  31. } St2;
  32.  
  33. typedef struct {
  34.     char c;
  35.     int i;
  36.     float f;
  37.     double d;
  38. } St3;
  39.  
  40. typedef struct {
  41.     char c;
  42.     char *p_str;
  43. } St4;
  44.  
  45. typedef struct {
  46.     char c;
  47.     char str[ST_STR_LEN];
  48. } St5;
  49.  
  50. typedef struct {
  51.     char c;
  52.     St3 s;
  53. } St6;
  54.  
  55. typedef struct {
  56.     char c;
  57.     St3 s[14];
  58. } St7;
  59.  
  60. typedef struct {
  61.     char c;
  62.     char ca[12];
  63. } St8;
  64.  
  65. typedef struct {
  66.  
  67.     int a;
  68.     int b __attribute__ ((packed));
  69.     int c __attribute__ ((packed));
  70.     int *p __attribute__ ((packed));
  71.  
  72. } S9;
  73.  
  74. typedef struct {
  75.  
  76.     int a;
  77.     int b __attribute__ ((aligned (16)));
  78.     int c __attribute__ ((aligned (16)));
  79.     int *p __attribute__ ((aligned (16)));
  80.  
  81. } S10;
  82.  
  83. int main()
  84. {
  85.  
  86.     struct st0 s0;
  87.  
  88.     s0.c = 'o';
  89.     s0.i = 1;
  90.     s0.f = 1.2f;
  91.     s0.d = 12.1;
  92.  
  93.     printf( "s0.c: %c\n", s0.c );
  94.     printf( "s0.i: %d\n", s0.i );
  95.     printf( "s0.f: %f\n", s0.f );
  96.     printf( "s0.d: %f\n\n", s0.d );
  97.  
  98.     s1.c = 'a';
  99.     s1.i = 123;
  100.     s1.f = 1.23f;
  101.     s1.d = 12.3;
  102.  
  103.     printf( "s1.c: %c\n", s1.c );
  104.     printf( "s1.i: %d\n", s1.i );
  105.     printf( "s1.f: %f\n", s1.f );
  106.     printf( "s1.d: %f\n\n", s1.d );
  107.  
  108.     s2 = s1;
  109.  
  110.     printf( "s2.c: %c\n", s2.c );
  111.     printf( "s2.i: %d\n", s2.i );
  112.     printf( "s2.f: %f\n", s2.f );
  113.     printf( "s2.d: %f\n\n", s2.d );
  114.  
  115.     printf( "sizeof (struct st1): %zu\n", sizeof (struct st1) );
  116.     printf( "sizeof s2: %zu\n", sizeof s2 );
  117.     printf( "sizeof &s2: %zu\n", sizeof &s2 );
  118.     printf( "sizeof s2.c: %zu\n", sizeof s2.c );
  119.     printf( "sizeof s2.i: %zu\n", sizeof s2.i );
  120.     printf( "sizeof s2.f: %zu\n", sizeof s2.f );
  121.     printf( "sizeof s2.d: %zu\n\n", sizeof s2.d );
  122.  
  123.     struct st1 *p_s3;
  124.  
  125.     p_s3 = &s1;
  126.  
  127.     printf( "p_s3.c: %c\n", p_s3->c );
  128.     printf( "p_s3.i: %d\n", p_s3->i );
  129.     printf( "p_s3.f: %f\n", p_s3->f );
  130.     printf( "p_s3.d: %f\n", p_s3->d );
  131.     printf( "sizeof p_s3: %zu\n", sizeof p_s3 );
  132.     printf( "sizeof &p_s3: %zu\n", sizeof &p_s3 );
  133.     printf( "sizeof *p_s3: %zu\n", sizeof *p_s3 );
  134.     printf( "sizeof p_s3.c: %zu\n", sizeof p_s3->c );
  135.     printf( "sizeof p_s3.i: %zu\n", sizeof p_s3->i );
  136.     printf( "sizeof p_s3.f: %zu\n", sizeof p_s3->f );
  137.     printf( "sizeof p_s3.d: %zu\n", sizeof p_s3->d );
  138.     printf( "&s1: %p\n", (void *) &s1 );
  139.     printf( "p_s3: %p\n", (void *) p_s3 );
  140.     printf( "&p_s3: %p\n", (void *) &p_s3 );
  141.     printf( "HEX: p_s3: %#lx\n\n", (long unsigned int) p_s3 );
  142.  
  143.     printf("s1 before:\n\n");
  144.  
  145.     printf( "s1.c: %c\n", s1.c );
  146.     printf( "s1.i: %d\n", s1.i );
  147.     printf( "s1.f: %f\n", s1.f );
  148.     printf( "s1.d: %f\n\n", s1.d );
  149.  
  150.     p_s3->c = 'b';
  151.     p_s3->i = 321;
  152.     p_s3->f = 3.21f;
  153.     p_s3->d = 32.1;
  154.  
  155.     printf("s1 after:\n\n");
  156.  
  157.     printf( "s1.c: %c\n", s1.c );
  158.     printf( "s1.i: %d\n", s1.i );
  159.     printf( "s1.f: %f\n", s1.f );
  160.     printf( "s1.d: %f\n\n", s1.d );
  161.  
  162.  
  163.     struct st2 test_s_1;
  164.  
  165.     test_s_1.c = 'c';
  166.     test_s_1.i = 22;
  167.     test_s_1.f = 2.2f;
  168.     test_s_1.d = 22.0;
  169.  
  170.     printf( "test_s_1.c: %c\n", test_s_1.c );
  171.     printf( "test_s_1.i: %d\n", test_s_1.i );
  172.     printf( "test_s_1.f: %f\n", test_s_1.f );
  173.     printf( "test_s_1.d: %f\n\n", test_s_1.d );
  174.  
  175.     St2 test_s_2;
  176.  
  177.     test_s_2.c = 'd';
  178.     test_s_2.i = 222;
  179.     test_s_2.f = 2.22f;
  180.     test_s_2.d = 222.0;
  181.  
  182.     printf( "test_s_2.c: %c\n", test_s_2.c );
  183.     printf( "test_s_2.i: %d\n", test_s_2.i );
  184.     printf( "test_s_2.f: %f\n", test_s_2.f );
  185.     printf( "test_s_2.d: %f\n\n", test_s_2.d );
  186.  
  187.     St3 test_s_3;
  188.  
  189.     test_s_3.c = 'd';
  190.     test_s_3.i = 333;
  191.     test_s_3.f = 3.33f;
  192.     test_s_3.d = 333.0;
  193.  
  194.     printf( "test_s_3.c: %c\n", test_s_3.c );
  195.     printf( "test_s_3.i: %d\n", test_s_3.i );
  196.     printf( "test_s_3.f: %f\n", test_s_3.f );
  197.     printf( "test_s_3.d: %f\n\n", test_s_3.d );
  198.  
  199.     St3 *p_s_3;
  200.  
  201.     p_s_3 = malloc( sizeof p_s_3 );
  202.  
  203.     if ( !p_s_3 )
  204.     {
  205.         printf("Error: cannot allocate memory.\n");
  206.     }
  207.     else
  208.     {
  209.         p_s_3->c = 'e';
  210.         p_s_3->i = 111;
  211.         p_s_3->f = 1.11f;
  212.         p_s_3->d = 111.0;
  213.  
  214.         printf( "p_s_3->c: %c\n", p_s_3->c );
  215.         printf( "p_s_3->i: %d\n", p_s_3->i );
  216.         printf( "p_s_3->f: %f\n", p_s_3->f );
  217.         printf( "p_s_3->d: %f\n\n", p_s_3->d );
  218.  
  219.         free( p_s_3 );
  220.     }
  221.  
  222.     St4 s4;
  223.  
  224.     s4.c = 'f';
  225.     s4.p_str = first_str;
  226.  
  227.     printf( "s4.c: %c\n", s4.c );
  228.     printf( "s4.p_str: %s\n\n", s4.p_str );
  229.  
  230.     St4 s4a;
  231.  
  232.     s4a = s4;
  233.  
  234.     printf( "s4a.c: %c\n", s4a.c );
  235.     printf( "s4a.p_str: %s\n\n", s4a.p_str );
  236.  
  237.     s4.p_str = second_str;
  238.  
  239.     printf( "(2) s4.c: %c\n", s4.c );
  240.     printf( "(2) s4.p_str: %s\n\n", s4.p_str );
  241.  
  242.     printf( "(2) s4a.c: %c\n", s4a.c );
  243.     printf( "(2) s4a.p_str: %s\n\n", s4a.p_str );
  244.  
  245.     St5 s5 = {0};
  246.  
  247.     s5.c = 'g';
  248.  
  249.     if ( strlen(first_str) < ST_STR_LEN - 1 )
  250.     {
  251.         printf("\"%s\", length: %ld\n\n", first_str, strlen(first_str));
  252.         strcpy( s5.str, first_str );
  253.     }
  254.     else
  255.     {
  256.         printf("Error: first string too long.\n");
  257.     }
  258.  
  259.     printf( "(1) s5.c: %c\n", s5.c );
  260.     printf( "(1) s5.str: %s\n\n", s5.str );
  261.  
  262.     if ( strlen(second_str) < ST_STR_LEN - 1 )
  263.     {
  264.         printf("\"%s\", length: %ld\n\n", second_str, strlen(second_str));
  265.         strcpy( s5.str, second_str );
  266.     }
  267.     else
  268.     {
  269.         printf("Error: second string too long.\n");
  270.     }
  271.  
  272.     printf( "(2) s5.c: %c\n", s5.c );
  273.     printf( "(2) s5.str: %s\n\n", s5.str );
  274.  
  275.     St5 s5a = { .c = 'h', .str = "Orion" };
  276.  
  277.     printf( "s5a.c: %c\n", s5a.c );
  278.     printf( "s5a.str: %s\n\n", s5a.str );
  279.  
  280.     St5 s5b = { .str = "Capricorn", .c = 'r' };
  281.  
  282.     printf( "s5b.c: %c\n", s5b.c );
  283.     printf( "s5b.str: %s\n\n", s5b.str );
  284.  
  285.     St6 s6;
  286.  
  287.     printf( "sizeof s6: %zu\n", sizeof s6 );
  288.     printf( "sizeof &s6: %zu\n", sizeof &s6 );
  289.     printf( "sizeof s6.c: %zu\n", sizeof s6.c );
  290.     printf( "sizeof s6.s: %zu\n\n", sizeof s6.s );
  291.  
  292.     s6.s.c = 'j';
  293.  
  294.     printf( "s6.s.c: %c\n\n", s6.s.c );
  295.  
  296.     St7 s7;
  297.  
  298.     printf( "sizeof s7: %zu\n", sizeof s7 );
  299.     printf( "sizeof &s7: %zu\n", sizeof &s7 );
  300.     printf( "sizeof s7.c: %zu\n", sizeof s7.c );
  301.     printf( "sizeof s7.s: %zu\n\n", sizeof s7.s );
  302.  
  303.     s7.s[0].c = 'k';
  304.     s7.s[1].c = 'l';
  305.  
  306.     printf( "s7.s[0].c: %c\n", s7.s[0].c );
  307.     printf( "s7.s[1].c: %c\n\n", s7.s[1].c );
  308.  
  309.     St8 s8;
  310.  
  311.     printf( "sizeof s8: %zu\n", sizeof s8 );
  312.     printf( "sizeof &s8: %zu\n", sizeof &s8 );
  313.     printf( "sizeof s8.c: %zu\n", sizeof s8.c );
  314.     printf( "sizeof s8.ca: %zu\n\n", sizeof s8.ca );
  315.  
  316.     s8.ca[0] = 'm';
  317.     s8.ca[1] = 'n';
  318.  
  319.     printf( "s8.ca[0]: %c\n", s8.ca[0] );
  320.     printf( "s8.ca[1]: %c\n\n", s8.ca[1] );
  321.  
  322.     S9 strc_9;
  323.  
  324.     S9 strc_9a[10];
  325.  
  326.     S10 strc_10;
  327.  
  328.     S10 strc_10a[10];
  329.  
  330.     printf("sizeof (S9): %zu\n", sizeof (S9));
  331.     printf("sizeof strc_9: %zu\n", sizeof strc_9);
  332.     printf("sizeof strc_9a: %zu\n", sizeof strc_9a);
  333.     printf("sizeof *strc_9a: %zu\n", sizeof *strc_9a);
  334.     printf("sizeof strc_9a[0]: %zu\n\n", sizeof strc_9a[0]);
  335.  
  336.     printf("sizeof (S10): %zu\n", sizeof (S10));
  337.     printf("sizeof strc_10: %zu\n", sizeof strc_10);
  338.     printf("sizeof strc_10a: %zu\n", sizeof strc_10a);
  339.     printf("sizeof *strc_10a: %zu\n", sizeof *strc_10a);
  340.     printf("sizeof strc_10a[0]: %zu\n\n", sizeof strc_10a[0]);
  341.  
  342.     return 0;
  343. }
  344.  
  345. /*
  346. output:
  347.  
  348. s0.c: o
  349. s0.i: 1
  350. s0.f: 1.200000
  351. s0.d: 12.100000
  352.  
  353. s1.c: a
  354. s1.i: 123
  355. s1.f: 1.230000
  356. s1.d: 12.300000
  357.  
  358. s2.c: a
  359. s2.i: 123
  360. s2.f: 1.230000
  361. s2.d: 12.300000
  362.  
  363. sizeof (struct st1): 24
  364. sizeof s2: 24
  365. sizeof &s2: 8
  366. sizeof s2.c: 1
  367. sizeof s2.i: 4
  368. sizeof s2.f: 4
  369. sizeof s2.d: 8
  370.  
  371. p_s3.c: a
  372. p_s3.i: 123
  373. p_s3.f: 1.230000
  374. p_s3.d: 12.300000
  375. sizeof p_s3: 8
  376. sizeof &p_s3: 8
  377. sizeof *p_s3: 24
  378. sizeof p_s3.c: 1
  379. sizeof p_s3.i: 4
  380. sizeof p_s3.f: 4
  381. sizeof p_s3.d: 8
  382. &s1: 0x555ef2857040
  383. p_s3: 0x555ef2857040
  384. &p_s3: 0x7ffc5cdffac0
  385. HEX: p_s3: 0x555ef2857040
  386.  
  387. s1 before:
  388.  
  389. s1.c: a
  390. s1.i: 123
  391. s1.f: 1.230000
  392. s1.d: 12.300000
  393.  
  394. s1 after:
  395.  
  396. s1.c: b
  397. s1.i: 321
  398. s1.f: 3.210000
  399. s1.d: 32.100000
  400.  
  401. test_s_1.c: c
  402. test_s_1.i: 22
  403. test_s_1.f: 2.200000
  404. test_s_1.d: 22.000000
  405.  
  406. test_s_2.c: d
  407. test_s_2.i: 222
  408. test_s_2.f: 2.220000
  409. test_s_2.d: 222.000000
  410.  
  411. test_s_3.c: d
  412. test_s_3.i: 333
  413. test_s_3.f: 3.330000
  414. test_s_3.d: 333.000000
  415.  
  416. p_s_3->c: e
  417. p_s_3->i: 111
  418. p_s_3->f: 1.110000
  419. p_s_3->d: 111.000000
  420.  
  421. s4.c: f
  422. s4.p_str: Andromeda
  423.  
  424. s4a.c: f
  425. s4a.p_str: Andromeda
  426.  
  427. (2) s4.c: f
  428. (2) s4.p_str: Zeppelin
  429.  
  430. (2) s4a.c: f
  431. (2) s4a.p_str: Andromeda
  432.  
  433. "Andromeda", length: 9
  434.  
  435. (1) s5.c: g
  436. (1) s5.str: Andromeda
  437.  
  438. "Zeppelin", length: 8
  439.  
  440. (2) s5.c: g
  441. (2) s5.str: Zeppelin
  442.  
  443. s5a.c: h
  444. s5a.str: Orion
  445.  
  446. s5b.c: r
  447. s5b.str: Capricorn
  448.  
  449. sizeof s6: 32
  450. sizeof &s6: 8
  451. sizeof s6.c: 1
  452. sizeof s6.s: 24
  453.  
  454. s6.s.c: j
  455.  
  456. sizeof s7: 344
  457. sizeof &s7: 8
  458. sizeof s7.c: 1
  459. sizeof s7.s: 336
  460.  
  461. s7.s[0].c: k
  462. s7.s[1].c: l
  463.  
  464. sizeof s8: 13
  465. sizeof &s8: 8
  466. sizeof s8.c: 1
  467. sizeof s8.ca: 12
  468.  
  469. s8.ca[0]: m
  470. s8.ca[1]: n
  471.  
  472. sizeof (S9): 20
  473. sizeof strc_9: 20
  474. sizeof strc_9a: 200
  475. sizeof *strc_9a: 20
  476. sizeof strc_9a[0]: 20
  477.  
  478. sizeof (S10): 64
  479. sizeof strc_10: 64
  480. sizeof strc_10a: 640
  481. sizeof *strc_10a: 64
  482. sizeof strc_10a[0]: 64
  483.  
  484. */
  485.  
Tags: struct sizeof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement