An incomplete type dereference pointer

I saw a lot of questions on this, but I will ask the question differently without special code. Is there a way EASY to determine what causes a type to be incomplete? In my case, I use someone else's code elses, and I’m absolutely sure that I don’t have the correct headers, but (since computers do it much faster and better than human eyeballs), there is a way to make the compiler say, β€œhey, you you think you have type X on line 34, but it's actually missing. " The error itself appears only at the appointment, which is not very useful.

+58
c struct dereference typedef compiler-errors
Apr 23 2018-10-23T00:
source share
7 answers

I saw a question the other day when someone inadvertently used an incomplete type, specifying something like

struct a { int q; }; struct A *x; x->q = 3; 

The compiler knew that struct A was a structure, even though A was completely undefined thanks to the struct keyword.

This was in C ++, where such use of struct is atypical (and, as it turns out, can lead to pedestrian shooting). In C, if you do

 typedef struct a { ... } a; 

then you can use as TypeName and omit - a struct later. This will cause the compiler to give you an undefined identifier error later, and not an incomplete type if you type the name incorrectly or forget the header.

+46
Apr 23 '10 at 17:38
source share

What does it mean that an error appears only upon appointment? For example, on GCC, without assignment in sight:

 int main() { struct blah *b = 0; *b; // this is line 6 } 

incompletetype.c:6: error: dereferencing pointer to incomplete type .

The error is on line 6, where I used an incomplete type, as if it were a full type. I was still fine.

The error is that you must include any header that defines the type. But the compiler cannot figure out which line should have been included: any line outside the function will be fine, to a large extent. Also, it is not going to be wasted through every text file in your system, looking for a header that defines it, and suggests you include it.

Alternatively (a good point, potatoswatter) the error is on the line where b was defined when you wanted to specify some type that actually exists, but actually blah is specified. Finding the definition of the variable b should not be too complicated in most cases. IDEs can usually do this for you, compiler warnings may not be a concern. However, this is pretty awful code if you cannot find a definition of what you are using.

+10
Apr 23 '10 at 17:37
source share

Another possible reason is indirect reference. If the code refers to a structure that is not included in the current c file, the compiler will complain.

a-> b-> c // error if b is not included in the current file c

+8
Mar 01 '17 at 10:06 on
source share

I do not quite understand what the problem is. An incomplete type is not a type that is "missing." Incompete type is a type declared but not defined (in the case of structure types). Finding a non-developing ad is easy. As for detecting the missing definition ... the compiler will not help you, because this is exactly what caused the error.

The main cause of incomplete type errors in C is typos in type names that prevent the compiler from matching one name with another (for example, when matching declarations with definitions). But then again, the compiler will not help you. The compiler does not guess typos.

+7
Apr 23 '10 at 17:35
source share

this error usually shows whether the name of your structure is different from the initialization of your structure in the code, so usually c will find the name of the structure being created and if the original structure is not found, it is usually either pointing the pointer to a pointer, an error will appear.

+1
Jan 09 '12 at 3:11
source share

A - Solution

Speaking of C, I just found empirically that the following declaration code would be a solution;

 typedef struct ListNode { int data; ListNode * prev; ListNode * next; } ListNode; 

So, as a rule, I give the same name for both the type definition and the structure name;

 typedef struct X { // code for additional types here X* prev; // reference to pointer X* next; // reference to pointer } X; 

B - Problem Patterns

If the following declarations are considered incomplete by the gcc compiler when the following statement is executed .;

 removed->next->prev = removed->prev; 

And I get the same error for the dereference code displayed in the error output;

 >gcc Main.c LinkedList.c -o Main.exe -w LinkedList.c: In function 'removeFromList': LinkedList.c:166:18: error: dereferencing pointer to incomplete type 'struct ListNode' removed->next->prev = removed->prev; 

For both header file declarations listed below;

 typedef struct { int data; ListNode * prev; ListNode * next; } ListNode; 

Plus this one:

 typedef struct ListNodeType { int data; ListNode * prev; ListNode * next; } ListNode; 
0
Feb 12 '17 at 22:26
source share

Outside of possible scenarios related to optimizing the entire program, code is generated for something like:

 struct foo *bar; struct foo *test(struct foo *whatever, int blah) { return blah ? whatever: bar; } 

completely independent of what members of struct foo can contain. Since make utilities usually recompile any compilation unit in which a full definition of the structure appears, even if such changes cannot actually affect the code they generate, it usually omits the full definitions of the structure from compilation units that do not really need them, and such an omission usually not worth the warning.

The compiler must have a complete structure or connection definition in order to know how to process objects of type declarations with automatic or static duration, aggregate declarations containing type elements, or code that refers to members of the structure or union. If the compiler does not have the information necessary to perform one of the above operations, it will have no choice but to curse it.

By the way, there is another situation where the Standard allows the compiler to require that the full definition of the join be visible, but does not require diagnostics: if two structures start with the Common Initial Sequence, and the type of join containing both is visible when the compiler processes code that uses a pointer to one of the structure types for checking a member of this Common Initial Sequence, the compiler must recognize that such code can access the corresponding structure element of another type. I don’t know which compilers, if any, adhere to the standard when the view of the continuous union is visible, but not when it is not [gcc is inclined to generate inappropriate code anyway if the -fno-strict-aliasing flag is used, in this case it will generate the appropriate code in both cases], but if you want to write code that uses the CIS rule in such a way as to guarantee the correct behavior on compatible compilers, you may need to make sure that the full definition of the connection type is visible; failure to do this may lead to the fact that the compiler will automatically generate dummy code.

0
Feb 13 '17 at 16:30
source share



All Articles