dereferencing Pointer to incomplete type struct - c

When I try to compile, i get an Error saying :" dereferencing Pointer to incomplete type struct Freunde"
Thats my struct:
typedef struct {
char *Name;
struct Freunde *next;
} Freunde;
The Error happens here:
while (strcmp(Anfang->next->Name, Name) != 0)
Anfang = Anfang->next;
Edit/// So here is some more Code from the Programm I do try to run:
void add(Freunde* Anfang, char* Name) {
Freunde * naechster;
while (Anfang->next != NULL) {
Anfang = Anfang->next;
}
Anfang->next = (Freunde*) malloc(sizeof(Freunde));
naechster = Anfang->next;
naechster->Name = Name;
naechster->next = NULL;
}
int main() {
Freunde *liste;
liste = (Freunde*) malloc(sizeof(Freunde));
liste->Name = "Mert";
liste->next = NULL;
add(liste, "Thomas");
add(liste, "Markus");
add(liste, "Hanko");
Ausgabe(liste);
return 0;
}

The main problem is that you defined the next member of your structure as struct Freunde *next; but there is no struct Freunde in your code.
First declare a struct Freunde, like this
struct Freunde
{
char *name;
struct Freunde *next;
};
and then you could typedef, but you don't have to
typedef struct Freunde Freunde;
Also:
Do not cast the return value of malloc() for these reasons
Always check that malloc() did not return NULL.

