Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

C struct pointer types are the same but still fails

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading