0

Having this linked list:

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

struct node {
    int value;
    struct node *next;
};
typedef struct node node_t;

void printlist(const node_t*);
node_t *create_node(int);

int main(void){
    int values[3] = {1,2,3};
    node_t *nodes[3];
    for(int i =0; i<3 ; i++)
    {
        nodes[i] = create_node(values[i]);
        if(i!=2)
            nodes[i]->next = nodes[i+1]; //HERE, can I assign next (un)initialized node?
    }

    node_t *header = nodes[0];
    printlist(header);
}

void printlist(const node_t* header){
    for(const node_t *i = header; i; i=i->next)
        printf("value is %i\n",i->value);
}

node_t *create_node(int value){
    node_t *new = malloc(sizeof(node_t));
    new->value=value;
    new->next = 0;
    return new;
}

Which gives:

value is 1
value is 29590344

Command terminated

As I can see from output, the first node (header), does not have assign next struct member, which should happened from the loop. But I am assigning the next (pointer to new node) to an (un)initialized member from array (of pointer to nodes). I expect a initilization of array should also initilize a memory, when it has size. But does it? If so, then I do not understand why the assignment does not work, otherwise I understand and have to implement other loop. Thanks for answers.

autistic456
  • 183
  • 1
  • 10
  • Since `nodes[i+1];` hasn't been assigned yet, where do you expect it to point to? You also overwrite that address on the next iteration, so what you are doing is wrong on a logical level as well – UnholySheep May 30 '20 at 18:56
  • *"I expect a initilization of array should also initilize a memory, when it has size"* - The C standard disagrees on that. Unless you explicitly initialize the values of a non-`static` array all its member have *indeterminate* value – UnholySheep May 30 '20 at 18:57
  • @UnholySheep I am only assigning `0` for sure. So the last node will have null for `next`. Also, that is the question - I thought array does initilialize the address for each member (that was what I thought). So it does not - as I see – autistic456 May 30 '20 at 18:58
  • @UnholySheep ok, that could be answer. Maybe some standard quotation as addition, but as answer it suffice – autistic456 May 30 '20 at 19:01

1 Answers1

0

Your code does not create the dynamic list as you have an array of 3 nodes. next is not needed.

You probably want something like this.

/* you need to check if memory allocation was successful */

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

typedef struct node {
    int value;
    struct node *next;
} node_t;

void printlist(const node_t*);
node_t *append_node(node_t *, int);

int main()
{
    node_t *head = NULL, *parent = head;

    for(int x = 0; x < 10; x++)
        if(!head)
        {
            head = append_node(NULL, x);
            parent = head;
        }
        else
        {
            parent = append_node(parent, x);
        }

    printlist(head);
}

void printlist(const node_t* head){
    while(head)
    {
        printf("value is %i\n",head->value);
        head = head -> next;
    }
}

node_t *append_node(node_t *parent, int value){
    node_t *new = malloc(sizeof(*new));
    new->value=value;
    new->next = NULL;
    if(parent) parent -> next = new;
    return new;
}


0___________
  • 60,014
  • 4
  • 34
  • 74