-2

I want to add item to a linked list (course object).

course* start = ....;       // course pointer as start of list
course* c = ....;           // another course object pointer to add to list

Then to add to the list, I use:

course* temp = start;       // copy start current value to a temp object
start = c;                  // set start value to newest object
c->next = temp;             // linked back to previous object
return start;               // return result

That works, but now I want to wrap that into a function named addToEnd():

addToEnd(start, c);

with

void addToEnd(course* start, course* c)
{
    // LIFO
    course* temp = start;       // copy start current value to a temp object
    start = c;                  // set start value to newest object
    c->next = temp;             // linked back to previous object
}

The change occurs only inside the function, and has no effect outside. What have I done wrong?

And NO! My question is different from the suggested 'LinkedList C++ implementation' question.

CaTx
  • 1,421
  • 4
  • 21
  • 42
  • I strongly recommend making a linked list class that contains linked nodes which contain a `course`. The direction you are headed looks better suited to programming in C. Not that that's a bad thing, but not the best way to write C++. – user4581301 Nov 23 '17 at 23:41
  • I do agree that is more generic. However, I am only to solve this problem first. – CaTx Nov 23 '17 at 23:50
  • I'd even more strongly recommend using the standard library list and vector classes. –  Nov 23 '17 at 23:53
  • Possible duplicate of [LinkedList C++ implementation](https://stackoverflow.com/questions/11140249/linkedlist-c-implementation) – Dr. Xperience Nov 24 '17 at 02:21

2 Answers2

1

Note: I wrote this thinking C, not C++. The answer works, but isn't ideal. @user4581301's answer is better.

The short answer is: Changes to "start" won't be visible outside the function, but changes to "*start" will be.

Likely you would want to pass a pointer to a pointer to the function.

So something like

addToEnd(&start, c);

and

void addToEnd(course** start, course* c)
{
    // LIFO
    course* temp = *start;       // copy start current value to a temp object
    *start = c;                  // set start value to newest object
    c->next = temp;             // linked back to previous object
}
EdmCoff
  • 3,506
  • 1
  • 9
  • 9
1

Now this is going to sound silly, but you are passing the pointers by value.

Silly right? They're pointers. You don't get much more pass by reference than a pointer. Well, it's the item being pointed AT that's being passed by reference. The pointer itself... Not so much.

So what happens is start is an Automatic (also known as local) variable that contains an address. The address the caller passed into the function is copied into start. When you change start inside the function it is changing the copy and when the function ends, start is gone. The caller has no clue start was ever changed.

If you want to update a pointer inside a function, you have to pass the pointer itself by reference

void addToEnd(course*& start, course* c) // note the only change required is the 
                                          // addition of the &
{
    // LIFO
    course* temp = start;       // copy start current value to a temp object
    start = c;                  // set start value to newest object
    c->next = temp;             // linked back to previous object
}

so that the caller gets the updated value.

More reading: What's the difference between passing by reference vs. passing by value?

user4581301
  • 33,082
  • 7
  • 33
  • 54