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

I just got into structsstructs and iI decided to create a list using them. Here is my code :

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

    struct list
    { 

    
     int x;  
    struct list *pointer1;
  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 Is the use of one pointer only possible? By the way i, I am pretty sure iI could do the same thing using an array of structsstructs too.Any Any advice would be really helpful!

I just got into structs and i decided to create a list using them. Here is my code :

#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!

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!

Source Link
RookieCookie
  • 363
  • 2
  • 3
  • 9

Creating a list using a struct in C

I just got into structs and i decided to create a list using them. Here is my code :

#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!