Click here to Skip to main content
15,890,345 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In declaring a structure, I went this way;
to hold item numbers like this "104101-101A", in this definition:

typedef struct modernitem_struct {
union {
struct moditem_struct {
char modgrpnum[3];
char modsubgrpnum[3];
char modhypen[1];
char moditemextn[3];
char moditemsubindtr[1];
*-->>>
} mod_struct;
struct mod_whole {
char whole_mod_item[12];
} whole_fyitem;
} w_union;
} full_mod_item;

/// a "C" structure definition...
full_mod_item moditem;

strcpy(moditem.w_union.whole.fyitem.whole_mod_item, "\0");
strcpy(moditem.w_union.whole.fyitem.whole_mod_item, "104101-101A");

The Question:

Since the "struct moditem_struct" structure hold 104101-101A in pieces,
The sum of the "struct moditem_struct" structure holds 11 characters.
And the sum of the "struct mod_whole" structure holds 12 characters.
NOW, do I need to add a character placeholder just to hold the null
character at the line indicated(*-->>>), to make 12 characters?

Thanks, for any help.

What I have tried:

It seems to work, but it's mainly question. I'm having problems around the routine it is contained in. Thanks.
Posted
Updated 7-Jun-21 20:07pm

1 solution

No, the struct is sized so the largest of the two structs in the union will fit.
To check, declare them all separately:
C
#include<stdio.h>
typedef struct moditem_struct 
    {
    char modgrpnum[3];
    char modsubgrpnum[3];
    char modhypen[1];
    char moditemextn[3];
    char moditemsubindtr[1];
    } mod_struct;

typedef struct mod_whole 
    {
    char whole_mod_item[12];
    } whole_fyitem;

typedef struct modernitem_struct 
    {
    union 
        {
        mod_struct ms;
        whole_fyitem wf;
        } w_union;
    } full_mod_item;

full_mod_item moditem;
mod_struct ms;
whole_fyitem wf;
int main()
    {
    printf("%ld\n", sizeof(mod_struct));
    printf("%ld\n", sizeof(whole_fyitem));
    printf("%ld\n", sizeof(full_mod_item));
    return 0;
    }
And you will get "sensible" results:
11
12
12
 
Share this answer
 
Comments
CPallini 8-Jun-21 3:07am    
5.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900