Skip to main content
2 of 2
added 34 characters in body
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Creating a list using a struct in C

I just got into structs and I decided to create a list using them:

#include<stdio.h>
#include<stdlib.h>
int main(void)
{
    char counter;

    struct list
    {

        int x;  
        struct list *pointer1;
    
    };


    struct list test;
    struct list *pointer2;

    pointer2=&test;

    for(counter=0;counter<5;counter++)
    {

        pointer2->pointer1=malloc(sizeof(struct list)); 
        pointer2=pointer2->pointer1;    
    
    }   
}

I was wondering if there is another way to do the exact same thing. Is the use of one pointer only possible? By the way, I am pretty sure I could do the same thing using an array of structs too. Any advice would be really helpful!

RookieCookie
  • 363
  • 2
  • 3
  • 9