1

I am currently trying to make function create() for singly linked list, where I am supposed to pass unlimited amount of parameters and it will pass the parameters as nodes' values. The code looks like this:

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

//define sllnode struct
typedef struct sllist
{
    int val;
    struct sllist *next;
}
sllnode;

sllnode* create(int count, ...);

int main(void)
{
    //here i try to print out values of this list
    sllnode* new_sllist = create(34,2,5,18);

    //print out values that I have assign using create() to test
    for(int i = 0; i < 4; i++)
    {
        printf("%i\n",new_sllist[i].val);
    }
}

//create function
sllnode* create(int count, ...)
{
    va_list list;
    int i;
    int arr[count];

    va_start(list, count);

    //create array arr that have all the values passed as parameters
    for(i = 0; i < count; i++)
    {
        arr[i] = va_arg(list,int);
    }

    //allocate memory for new singly linked list
    sllnode *sllist = malloc(count * sizeof(sllnode));

    //check if memory has been successfully allocated
    if(sllist == NULL)
    {
        printf("Unable to allocate memory.\n");
        exit(EXIT_FAILURE);
    }
    // loop through array arr and assign values to val and *next of each sllnode in new sllist
    for (int j = 0; j < count; j++)
    {
        sllist[j].val = arr[j];
        sllist[j].next = &sllist[j+1];

        if(j == count - 1)
        {
            sllist[j].val = arr[j];
            sllist[j].next = NULL;
        }
    }
    return sllist;
    free(sllist);
}

But when I print out I only receive the last 3 values (2,5,18) and a number -23791193490 which differs each time (I suppose this has seeped into another part of memory). How do I do this correctly?

3
  • 1
    OT: You are allocating a single block of memory for count list elements, which makes it difficult to free individual elements after manipulating the list in various ways. It is more conventional to allocate memory for each element individually. Commented Jun 10, 2020 at 11:01
  • I think you should also not use a variadic function for this. It reeks of syntax fixation. Commented Jun 10, 2020 at 11:42
  • Thank you for your feedback. I am almost 3 weeks into my coding ventures and currently trying to follow the instructions on cs50. Can you give some suggestions on how I can make this better and achieve the same result (when passing unlimited parameters then create same amount of nodes)? Commented Jun 10, 2020 at 15:45

1 Answer 1

1

You are passing 34 for the count parameter. Correct usage would be:

sllnode* new_sllist = create(4,34,2,5,18);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.