lyhsuan01
lyhsuan01

Reputation: 63

Why does the singly linked list print "NULL"?

I wrote a code that prints words in array of a singly linked list. But when I run the code, it will print (null) but not JAN. I have no idea why it keeps printing null. Can anyone helps me?

This is my code:

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

struct LLNode
{
    char *CharArr[10];
    struct LLNode *next;
};

struct LLNode * createNode (char val[])
{
    struct LLNode *temp;
    temp =(struct LLNode *)malloc(sizeof(struct LLNode));
    temp-> CharArr[10] = val;
    temp-> next = NULL;
    return (temp) ;
};

int main ()
{
    struct LLNode *head = NULL;
    struct LLNode *curr = NULL;
    //char a[10]="JAN";

    head = curr = createNode ("JAN") ;
    printf ("curr->CharArr[10] = %s\n", curr->CharArr[10]) ;
}

Upvotes: 1

Views: 127

Answers (2)

Aif
Aif

Reputation: 11220

I'm not sure what you're trying to achieve with CharArr: either your node carries the data, and you get rid of the pointer to the char array, or you want it to be a pointer to another region where the string lives, and get rid of the array part.

First version, where the node contains the characters:

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

struct LLNode
{
    char *CharArr[10];
    struct LLNode *next;
};

struct LLNode * createNode (char val[])
{
    struct LLNode *temp;
    temp =(struct LLNode *)malloc(sizeof(struct LLNode));
    temp-> CharArr[10] = val;
    temp-> next = NULL;
    return (temp) ;
};

int main ()
{
    struct LLNode *head = NULL;
    struct LLNode *curr = NULL;

    head = curr = createNode ("JAN") ;
    printf ("curr->CharArr[10] = %s\n", curr->CharArr[10]) ;
}

Second version, pointing to a region outside the Node:

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

struct LLNode
{
    char *CharArr;
    struct LLNode *next;
};

struct LLNode * createNode (char val[])
{
    struct LLNode *temp;
    temp =(struct LLNode *)malloc(sizeof(struct LLNode));
    temp->CharArr = strdup(val);
    temp-> next = NULL;
    return (temp) ;
};

int main ()
{
    struct LLNode *head = NULL;
    struct LLNode *curr = NULL;
    //char a[10]="JAN";

    head = curr = createNode ("JAN") ;
    printf ("curr->CharArr = %s\n", curr->CharArr) ;
}

Notice that in both cases, no cleanup is done on the node. In the second version, you also need to free() the CharArr.

See valgind output:

$ valgrind ./so 
==19670== Memcheck, a memory error detector
==19670== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==19670== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==19670== Command: ./so
==19670== 
curr->CharArr = JAN
==19670== 
==19670== HEAP SUMMARY:
==19670==     in use at exit: 20 bytes in 2 blocks
==19670==   total heap usage: 3 allocs, 1 frees, 1,044 bytes allocated
==19670== 
==19670== LEAK SUMMARY:
==19670==    definitely lost: 16 bytes in 1 blocks
==19670==    indirectly lost: 4 bytes in 1 blocks
==19670==      possibly lost: 0 bytes in 0 blocks
==19670==    still reachable: 0 bytes in 0 blocks
==19670==         suppressed: 0 bytes in 0 blocks
==19670== Rerun with --leak-check=full to see details of leaked memory
==19670== 
==19670== For lists of detected and suppressed errors, rerun with: -s
==19670== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Upvotes: 1

Joshua
Joshua

Reputation: 43268

Out of range indexing. Your program is undefined but it reduced to something really simple.

    temp-> CharArr[10] = val;
    temp-> next = NULL;

CharArr only has ten elements, and you wrote to the non-extant tenth element, so when you wrote to temp->next you overwrote val.

Maybe you think arrays are indexed from 1 to n, but they're indexed from 0 to n.

To see this work, change char *CharArr[10]; to char *CharArr[11];. But you probably want to reconsider that type altogether. It looks like you meant char CharArr[10]; and populate it by calling strcpy (#include <string.h>).

Upvotes: 1

Related Questions