Home > other >  C struct pointer types are the same but still fails
C struct pointer types are the same but still fails

Time:10-13

I am building a C test program on Linux.

Why does this code compilation fail below? After list_t is type-defined, shouldn't the compiler understand later that it is the same type as a struct list ?

The attempt to create a new list item using malloc() fails with error: invalid type argument

#include <stdio.h>
#include <stdlib.h>

typedef struct list
{
    struct list *next_ptr; 
    int value;
}list_t;

int main()
{
   list_t myList;
   myList->next_ptr = malloc(sizeof(list_t)); // This fails
   return 0;
}

CodePudding user response:

myList->next_ptr should be myList.next_ptr because myList is not a pointer.

  • Related