-4

I am trying to get information from Tree field I made, and I am getting the error in the title: dereferencing pointer to incomplete type 'struct List_t'

Tree Source File:

    struct Node_t{
Element data;
char* location;
struct Node_t*  son;
struct Node_t* next; 
};
    struct List_t{
Node head;
copyFunc copyfunc;
compareFunc compfunc;
freeFunc freefunc;
printFunc printfunc;
};

Tree Header File:

typedef struct Node_t* Node;
typedef struct List_t* Tree;
typedef void* Element;

App Source file:

Tree t;
t = createTree(compareInt, copyInt , freeInt, printInt);
int* x =(int*)malloc(sizeof(int));
*x=53;
Add(t, x);
char* location;
location= t->head->location; //here I got the error
printf(location);

what should I do? what am I doing wrong?

thanks!

MaorE
  • 1
  • 3
  • 1
    You should take the [tour], especially [ask]. – Yunnosch Dec 17 '17 at 14:40
  • Thanks for copying the actual error message into the question. Now, please, also copy all of the needed code into the question. There are competent and usually helpful people here who simply refuse following links. So you are reducing your chances for good answers by taking the screenshot shortcut. – Yunnosch Dec 17 '17 at 14:50
  • thanks for your review @Yunnosch I edited it. hopes now it make sense. – MaorE Dec 17 '17 at 15:11
  • Please read the link [mcve], guessing at the complex details of the concept just by reading the words is not enough. Your question contains now a lot of relevant code, but it is still far from being a MCVE. For example, it is still not visible whether and where you include the file you describe as "tree source file". It would also be interesting to know whether it is a compiled source file (tree.c) or an included header (tree.h), "trees source file" implies .c. – Yunnosch Dec 17 '17 at 15:23

2 Answers2

2

The declaration of struct List_t needs to be in the header file. Along with a declaration of createTree.

William Pursell
  • 204,365
  • 48
  • 270
  • 300
  • thanks. It has been already declerated. maybe it's something else? – MaorE Dec 17 '17 at 14:44
  • @MaorE If you think that the type mentioned by William is already declared at the point of your error, then please identify the line which does that. Note that the line in the tree source file is NOT the answer. Or it should not be - you are not including a source file are you? This would be much easier with a [mcve]. – Yunnosch Dec 17 '17 at 14:48
  • The compiler does not see the declaration of the struct. That's what the error means. You need to make it visible, and the easiest way to do that is to add it to the header. If this is an exercise, perhaps the intent is to demonstrate opaque types, which means you're not supposed to peak inside the struct internals but use some provided API. – William Pursell Dec 17 '17 at 14:48
  • @Yunnosch the second line in my header file is a decleration. am I wrong? – MaorE Dec 17 '17 at 15:24
  • @MaorE It is a declartion, yes. But of the type pointer-to-struct, not struct. – Yunnosch Dec 17 '17 at 15:28
0

You have provided three pieces of code, and identified them as:

  1. Tree Source File:

  2. Tree Header File:

  3. App Source file:

Let's call these files tree.c, tree.h and app.c

When you are compiling C source files, generally you have a single .c file that may contain lines like:

#include <stdio.h>
#include "tree.h"

inside it. That is how the compiler knows to go and look inside another file for a definition.

If your app.c file contains the lines above, then the code inside app.c may only make use of information provided by stdio.h and tree.h.

In particular, if you have provided information inside tree.c, that information is not visible to app.c because there is no #include directive that references it.

The solution is (as others have said) to move your struct definitions and typedef statements and any other parts of the public interface to be located in the tree.h file.

Alternatively, if you wish for the members of the structure to be private, you can provide a function that returns the data. Of course, the declaration of that function will be part of the public interface so it will have to be in the tree.h file as well (the definition of the function can be in tree.c, but the declaration will be public).

aghast
  • 14,785
  • 3
  • 24
  • 56
  • @MaorE Please note the additional effort this answer took, for describing the guesses and assumptions the answerer had to make. Do you see the difference of your code quotes to an actual MCVE? – Yunnosch Dec 17 '17 at 15:31