Home > Blockchain >  Filling an array of structs with a for loop in c89
Filling an array of structs with a for loop in c89

Time:12-02

I'm trying to fill a table with structs inside a for loop, and I can't find anywhere how it's supposed to be done. Here is the code for the struct :

#define LEN 255

typedef struct {
    int number;
    char surname[LEN];
    char name[LEN];
} entry;

And how I'm attempting to read them from a file :

#include <string.h>
#define MAX_TAB 400

int read_entries (FILE* f, entry table[MAX_TAB]) {
    int i, number;
    char name[LEN], surname[LEN];

    for (i = 0 ; i < MAX_TAB ; i   ) {
        if (fscanf(f, "%d %s %s\n", &number, surname, name) != 3) {
            break;
        }
        table[i] = {number = number, surname = surname, name = name};
    }

    return i;
}

Unfortunately this doesn't work, as it seems struct initialisers are only available at variable declaration in C89. Then how do I use the values I just read to fill the table ? If possible, I would like answers that do not use malloc.


Here is a test file for convenience :

0 Liddell Alice
1 Sponge Bob
2 DaSilva Carlos
3 AndGoliath David
4 Eden Eve
5 Mirror Faith
6 Divine Grace

CodePudding user response:

fscanf(f, "%d %s %s\n", &number, surname, name)

You can't store data "inside" uninitialized pointers, that's not how pointers work. See Crash or "segmentation fault" when data is copied/scanned/read to an uninitialized pointer


table[i] = {number = number, surname = *surname, name = *name};

C89 doesn't have any convenient way to do this using an initializer list. You'll have to use assignment etc:

char surname[100];
fscanf(..., surname, ...);

table[i].number = number;
table[i].surname = strdup(surname);
...

(strdup is as of the time I'm writing this widely available but not yet standard C. It will however get added in the upcoming C23 revision of the language.)

  • Related