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.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • 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? – Jabberwocky Jan 19 '22 at 18:59
  • Something else: if your list contains one single element, what do you think `print_list` prints? – Jabberwocky Jan 19 '22 at 19:09
  • `ListOfElements new = malloc(sizeof(*new));`. – Ian Abbott Jan 19 '22 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](https://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers). 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;;` – ryyker Jan 19 '22 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 – Jabberwocky Jan 19 '22 at 19:16
  • @Jabberwocky - Agreed. Microsoft does this with its APIs (_black boxes_). – ryyker Jan 19 '22 at 19:19
  • @ryyker Microsoft overuses it, IMHO. Virtually every `Type` has a `typedef Type *PType;` and maybe some qualified variants. – Ian Abbott Jan 19 '22 at 19:22
  • Hiding pointers to *incomplete* types behind typedefs may be acceptable, even though I would prefer a struct holding such a pointer any day of the week. But pointers to concrete data types? Please don't. – n. m. could be an AI Jan 19 '22 at 19:28
  • typedeffing pointers in a clear fashion, eg: 'pMyStruct', 'MyStructPtr', can avoid "My god, it's full of stars" dereference nightmares. – Martin James Jan 19 '22 at 22:39

1 Answers1

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);
    }
}
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335