Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // -std=c99 -Wall -Wextra -Wpedantic -Wshadow
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define TEST_STR_MAX 512
- struct st {
- char str1[TEST_STR_MAX];
- char str2[TEST_STR_MAX];
- char *str3;
- int iv;
- double dv;
- };
- int main()
- {
- struct st st_arr[100];
- char teststr[] = "Test";
- st_arr[0] = ( struct st ){ "One", "Two", NULL, 1, 0.1 };
- st_arr[1] = ( struct st ){ "Ice", "Cream", NULL, 123, 2.1 };
- st_arr[2] = ( struct st ){ 0 };
- // with designated initializers
- st_arr[3] = ( struct st ){ .str1 = "Penguin", .str2 = "Antarctica" };
- st_arr[4] = ( struct st ){ .str2 = "Antarctica", .str1 = "Penguin", .dv = 321.1 };
- st_arr[5] = ( struct st ){ .str1 = "Again", .iv = 5 };
- st_arr[6] = ( struct st ){ .str2 = "String" };
- st_arr[7] = ( struct st ){ .str3 = teststr };
- st_arr[8] = ( struct st ){ .iv = 32 };
- for ( int i = 0; i < 9; ++i )
- {
- printf( "(%d) %s %s %s %d %f\n",
- i,
- strlen( st_arr[i].str1 ) ? st_arr[i].str1 : "ZeroLen",
- strlen( st_arr[i].str2 ) ? st_arr[i].str2 : "ZeroLen",
- st_arr[i].str3 ? st_arr[i].str3 : "NULL",
- st_arr[i].iv,
- st_arr[i].dv
- );
- printf (" *st_arr[%d].str1: %s *st_arr[%d].str2: %s\n",
- i,
- *st_arr[i].str1 ? "NonZero" : "Zero",
- i,
- *st_arr[i].str2 ? "NonZero" : "Zero"
- );
- printf( " %p %p %p\n",
- (void*) st_arr[i].str1,
- (void*) st_arr[i].str2,
- (void*) st_arr[i].str3
- );
- }
- return 0;
- }
- /*
- output:
- (0) One Two NULL 1 0.100000
- *st_arr[0].str1: NonZero *st_arr[0].str2: NonZero
- 0x7ffca60c7120 0x7ffca60c7320 (nil)
- (1) Ice Cream NULL 123 2.100000
- *st_arr[1].str1: NonZero *st_arr[1].str2: NonZero
- 0x7ffca60c7538 0x7ffca60c7738 (nil)
- (2) ZeroLen ZeroLen NULL 0 0.000000
- *st_arr[2].str1: Zero *st_arr[2].str2: Zero
- 0x7ffca60c7950 0x7ffca60c7b50 (nil)
- (3) Penguin Antarctica NULL 0 0.000000
- *st_arr[3].str1: NonZero *st_arr[3].str2: NonZero
- 0x7ffca60c7d68 0x7ffca60c7f68 (nil)
- (4) Penguin Antarctica NULL 0 321.100000
- *st_arr[4].str1: NonZero *st_arr[4].str2: NonZero
- 0x7ffca60c8180 0x7ffca60c8380 (nil)
- (5) Again ZeroLen NULL 5 0.000000
- *st_arr[5].str1: NonZero *st_arr[5].str2: Zero
- 0x7ffca60c8598 0x7ffca60c8798 (nil)
- (6) ZeroLen String NULL 0 0.000000
- *st_arr[6].str1: Zero *st_arr[6].str2: NonZero
- 0x7ffca60c89b0 0x7ffca60c8bb0 (nil)
- (7) ZeroLen ZeroLen Test 0 0.000000
- *st_arr[7].str1: Zero *st_arr[7].str2: Zero
- 0x7ffca60c8dc8 0x7ffca60c8fc8 0x7ffca60e0a83
- (8) ZeroLen ZeroLen NULL 32 0.000000
- *st_arr[8].str1: Zero *st_arr[8].str2: Zero
- 0x7ffca60c91e0 0x7ffca60c93e0 (nil)
- */
Advertisement
Add Comment
Please, Sign In to add comment