0

I've really been struggling with PSET5 so I decided to take a step back and write a simple linked list program. My program take in an array of characters as a command line argument and stores each character in a linked list and prints them out.

My program more works as is, but when I move one line of code the list won't print and I can't figure out why.

Near the bottom of my code I remove:

// DELETE THIS NEXT LINE
printList(list);

Near the start of the code I uncomment:

// UNCOMMENT THIS LINE
// printList(list);

Moving this one line of code cause the printing to not work and I can't figure out why. I've screwed around with the debugger for quite some time, and it seems like my printList() function doesn't think anything is in the list. Why is this?

Both my addToList() and printList() functions take the *list variable as a pointer? Doesn't that mean the list is passed by reference and it should still exist by the time printList() calls it?

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

typedef struct character
{
    struct character* next;
    char storedCharacter;
}
character;

void addToList(character *list, char* argv[]);
void printList(character *list);


int main(int argc, char* argv[])
{
    // start a list
    character *list = NULL;

    addToList(list, argv);
    // UNCOMMENT THIS LINE
    // printList(list);
}

void addToList(character *list, char* argv[])
{
    for (int i = 0; i < 45; i++)
    {
        if (argv[1][i] == '\0')
        {
            break;
        }
        else if (i == 0)
        {
            character *c = malloc(sizeof(character));
            c->storedCharacter = argv[1][i];
            c->next = NULL;
            list = c;
        }
        else
        {
            character *c = malloc(sizeof(character));
            c->storedCharacter = argv[1][i];

            // point new node to start of list
            c->next = list;

             list = c;
        }
    }
    // DELETE THIS NEXT LINE
    printList(list);
}

void printList(character *list)
{
    for (character *tmp2 = list; tmp2 != NULL; tmp2 = tmp2->next)
    {
        printf("%c\n", tmp2->storedCharacter);
    }
}
0

C does not have the concept of pass-by-reference, because C does not have reference. Everything in C is passed by value. It did print out a b c for me if I passed in abc from the command line.

addToList(list, argv);
// UNCOMMENT THIS LINE
printList(list);

list is a scalar variable. its value is passed to addToList. The parameter of addToList is another scalar variable. the value of list (the one in main) is copied to it.

In your following up printList(list), the list keep the same value as before calling addToList.

what you want is

characters list;

addToList(&list);
printList(&list);

If you tried my link with the visualization, you should be able to see, the list in main is never updated.

In addToList

  list = c;

should be changed to

  list->next = c;

Another tip, using printf to print out the addresses and the contents of all the list, you will see you are not updating the same memory location.

8
  • It seems to work for me. Are you running it with one command line argument? Like: ./fileName abc
    – BigTJ
    Jun 13 '20 at 5:49
  • I ran without any arguments.
    – stensal
    Jun 13 '20 at 6:08
  • I added your argument to the command line. Here is a new link, you click "Record" this time, and watch how your linked list is constructed in visualized execution.
    – stensal
    Jun 13 '20 at 6:14
  • From the debugger I have a pretty good idea that the list is being constructed fine. For some reason my list variable is going out of scope after the addToList method completes the list. Not sure why it's going out of scope, I passed the pointer to the list to each method. Shouldn't both methods be able to access and modify it, with the list "remembering" the changes"
    – BigTJ
    Jun 13 '20 at 17:40
  • Yes, both addToList and printList access the same list. Try the above link and click "Record" button. You should be able to see how the list is created and accessed in pictures.
    – stensal
    Jun 13 '20 at 19:09
0

I tried asking another question stripping out all the extra junk that I had in this question. By the time I finished the question I think I mentally reconciled why this is happening. See

Pointer passed as argument, then suddenly goes null

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .