1

I'm writing this post after hours of madness and thinking. Probably this is the most stupid exercise you are gonna read today but for me, after hours of exercise, is not like that.

Going back to the question. My professor requested an allocation of a dynamic array inside a linked list. And this point is nothing hard. I wrote the structure and define 2 types. The next step is to write 2 functions:

  1. The first one, called init, creates a new element of the list, allocates the array using an n integer, and returns it to the main;
  2. A print function to show what the arrays have at their inside.

The code looks like that.

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

struct elements{
    int  *array;
    int size;
    struct elements* next; 
};
typedef struct elements elementOfList; 
typedef elementOfList* ListOfElements;

ListOfElements init(int n){
    ListOfElements new;
    new->array = malloc(sizeof(int)*n);
    for(int i = 0; i < n; i++)  new->array[i] = 0;
    new->size = n;
    new->next = NULL;
    return new;
}

void print_list(ListOfElements list){
    if(list == NULL) return;

    printf("%d",list->size);
    print_list(list->next);
}

int main(){

    ListOfElements list = init(4);
    print_list(list);
    // -> n = 4 | 0, 0, 0, 0,

    list->next = init(12);
    print_list(list);
    // -> n = 4 | 0, 0, 0, 0,
    // -> n = 12 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, %

    return 0;
}

As you can see, returning a "listOfElements" to the main makes me do a disaster. At this point, I think I messed up a lot of things. The algorithm went in a loop at the second print. No problem with the printing of the first array but with the second one....shit... (I know I actually don't print the array. I print the size of the array only to make it more readable right now).

I think that my mistake is linked to the "init" function. Something went wrong and I can't understand where. I hope somebody can help or even suggest to me some fixes to the program.

While I wait that somebody to read this post I will try to put on paper what the frick my program is doing.

Thank you for your attention and have a nice day.

9
  • 3
    First of all: never hide a pointer type behind a typedef (like in typedef elementOfList* ListOfElements;), it only causes confusion. Then: in init, right after ListOfElements new;, where do you think new points to? Commented Jan 19, 2022 at 18:59
  • Something else: if your list contains one single element, what do you think print_list prints? Commented Jan 19, 2022 at 19:09
  • ListOfElements new = malloc(sizeof(*new));. Commented Jan 19, 2022 at 19:11
  • @Jabberwocky - "First of all: never hide a pointer type behind a typedef (like in..." A lot agree with you on this, but then some do not. Its an opinion thing, so Never is too strong a word here. But at least the typedef could be names something to indicate it is a pointer, like typedef elementOfList* Ptr_ListOfElements;; Commented Jan 19, 2022 at 19:13
  • @ryyker yes, it might be opinion. But it really causes confusion unless the pointer is a blackbox, or maybe a pointer to a function Commented Jan 19, 2022 at 19:16

1 Answer 1

1

The function init shall allocate an object of the type elementOfList

ListOfElements init(int n)
{
    ListOfElements new = malloc( sizeof( *new ) );
    
    if ( new != NULL )
    {
        new->array = malloc( n * sizeof( int ) );

        if ( new->array == NULL ) n = 0;

        new->size = n;
        for ( int i = 0; i < n; i++ )  new->array[i] = 0;
        // or you can use memset instead of the for loop

        new->next = NULL;   
    }

    return new;
}

and the function print_list should output elements of the inner array. For example

void print_list(ListOfElements list)
{
    if ( list != NULL )
    {
        printf("-> %d | ",list->size);
      
        for ( int i = 0; i < list->size; i++ )
        {
            if ( i != 0 ) printf( ", " ); 
            printf( "%d", list->array[i] );
        }

        putchar( '\n' );

        print_list(list->next);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

I wouldn't use recursion, because the problem is not inherently recursive.

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.