Home > Software engineering >  invalid use of incomplete typedef in C
invalid use of incomplete typedef in C

Time:02-28

I'm implementing a data structure in C and I get this error in my test file. Without adding code because then that would be a huge post with a ton of code to go through, but here's what my code looks like:

header.h file:
typedef struct array Arr;

functions.c file:
#include "header.h"

struct array{
int number;
int size;
char *names;
}

main.c file:
#include "header.h"

bool function(const Arr *const variable)
{

for (int i = 0; i < variable->size; i  )
{
 variable->number[i] = i;
}
}

and so I get the error mentioned in the title referring to Arr*->number and Arr->*size. What I suspect to be the issue is that Arr is only typedefed but not defined. If that's the case, how can I resolve it?

Here's the main code:

  main.c
#include <stdio.h>
  #include "header.h"
  int main(){
  set *setA = set_empty();

  set_insert(69,setA );

  set_insert(15, setA);

    set *setB = set_empty();

    set_insert(12,setB );

    set_insert(15, setB);

  set *setDiff = set_difference(setA, setB);

  printf("\n");

  print_set(setDiff);

  bool diff = verify_difference(setDiff, setA, setB);
}

bool verify_difference(const set *const setDiff, const set *const setA, const struct set *const setB)

{

  bool answer = true;

  for (int x = 0; x < setDiff->size; x  )
  {
          if (set_member_of(setDiff->array[x], setA) && set_member_of(setDiff->array[x], setB))
          {
                  answer = false;
                  break;
          }
  }
  return answer;
}

header.h
#ifndef HEADER_H
#define HEADER_H
#include <stdbool.h>

typedef struct set set;

set *set_empty();

void set_insert(const int value, set *s);

bool set_member_of(const int value, const set *const s);

functions.c
#include <stdio.h>
#include "header.h"
struct set {

    int capacity;

    int size;

    char *array;

};

set *set_empty()
{

struct set *ptr = malloc(sizeof(struct set));

ptr->size = 0;

ptr->array = malloc(sizeof(char));

ptr->capacity = 1;

return ptr;
}
void set_insert(const int value, set *s)
{
    if (!set_member_of(value, s)) {
        int bit_in_array = value; // To make the code easier to read

        // Increase the capacity if necessary
        if (bit_in_array >= s->capacity) {
            int no_of_bytes = bit_in_array / 8   1;
            s->array = realloc(s->array, no_of_bytes);
            for (int i = s->capacity / 8 ; i < no_of_bytes ; i  ) {
                s->array[i] = 0;
            }
            s->capacity = no_of_bytes * 8;
        }

        // Set the bit
        int byte_no = bit_in_array / 8;
        int bit = 7 - bit_in_array % 8;
        s->array[byte_no] = s->array[byte_no] | 1 << bit;
        s->size  ;
    }
}
set *set_difference(const set *const s1, const set *const s2)
{

struct set *s = set_empty();


for (int i = 0; i < s1->size; i  )
{
  if (!set_member_of(s1->array[i], s2))
  {
    set_insert(s1->array[i], s);
  }
}

for (int i = 0; i < s2->size; i  )
{
  if (!set_member_of(s2->array[i], s1))
  {
    set_insert(s2->array[i], s);
  }
}

return s;
}

bool set_member_of(const int value, const set *const s)
{
    int bit_in_array = value;

    if (bit_in_array >= s->capacity) {
        return false;
    }

    int byte_no = bit_in_array / 8;
    int bit = 7 - bit_in_array % 8;
    char the_byte = s->array[byte_no];

    return the_byte & 1 << bit;
}

CodePudding user response:

The definition of the structure shall be available in main. Otherwise the compiler does not know whether there is the data member number in the structure referred in this statement

Arr->number[i] = i;

Moreover in any case this statement is incorrect because Arr is a type specifier and according to the structure definition the data member number is not an array

It seems you mean

variable[i].number = i;

But as the function parameter

bool function(const Arr *const variable)

is declared as a pointer to a constant object then you may not change pointed to data members of the structure.

So either move the definition of the function function from main.c in functions.c or place the structure definition in the header file.

And there is a typo

Typedef struct array Arr;
^^T

you need to use lower case letter

typedef struct array Arr;

CodePudding user response:

I can only hazard a guess. Your code snippet could be wrong.

Move the structure definition to header.h & check.

    //header.h file:
    typedef struct array Arr;
    
    struct array{
        int number;
        int size;
        char *names;
    };
  • Related