Ways to initialize struct’s

Asked

Viewed 626 times

3

I know these two ways to boot struct's.

#include <stdio.h>
typedef struct {
    char s[10];
    int a, b;
    double x;
} TIPO;
int main(){
    TIPO v1[10]={"foo", 5, 13, 4.3, "bar", 0, 0, 2.3};
    TIPO v2[10]={{"FOO", 10, 1, .3},{ "Barrr", 10, -20, 39.22}};
    printf("%s : %d : %d : %.3lf\n", v1[0].s,  v1[0].a, v1[0].b, v1[0].x);
    printf("%s : %d : %d : %.3lf\n", v1[1].s,  v1[1].a, v1[1].b, v1[1].x);
    printf("%s : %d : %d : %.3lf\n", v2[0].s,  v2[0].a, v2[0].b, v2[0].x);
    printf("%s : %d : %d : %.3lf\n", v2[1].s,  v2[1].a, v2[1].b, v2[1].x);
}

Upshot:

foo : 5 : 13 : 4.300
bar : 0 : 0 : 2.300
FOO : 10 : 1 : 0.300
Barrr : 10 : -20 : 39.220

What other ways to initialize structs in C11/C18?

How to initialize, say, b=1 for all elements of v1?

2 answers

4


The fact that something works doesn’t mean that I should always use, some cases work by coincidence, in a specific situation, I would do it in the most semantic way ever.

Basically I can remember a normal case (there may be others I don’t remember or possible tricks) which is the use of named members instead of positional:

TIPO v3[10] = {{ .s = "FOO", .a = 10, .b = 1, .x = .3 }, { .a = 10, .s = "Barrr", .b = -20, .x = 39.22 }};

Another way:

TIPO v3[10];
strcpy(v3[0].s,"FOO");
v3[0].a = 10;
v3[0].b = 1;
v3[0].x = .3;
v3[1].a = 10;
strcpy(v3[1].s, "Barrr");
v3[1].b = -20;
v3[1].x = 39.22;

Apart from that you can create abstractions that help you, but within those abstractions it will always be the form you used in the question or those new ones I showed you, then of course you will use variables and not literals as is the example of the question. You must evaluate whether these abstractions are necessary, in C less abstractions are used than in other languages. I gave examples without them because the question uses crudely.

To initialize an equal member for all elements would be something like this )depends exactly on what you want):

for (int i = 0; i < 10; i++) v1[i].b = 1;

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Remembering that this will not initialize other members. If you want this you have to do the same for each of them.

1

C is a medium/low level language with no direct object support. The structs are a very nice way to indicate to the compiler a layout of a memory area, and with syntax to fill and edit several fields of this area )of the structs).

But structs are essentially "dumb" - they know nothing - it’s like a jig rule on top of a blank paper. Assignment only (use of operator =) fills these memory areas with the desired values, while running the program.

What you need to give "intelligence" to structs is to create a series of functions, one for each thing you want for structs, - each one can be very simple. In common, they all get a pointer to the struct that you’re going to work on in the first element - and operating on that structure provides the "intelligence" needed for the object.

Having "functions associated with a data structure" is essentially the first benefit of Object Orientation (after OO has some other concepts, implemented on top of that, such as inheritance, etc).

To make it easier to read your code, place the name of the struct, or data type, as a prefix of the functions.

In this case, to always ensure startup with b=1:

#include <stdio.h>
typedef struct {
    char s[10];
    int a, b;
    double x;
} TIPO;

TIPO * tipo_init(TIPO *tipo) {
   tipo->s[0] = "\x0";
   tipo->a = 0;
   tipo->b = 1;
   tipo->x = 0;
   return tipo
}

TIPO * tipo_init_vector(TIPO * vector, int length) {
   int i;
   for (i=0; i<length; i++) {tipo_init(vector[i]);}
   return vector;
}

TIPO * tipo_create_vector(int length){
   TIPO * vector;
   vector = malloc(length * sizeof(TIPO));
   if (vector != NULL) {
       tipo_init_vector(vector, length);
   }
   return vector;
}

main() {
   TIPO *v1, *v2;
   v1 = tipo_create_vector(10);
   ...
}

Browser other questions tagged

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