Another aspect of the problem, or another way to think about it, is you are creating a typedef from a struct and attempting to include a pointer to that struct type as a member.
typedef struct {
char *Name;
struct Freunde *next;
} Freunde;
As explained, when you declare the member pointer struct Freunde *next;, the compiler has no idea what Freunde is yet. Thus the error.
To remedy this, you can either do as described in the other answer, or include a struct name tag in your declaration.
typedef struct Freunde {
char *Name;
struct Freunde *next;
} Freunde;
In this case struct Freunde {... tells the compiler that there is a struct named Freunde, so when it reaches your member struct Freunde *next; it is fine.

Related

Initialize a struct in C using {}

In the marked line I get an error Error - expected expression
#include <stdlib.h>
struct list_head {
struct list_head *next, *prev;
};
struct program_struct {
const char *name;
struct list_head node;
};
typedef struct program_struct program_t;
struct task_t {
program_t blocked_list;
};
int main() {
struct task_t *p = malloc(sizeof(*p));
p->blocked_list.name = NULL;
p->blocked_list.node = {&(p->blocked_list.node), &(p->blocked_list.node)}; //error
return 0;
}
I know I can replace this line with
p->blocked_list.node.next = &(p->blocked_list.node);
p->blocked_list.node.prev = &(p->blocked_list.node);
But can I make it work using {} like I tried in the first piece of code?
Initialization is allowed only when you define a variable. So, you can't use initializers in assignment.
You can instead use C99's compound literals:
p->blocked_list.node = (struct list_head) {&(p->blocked_list.node), &(p->blocked_list.node)}; //error

Dereferencing pointer to incomplete type

I am new to C programming and as a mini project I decided to try to implement a stack in C using OOP style structure in a file GenericStack.h as shown below:
void _GENERICSTACK0001(void *,void *);
void *_GENERICSTACK0002(void *);
int _GENERICSTACK0003(void *);
typedef struct
{
struct GenericStackNode *next;
void *data;
int type;
}GenericStackNode;
typedef struct
{
struct GenericStackNode *top;
int count;
void (*add)(void *,void *);
void *(*pop)(void *);
int (*hasNext)(void *);
int (*getCount)(void *);
}GenericStack;
GenericStack newGenericStack()
{
GenericStack *genStack = malloc(sizeof(GenericStack));
genStack->add = _GENERICSTACK0001;
genStack->pop = _GENERICSTACK0002;
genStack->hasNext = _GENERICSTACK0003;
genStack->getCount = _GENERICSTACK0003;
genStack->top=NULL;
genStack->count = 0;
return *genStack;
}
void _GENERICSTACK0001(void *self,void *data)//add
{
GenericStack *genStack = self;
if(genStack->top == NULL)
{
genStack->top = malloc(sizeof(GenericStackNode));
genStack->top->next = NULL;
genStack->top->type = 0;
genStack->top->data = data;
}
else
{
GenericStackNode *temp = malloc(sizeof(GenericStackNode));
temp->next = genStack->top;
temp->type = 0;
temp->data = data;
genStack->top = temp;
genStack->count++;
}
}
void *_GENERICSTACK0002(void *self)//pop
{
GenericStack *genStack = self;
void *data = NULL;
if(genStack->top == NULL)
{
return data;
}
else
{
GenericStackNode *temp = genStack->top;
genStack->top = genStack->top->next;
data = temp->data;
free(temp);
genStack->count--;
return data;
}
}
int _GENERICSTACK0003(void *self)
{
GenericStack *genStack = self;
return genStack->count;
}
All I need to know is why (among many others) I get the specific error:
GenericStack.h:41:16: error: dereferencing pointer to incomplete type
genStack->top->type = 0;
I have checked the other answers on stackoverflow concerning "dereferencing pointer to incomplete type" but I cant seem to understand.
You're getting an error from GenericStack, but you have a problem in both GenericStack and GenericStackNode.
In C, struct X and X are different types. When you write:
struct GenericStackNode *next;
it declares a type called struct GenericStackNode (and a member which is a pointer to that type). This type is incomplete because you have not provided the struct definition.
The type could be completed by providing a struct definition later, but you never do that. Instead, you define an unnamed struct and typedef GenericStackNode to it , but that has no effect on struct GenericStackNode.
Then, struct GenericStackNode *top; still uses this same incomplete type, not the struct you defined above.
Assuming you meant for this pointer to be a pointer to the same type of struct it's contained in, you could use this pattern for both of your structs:
typedef struct X X;
struct X
{
X *ptr;
};
Often people combine the typedef with the struct definition but I find it clearer to have them separate.
You already type-defined GenericStackNode as a type, there is no need for struct GenericStackNode anymore, just GenericStackNode :
typedef struct
{
struct GenericStackNode *top;
...
}
should be only
typedef struct
{
GenericStackNode *top;
...
}
also , you can't use GenericStackNode when you still havn't defined it yet :
typedef struct
{
struct GenericStackNode *next;
void *data;
int type;
} GenericStackNode ;
you can write :
typedef struct GenericStackNode
{
struct GenericStackNode *next;
void *data;
int type;
} GenericStackNode ;

Dereferencing pointer to incomplete type(with well defined structs) in C

I know there are at least 10 questions already about this, but they all point to something I am not doing.
In a header file I have...
typedef struct Node {
struct Node *next;
struct pgmap page;
} Node;
typedef struct linkedlist {
struct Node *head_ptr;
struct Node *tail_ptr;
} LList;
In my c file I have
struct LList mainList;
int main()
{
struct LList *root;
root = &mainList;
root->head_ptr = NULL;
root->tail_ptr = NULL;
...
}
On the root-> lines I get the dereferencing ptr... error. All the threads already on here point to a problem where people accidentally create anonymous structs, such as
typedef struct{
int a;
}; monkey
instead of
typedef struct monkey{
int a;
}; monkey
So what am I missing????
There is no type called "struct LList". The code "typedef struct linkedlist { ... } LList;" creates two type names: one is struct linkedlist, and the other is just LList (without "struct"). You thus need to change "struct LList" to "LList."

Dereferencing pointer to incomplete type error in C

I bumped into this error when I was trying to access a field in my defined struct:
struct linkNode{
struct linkNode *next;
char *value;
};
In the header file I defined a type called linkNode_t:
typedef struct linkNode linkNode_t;
When I tried to use this struct in the main of another file, everything else was fine except when I tried to do
linkNode_t* currentpath = /*a pointer to a struct of type linkNode_t*/
int something = strlen(currentpath->value);/****ERROR*****/
Compiler gave me the incomplete type error. Am I declaring the struct properly?
Struct has to be declared in header, before you do typedef. You can combine both:
typedef struct linkNode {
struct linkNode *next;
char *value;
} linkNode_t;
As the others pointed out, it's generally better to put your "typedef" and your struct definition all in the same place.
But that isn't required, and that's not the problem.
This test case compiles and runs correctly:
#include <stdio.h>
#include <string.h>
#define NULL 0
struct linkNode{
struct linkNode *next;
char *value;
};
typedef struct linkNode linkNode_t;
linkNode_t rec = {
NULL,
"abcdef"
};
int
main (int argc, char *argv[])
{
linkNode_t* currentpath = &rec;
int something = strlen(currentpath->value);
printf ("sizeof (rec)= %d, currentpath->value= %s, something= %d...\n",
sizeof (rec), currentpath->value, something);
return 0;
}
ACTUAL PROBLEM AND SOLUTION:
1) You're doing all the right stuff.
2) Just make sure you put your "typedef" AFTER (or, at least, as part of) your struct definition:
struct linkNode{
...
};
typedef struct linkNode linkNode_t;
struct linkNode{
struct linkNode *next;
char *value;
};
This is incomplete because you cannot use struct directly inside the structure.
You should use
typedef struct linkNode{
struct linkNode *next;
char *value;
}new_name;

