Error when compiling a modularized program in c

Asked

Viewed 259 times

1

I made a program in which reads strings from a file separated by - (hyphen) and saves each string at a position of a struct vector.

When compiling, generates the following error message:

array type has incomplete element type

I searched the net and saw that a solution would be to write the struct implementation inside the file structures. h, however, I would like to leave the implementation hidden, leaving only the prototypes in . h

Would it be possible?

Follows the code:

main. c

#include <stdlib.h>
#include "estruturas.h"

int main()
{
    String vetor_de_string[MAX];
    leArquivo(vetor_de_string);

    return 0;
}

structures. h

#ifndef ESTRUTURAS_H_
#define ESTRUTURAS_H_
#define MAX 50

typedef struct string String;

void leArquivo(String *s);

#endif

structures. c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "estruturas.h"

struct string
{
    char nome[20];
};

void leArquivo(struct string *s)
{
    FILE *f;

    f = fopen("data.txt", "r");

    if(!f)
    {
        printf("*** Erro: Nao foi possivel abrir o arquivo! ***\n");
        exit(1);
    }

    int l = 0, i = 0;
    char aux, a[20], b[20], c[20];

    while((aux = fgetc(f)) != EOF)
    {
        if(aux == '\n')
            l++;
        if(l > 0)
        {
            fscanf(f, "%19[^-]s", a);   
            aux = fgetc(f); 
            fscanf(f, "%19[^-]s", b);
            aux = fgetc(f);
            fscanf(f, "%19[^\n]s", c);

            strcpy(s[i].nome, a);
            strcpy(s[++i].nome, b);
            strcpy(s[++i].nome, c);
            i++;
        }
    }
    fclose(f);
}
  • What do you call hiding the implementation? Implementation of what?

  • I referred to the struct string implementation. I don’t want to implement (define) it in the header file (header) called: structures. h But Dan Getz solved my problem. Plus, thanks for the interest. :)

1 answer

0


It is impossible to construct a vector of a type without knowing the size of the type, because the size of the vector is the size of each element multiplied by the number of elements. In structures. c it is known that each String has a size of 20, but in main. c no, so it’s impossible to know what the size of vetor_de_string. So either you have to put the definition of struct string in the header, or only use pointers to type. (It is known the size of a pointer to a type, without knowing the size of the type.) For example, you can modify the type of the argument from leArquivo() of String* for String** and add the following:

Structures. h

String *novaString();

Structures. c

String *novaString() {
    return malloc(sizeof(struct string));
}

and uses novaString() in main() to allocate the vector:

int i;
String *vetor_de_string[MAX];
for (i = 0; i < MAX; i++) {
    vetor_de_string[i] = novaString();
}

Then, if the programme is not being completed soon, the elements of vetor_de_string:

for (i = 0; i < MAX; i++) {
    free(vetor_de_string[i]);
}
  • Thank you very much Dan Getz. Already had a while that was breaking my head with this. rsrs Thanks!!

Browser other questions tagged

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