1

I have to create a program that performs various functions with a linked list. I must save the functions in a separate file from the main() but then I can't figure out how to include the list in the header file. I tried different combinations and right now I have this in the linkedList.h file:

typedef struct list_elements* item;

And this in the linkedList.c

typedef struct list_elements{
    int value;
    struct list_elements *next;
}item

The problem is that when I try to compile the main.c I get the message: "request for member ‘value’ in something not a structure or union" and "request for member ‘next’ in something not a structure or union".

I looked on the suggested textbooks and online but I cannot find an explanation on how to use linked lists with header files?

(The functions work when I tested the whole thing in a single file, so I don't think they are the problem);

Chnossos
  • 9,971
  • 4
  • 28
  • 40
Hadron
  • 13
  • 1
  • 6
  • 2
    More code is needed, this is not enough code to figure out what's wrong. – Iharob Al Asimi Nov 19 '15 at 14:09
  • This doesn't make sense. The header file is included in the compiled program before it's ever executed. Are you saying you are recompiling the program after execution? – Rob Nov 19 '15 at 14:10
  • You're sure about the asterisk in `typedef struct list_elements item`? You've probably used `item head; head.value = ...`. – Zeta Nov 19 '15 at 14:11

3 Answers3

1

You are trying to typedef the same identifier twice as different types.

Remove the first typedef typedef struct list_elements* item; and put the second one in the header linkedList.h, which must be included in linkedList.c.

this
  • 5,229
  • 1
  • 22
  • 51
0

The declaration from your header defines type item as a pointer to a struct list_elements. The declaration from linkedList.c declares the same name, item, as an alias for type struct list_elements itself. Although this is not inherently wrong, it is certain to be confusing. It does make it wrong for linkedList.c to #include the header file, which ordinarily it would be appropriate for it to do.

You should make all your type declarations in the header file, and include it in both C source files to ensure that they share those declarations. Type declarations include at least typedefs, struct and union declarations, and function prototypes. This is not an exclusive list of things you might want to put in a header.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157
0

There is more than one way to do that:

one of them is to define the structures with the functions' prototype you want to use in a .h file, and the code of the functions in a separate .c (with the same prototypes written in the .h file of course), then you are now ready to use the structure in any other .c file by including your header file. e.g:

StType.h

typedef struct Something{
    int a;
    int b;
}
typedef struct anotherSomething{
    int c;
    int d;
}

int fct1();
int fct2();

Fct.c

include StType.h;

int fct1()
{
    //...
}
void fct2()
{
    //...
}

your program.c

include StType.h

int main()
{
    Something s;
    fct1();
}

I usualy does something like this.

A. Harkous
  • 252
  • 1
  • 3
  • 14