0

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
  • 2
    start->next = first;, *first is the struct, you need the pointer. Commented Dec 10, 2012 at 19:54

4 Answers 4

3

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;
};
Sign up to request clarification or add additional context in comments.

1 Comment

It may be dereferencing an initialized pointer, but it won't cause this particular compiler error.
1

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;

2 Comments

test.c:17: warning: assignment from incompatible pointer type sorry same error
Change typedef struct to typedef struct planet
0

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;
}

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.