c programming, linked list error - c

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.

Related

Difference between these 2 declarations of structures in C

I'm working on this project for college and they gave me a sample code to use while declaring a structure, the other one is how I declared it with the information on PowerPoints and other study material.
This is the code they gave me:
typedef struct sala local, *plocal;
struct sala {
int id;
int capacidade;
int liga[3];
};
This is the code for another structure i did:
typedef struct pessoa {
char id[15];
int idade;
char estado;
int dias;
} pessoa;
Can anyone explain the difference to me ?
In my code editor "local" and "*local" appear in blue. (I use Netbeans).
This typedef declaration
typedef struct sala local, *local;
struct sala {
int id;
int capacidade;
int liga[3];
};
is invalid because the name local is declared twice with different meanings: the first one as an alias for the type struct sala and the second one as an alias for the type struct sala *.
This is the difference between the first and the second typedef declarations.:)
As for the placement of typedef declaration then it may be placed either before a corresponding structure definition. together with structure definition or after structure definition.
For example
typedef struct A A;
struct A
{
int x;
};
or
typedef struct A
{
int x;
} A;
or
struct A
{
int x;
};
typedef struct A A;
An essence difference between these declarations is that if you want to refer to the defined structure inside its definition then in the second and third cases you have to use the type name struct A because the typedef name A was not yet declared.
For example
typedef struct Node Node;
struct Node
{
int x;
Node *next;
};
but for example
typedef struct Node
{
int x;
struct Node *next;
} Node;

struct in c with an attribute that is a pointer to the same struct

i need to create a struct with an attribute that is a pointer to the same struct.
i'm trying this solution but not work:
typedef struct
{
int number;
void *other;
}mystruct;
extern mystruct first[];
extern mystruct second[];
mystruct first[] = {{1,NULL},{2,second}};
mystruct second[] = {{3,NULL},{4,first}};
mystruct *wrap;
wrap = (mystruct *)first[1].other;
int main(void){
printf("%d\n",first[0].number);
printf("%d\n",second[0].number);
printf("%d\n",wrap[1].number);
}
can someone help me?
best regards and thankyou
In C, you can name the struct before using it and typdefing it:
typedef struct mystruct_
{
int number;
struct mystruct_ *other;
} mystruct
I'm not entirely sure but are you looking for some sort of linked-lists or precisely speak Self Referential structure
struct list {
int something;
struct list *use_this_to_point_to_similar_type;
};
Here is another good reference what-is-self-referencing-structure-in-c
just a little bit simplification, and moving few instructions here and there, below code is a loosely written example of possibly what you are looking forward to achieve
#include<stdio.h>
struct mystruct
{
int number;
struct mystruct *other;
};
struct mystruct first[] = {{1,NULL},{2,NULL}};
struct mystruct second[] = {{3,NULL},{4,NULL}};
struct mystruct *wrap;
int main(void)
{
first[1].other = second;
second[1].other = first;
wrap = first[1].other;
printf("%d\n",first[0].number);
printf("%d\n",second[0].number);
printf("%d\n",wrap[1].number);
return 0;
}
your first and second don't need to be extern as they are allocated within your program. you can declare and init. var prior to the main. but the rest you must move into the main function:
int main(void){
wrap = (first[1].other);
printf("%d\n",first[0].number);
printf("%d\n",first[1].number);
printf("%d\n",second[0].number);
printf("%d\n",wrap[1].number);
return 0;}

"struct" before already-declared struct

I am still new in C. I know you can just use the already-declared struct as new data type such as int, double, etc. However, I encounter a struct written like this:
struct AdjListNode
{
int dest;
int weight;
struct AdjListNode* next;
};
In this struct, the data type of "next" pointer is struct AdjListNode*. What does struct have to do with the already-declared AdjListNode*? Thanks!
What does struct have to do with the already-declared AdjListNode*?
The answer is that the c syntax requires it.
You do not get a type AdjListNode by writing struct AdjListNode { ... };
AdjListNode is a struct tag and you always have to use struct AdjListNode when declaring variables.
See this simple example (without pointer inside the struct):
#include <stdio.h>
struct sSomeName
{
int x;
};
int main(void) {
struct sSomeName var; // OK, variable of type struct sSomeName
struct sSomeName* pVar; // OK, pointer to variable of type struct sSomeName
// sSomeName var2; // ERROR: unknown type name 'sSomeName'
var.x = 5;
pVar = &var;
printf("%d\n", pVar->x);
return 0;
}
So if you want to add a pointer inside the struct, you must write struct sSomeName just as you have to do inside main, i.e. like:
struct sSomeName
{
int x;
struct sSomeName* p;
};
Using typedef
If you want a type named AdjListNode you must use typedef.
A typedef example could look like:
#include <stdio.h>
typedef struct sSomeName sSomeName;
struct sSomeName
{
int x;
sSomeName* p;
};
int main(void) {
sSomeName var;
sSomeName* pVar;
var.x = 5;
var.p = NULL;
pVar = &var;
printf("%d\n", pVar->x);
printf("%p\n", (void*)pVar->p);
return 0;
}
Here with that declaration pointer to structure is declared. This is basically used for implementing linked list or for other data structures like tree.
It does'nt mean that struct is re-declared. It is similar to declare a struct variable.
The structure is created as follows: typedef struct AdjListNode. Example:
#include <stdio.h>
#include <stdlib.h>
typedef struct AdjListNode
{
int dest;
int weight;
struct AdjListNode* next;
}AdjListNode;
typedef struct Nodo{
char *nombre;
int *edad;
struct Nodo *siguiente;
}Nodo;
int main(int argc, char **argv) {
AdjListNode *nodo=malloc(sizeof(AdjListNode));
nodo->dest=1;
nodo->weight=2;
nodo->next=NULL;
printf("Nodo-->dest: %d", nodo->dest);
free(nodo);
}

dereferencing Pointer to incomplete type struct

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.

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;

Resources