c programming, linked list error

When i try to compile the following code, i get the error
test.c: In function 'main':
test.c:16: error: incompatible types in assignment
and the code is..
#include <stdio.h>
#include <ctype.h>
#include <string.h>
typedef struct {
char name[20];
struct planet* next;
} planet;
int main(int argc, char *argv[])
{
planet *p, *start, *first, *second, *third;
strcpy(start->name, "Suthan");
start->next = *first;
}
1) Allocate some memory to your pointers.
planet *start, *first; these are uninitialized pointers.
start->next // that's dereferencing an uninitialized pointer.
2) You're setting start->next (a pointer to a planet) to a deferenced pointer first. That's the root of your error.
start->next = *first; // should be = first;
3) Move the typedef name to get rid of the warning you were seeing.
typedef struct planet{
char name[20];
struct planet* next;
};
Since are trying to assign an instance of planet (*first) to a pointer to planet (start->next).
Try this instead:
start->next = first;
However, I'm also wondering about your declaration of planet. Does this help?
typedef struct _planet {
char name[20];
struct _planet* next;
} planet;
I think you did your struct incorrectly
typedef struct _planet {
char name[20];
struct _planet* next;
} planet;
Should be:
typedef struct planet {
char name[20];
struct planet* next;
}
I remember tripping over this aspect of self-referential structs.
typedef struct {
char name[20];
struct planet* next; /* <-- the trouble is here */
} planet;
In the middle of the struct definition, which is in the middle of a typedef definition, the compiler doesn't know what a struct planet looks like, but is happy to let you define a pointer to it. The compiler doesn't really need to know what it looks like until you dereference *next. By the time you get to the body of main() you still haven't told the compiler what a struct planet is, only a type called planet which happens to be an unnamed struct.
Consider the following approach.
struct foo {
int bar;
};
typedef struct foo foo_type;
struct foo bar;
foo_type baz;
This shows that the name of the struct is foo and the name of the defined type is foo_type. I could combine them like this:
typedef struct foo {
int bar;
} foo_type;
You can resolve the compiler warning by adding a dummy name to your inline struct definition.
typedef struct planet_struct {
char name[20];
struct planet_struct* next;
} planet;
You will also need to address the memory allocation issue that others have pointed out, but this answers the question you didn't actually ask.

Resources