RicardasSim

struct

Dec 8th, 2020 (edited)
1,147
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.84 KB | None | 0 0
  1. // -std=c99 -Wall -Wextra -Wpedantic -Wshadow
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <string.h>
  6.  
  7. #define TEST_STR_MAX 512
  8.  
  9. struct st {
  10.     char str1[TEST_STR_MAX];
  11.     char str2[TEST_STR_MAX];
  12.     char *str3;
  13.     int iv;
  14.     double dv;
  15. };
  16.  
  17. int main()
  18. {
  19.  
  20.     struct st st_arr[100];
  21.  
  22.     char teststr[] = "Test";
  23.  
  24.     st_arr[0] = ( struct st ){ "One", "Two", NULL, 1, 0.1 };
  25.     st_arr[1] = ( struct st ){ "Ice", "Cream", NULL, 123, 2.1 };
  26.     st_arr[2] = ( struct st ){ 0 };
  27.  
  28.     // with designated initializers
  29.  
  30.     st_arr[3] = ( struct st ){ .str1 = "Penguin", .str2 = "Antarctica" };
  31.     st_arr[4] = ( struct st ){ .str2 = "Antarctica", .str1 = "Penguin", .dv = 321.1 };
  32.     st_arr[5] = ( struct st ){ .str1 = "Again", .iv = 5 };
  33.     st_arr[6] = ( struct st ){ .str2 = "String" };
  34.     st_arr[7] = ( struct st ){ .str3 = teststr };
  35.     st_arr[8] = ( struct st ){ .iv = 32 };
  36.  
  37.     for ( int i = 0; i < 9; ++i )
  38.     {
  39.         printf( "(%d) %s %s %s %d %f\n",
  40.             i,
  41.             strlen( st_arr[i].str1 ) ? st_arr[i].str1 : "ZeroLen",
  42.             strlen( st_arr[i].str2 ) ? st_arr[i].str2 : "ZeroLen",
  43.             st_arr[i].str3 ? st_arr[i].str3 : "NULL",
  44.             st_arr[i].iv,
  45.             st_arr[i].dv
  46.         );
  47.  
  48.         printf ("    *st_arr[%d].str1: %s *st_arr[%d].str2: %s\n",
  49.                 i,
  50.                 *st_arr[i].str1 ? "NonZero" : "Zero",
  51.                 i,
  52.                 *st_arr[i].str2 ? "NonZero" : "Zero"
  53.         );
  54.  
  55.         printf( "    %p %p %p\n",
  56.                 (void*) st_arr[i].str1,
  57.                 (void*) st_arr[i].str2,
  58.                 (void*) st_arr[i].str3
  59.         );
  60.  
  61.     }
  62.  
  63.     return 0;
  64. }
  65.  
  66. /*
  67. output:
  68.  
  69. (0) One Two NULL 1 0.100000
  70.     *st_arr[0].str1: NonZero *st_arr[0].str2: NonZero
  71.     0x7ffca60c7120 0x7ffca60c7320 (nil)
  72. (1) Ice Cream NULL 123 2.100000
  73.     *st_arr[1].str1: NonZero *st_arr[1].str2: NonZero
  74.     0x7ffca60c7538 0x7ffca60c7738 (nil)
  75. (2) ZeroLen ZeroLen NULL 0 0.000000
  76.     *st_arr[2].str1: Zero *st_arr[2].str2: Zero
  77.     0x7ffca60c7950 0x7ffca60c7b50 (nil)
  78. (3) Penguin Antarctica NULL 0 0.000000
  79.     *st_arr[3].str1: NonZero *st_arr[3].str2: NonZero
  80.     0x7ffca60c7d68 0x7ffca60c7f68 (nil)
  81. (4) Penguin Antarctica NULL 0 321.100000
  82.     *st_arr[4].str1: NonZero *st_arr[4].str2: NonZero
  83.     0x7ffca60c8180 0x7ffca60c8380 (nil)
  84. (5) Again ZeroLen NULL 5 0.000000
  85.     *st_arr[5].str1: NonZero *st_arr[5].str2: Zero
  86.     0x7ffca60c8598 0x7ffca60c8798 (nil)
  87. (6) ZeroLen String NULL 0 0.000000
  88.     *st_arr[6].str1: Zero *st_arr[6].str2: NonZero
  89.     0x7ffca60c89b0 0x7ffca60c8bb0 (nil)
  90. (7) ZeroLen ZeroLen Test 0 0.000000
  91.     *st_arr[7].str1: Zero *st_arr[7].str2: Zero
  92.     0x7ffca60c8dc8 0x7ffca60c8fc8 0x7ffca60e0a83
  93. (8) ZeroLen ZeroLen NULL 32 0.000000
  94.     *st_arr[8].str1: Zero *st_arr[8].str2: Zero
  95.     0x7ffca60c91e0 0x7ffca60c93e0 (nil)
  96.  
  97. */
  98.  
Advertisement
Add Comment
Please, Sign In to add comment