Error: In C, got the error “dereferencing pointer to incomplete type” in a struct pointer?

When you try to access the a member of the t0 struct the compiler needs to know how this struct looks like (for example to see if there even is any a member in it). Since you didn't put the struct test type definition anywhere where the compiler can see it when compiling 2. C you get an error.

The compiler doesn't know what a struct test contains If you put the definition of the struct test in 1. H the compiler sees how that type looks like and can use the struct members Just put the complete type definition in 1. H that's where it's supposed to be.

When you try to access the a member of the t0 struct the compiler needs to know how this struct looks like (for example to see if there even is any a member in it). Since you didn't put the struct test type definition anywhere where the compiler can see it when compiling 2. C, you get an error.

The compiler doesn't know what a struct test contains. If you put the definition of the struct test in 1. H, the compiler sees how that type looks like and can use the struct members.

Just put the complete type definition in 1. H, that's where it's supposed to be.

About the correct place of struct test I thugh that it was in *. C and typedef in *. H Uhn.. When I do this, I get segmentation fault at the position of t0->a = 0.

I should have said that in place od 0, it will get a value from a input from user; here: pastebin. Com/ZbudbbVE – Shuryon Mar 11 at 17:23 @Shuryon: The correct place is in the *.h. About the segmentation fault: probably this means that createMem didn't actually set t0 to point to something useful.

– sth Mar 11 at 18:12.

Somewhere you have a preprocessed file that has typedef struct test testT; Which doesn't include struct test { int a; }; Preprocessing inlines all the #includes directives. As long as you were only using a testT pointer, the compiler would have known to "allocate a pointer's worth of memory" and the compilation would have progressed further than expected. When you actually try to use that pointer to dereference something, the compiler would then realize it NEEDED the full definition of "struct test" and you would get the error displayed.

You meand I need to include the correct *. H, right? But it is (I think), so I don't know what to do.

Take a look: pastebin. Com/ZbudbbVE – Shuryon Mar 11 at 17:10.

If you want the struct to be usable both in 1. C and 2. C, it must be defined in a header file that is visible to both.

I don't know why you say that this is "wrong", it's common practice and AFAIK there is no other way around that directly. If it's only defined in 1. C, then the compiler has no idea if struct test has a member named "a" when processing 2.c.

Another option is to just keep the forward declaration as you have now, but also include accessor/mutator functions in the header. Then 2. C does not have to know about the "internals" of struct test, but can act on it.

This is also very common in C APIs. (You could also define the struct identically both in 1. C and 2.

C but that's a very bad idea. ).

But I did #include "1. H" at 2.c. The real code: pastebin.Com/ZbudbbVE – Shuryon Mar 11 at 17:17 "1.

H" does not contain a definition of struct test. It only says "there is a struct test somewhere" and defines another name for that type. With just that, when looking at "2.

C", the compiler does not know what struct test actually is, so it can only do things with it that don't require any knowledge of the struct (like handling pointers to it). Struct test is an "opaque type" at this point. You might want to search for that term to find more information.

– Mat Mar 11 at 17:39.

But, when running the compiled program, exactly where the error occurs is the place of t0->a. The pointer to the allocated memory is actually in *t, not t (as seen from your createMatrix code at Pastebin), so you should really be doing: (*t)0. A and similarly in your for loop: (*matriz)i.row.

Uhn.. another error appeared doing it: error: invalid use of undefined type ‘struct positionLeaf’ – Shuryon Mar 11 at 17:07 @Shuryon: Well you're mixing up two different kinds of errors. My answer was about run-time errors, what you're getting is a compile-time error (which others have discussed). – casablanca Mar 11 at 17:50.

The definition of struct Test is only visible inside the file 1.c. The code t0->a doesn't see that this struct has a member named a. The types shared between several compile units shuld be defined in a header!

You should know that C/C++ compiles each . C file separately, so it has no way to know that the structure is defined in some other . C file.

You should perhaps do the following: (1. H) struct test { int a; }; ... (1. C) #include "1.

H" ... (2. C) #include "1. H" ... void funcTest(testT **t, int *size, ..){ createMem(t,*size); /* void createMem(testT **t, int size); */ t0->a = 0; /* ... more code ... */ }.

Yeah, it is this way, take a look: pastebin. Com/ZbudbbVE But still, the error is here >_row when running the programm. At the for loop – Shuryon Mar 11 at 17:46 @Shuryon: it doesn't compile at my side.

I pasted the log here. – Vlad Mar 11 at 21:11 @Shuryon: or you mean that it compiles my way? Could you please paste the code which segfaults?

– Vlad Mar 11 at 21:28.

The 'full code' is at pastebin.com - here (won't expire). But I think that with the explanation below, anybody can understands. Note: I haven't implemented yet the function that will erase the allocated memory and other things.

I have a struct defined in a 1. I have a 1. Then I have a function that has a parameter in it depending on testT, wich is in 2.

The bizarre thing is that the code compile when I use struct test at 1. H file (removing struct test from 1. C - which is wrong).

But, when running the compiled program, exactly where the error occurs is the place of t0->a.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions