Could C-structs have builders?

Asked

Viewed 213 times

0

I have the following structure within a program:

 struct STR00034ST{

     char C_O010XC14B, C_O020XC14B;
     short S_0010XI15C;
     float F_0010XI1D;

     STR00034ST (char _C_O010XC14B, char _C_O020XC14B,  short _S_0010XI15C, float _F_0010XI1D){

        C_O010XC14B=_C_O010XC14B;
        C_O020XC14B=_C_O020XC14B;
        S_0010XI15C=_S_0010XI15C; 
        F_0010XI1D=_F_0010XI1D;
    }
};

But whenever I compile it returns the following error: 'error: expected specifier-Qualifier-list before 'STR00034ST''

I’ve been researching and realized that I only found examples of struct constructor use within the C++ language and not in C. Therefore, it is impossible to use constructors inside a C struct?

1 answer

2


It is not possible to use constructors in C, so you would have to create an auxiliary function that creates and initializes structs. In C99 there is also a technique called Compound Literal, that builds a structure in-place. The syntax (in English) would be:

( type ) { initializer-list } ,

for example,

struct foo {int a; char b[2];} structure;.

  • Just your boot example that doesn’t boot anything. The rest seems right to me

Browser other questions tagged

You are not signed in. Login or sign up in order to post.