0

I can't figure this out for the life of me. This is a linked list example.

I'm a beginner. I've been looking at this for about an hour and I'm ready to fold and look for assistance. Heh.

So basically the issue is that there is a pointer on one of the functions, and pointers inside the functions, and pointers bloody everywhere and I can't seem to figure out the series of logical steps that are being taken here.

What I did finally figure out is that Entry *newOne is defining a "struct Entry" pointer. I don't really get what is happening in the full statement, or how the different parts are calling values. At all.

More specifics below.

#include <iostream>
#include <vector>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

struct Entry {
    string name, phone;
    Entry *next;
};

void PrintEntry(Entry *e)
{
    cout << e->name << " " << e->phone << endl;
}

Entry *GetNewEntry()
    {       
        Entry *newOne = new Entry;
        cout << "Enter name (ENTER to quit):";
        string name;
        getline (cin,name);
        if (name == "") return NULL;
        newOne->name = name;
        cout << "Enter phone: ";
        string phone;
        getline(cin, phone);
        newOne->phone = phone;
        newOne->next = NULL; // no one follows
        return newOne;
    }
int main () {
    Entry *n = GetNewEntry();
    PrintEntry(n);
    return 0;
}
  1. Entry *newOne = new Entry (I don't understand this - isn't new Entry just an address for a struct Entry? and isn't Entry *newOne a pointer? Then isn't this just assigning the value of the pointer to an address... quite lost.

And if newOne is simply an address (which it is after verifying) then why does saying newOne->phone=phone do anything? That doesn't make sense!

  1. Entry *GetNewEntry() (I don't understand this - at the end of the function an address to the newOne entry is returned - does the * add to this "return" value perhaps)

  2. Entry *n = GetNewEntry() (With relation to the function having the pointer symbol on it - GetNewEntry either returns the newOne memory address, or the newOne pointer - and Entry *n being a struct Entry pointer would then be set either to that memory address (much like Entry *newOne = new Entry) or it would be set to the pointer to that address... ugh)

  3. PrintEntry(n) refers back to PrintEntry(Entry *e)

As you can see I'm confused.

max_b_f
  • 31
  • 8
  • Pick up a good book from [here](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) and read about pointers and how they work. – vsoftco Jan 20 '16 at 03:41
  • `if (name == "") return NULL;` memory leak!! You don't delete`newOne` in this case. – Paul Rooney Jan 20 '16 at 03:43
  • So whats the question? – Paul Rooney Jan 20 '16 at 03:44
  • I added some specific questions to the bottom after the code. I'm just confused about the way pointers are being used here. – max_b_f Jan 20 '16 at 03:48
  • "Then isn't this just assigning the value of the pointer to an address" - "address" and "pointer value" mean the same thing. – user253751 Jan 20 '16 at 03:50
  • I thought it worked like this: int*p; int x; p=&x; (p is the address of x, *p is the value at x) - in the example Entry *p = new Entry sets *p as containing the value of a memory address (I thought new Entry returned a memory address)? and at the end return newOne returns an address. I need to slow down and think this through a bit more. I forgot that p is the value of the address of the thing the pointer is pointing to, and *p is the value stored at the address. I'll go from there. I'm still lost about the Entry *GetNewEntry() function and the overall logic of the total program. – max_b_f Jan 20 '16 at 03:52
  • @max_b_f `Entry *p = new Entry` does *not* do the same thing as `*p = new Entry` even though they look similar. In this case the `*` is part of `Entry *` not part of `*p`. – user253751 Jan 20 '16 at 04:16
  • in `X = Y` it means that `Y` gets stored in `X` (not the other way around as your first question suggests) – M.M Jan 20 '16 at 04:37
  • BTW this is not very good code so trying to learn from it is not a great idea – M.M Jan 20 '16 at 04:38
  • I've been doing a lot more thinking about this. It's actually really cool. I did have a question about what Paul Rooney meant about memory leak in that particular space of the code. I'm reading about Stack/Heap here http://gribblelab.org/CBootcamp/7_Memory_Stack_vs_Heap.html and it's all coming together very nicely. The code in OP is (mostly) directly copied from lecture 12 of Stanford's Programming Abstractions course. I am curious why you think it's poor code if you're interested in sharing your thoughts. – max_b_f Jan 20 '16 at 05:25
  • I think I would find it easier to understand pointers and C++ in general if I had a much more clear and all-encompassing guide to exactly what is going on in the CPU/RAM with any given program. I am just a bit confused about how the computer determines whether memory is occupied or not. If I ran my program now, how does the computer actually decide where to place things in the RAM? In my mental image, I see everything placed on the same part of the chip, like in a small micro-block or something. – max_b_f Jan 20 '16 at 05:33

2 Answers2

0

The first thing in this code:

struct Entry {
string name, phone;
Entry *next;};

is a linked list of possible phone book entries. I hope you are familiar with the linked list otherwise check here http://www.cprogramming.com/tutorial/lesson15.html

If you trace the code in main it is very simple. First of all it creates an Entry by calling GetNewEntry function. This function prompts the user for data like name and phone number and create the Entry object.

It sets the "next" variable in the structure as null because probably this is the only element it is going to create and returns the created object back to the main function.

Now the PrintEntry function will print the data inside the Entry that just has been created. From my point of view the "next" variable in the Entry struct is useless here but maybe in your lesson there is going to be a LinkedList which make use of this variable.

Pooya
  • 6,083
  • 3
  • 23
  • 43
0
  1. Entry *newOne = new Entry; (I don't understand this - isn't new Entry just an address for a struct Entry? and isn't Entry *newOne a pointer? Then isn't this just assigning the value of the pointer to an address... quite lost.

A pointer is a variable that holds an address. new Entry creates a new Entry on the heap, and then returns its address. Then this address is stored in newOne. This line also declares newOne, but it could have been written separately as Entry *newOne; newOne = new Entry;.

And if newOne is simply an address (which it is after verifying) then why does saying newOne->phone=phone do anything? That doesn't make sense!

Why wouldn't it? newOne->phone is short for (*newOne).phone. *newOne means "the thing whose address is newOne", so it's the Entry that we just created a moment ago (with new Entry). So (*newOne).phone is that Entry's member called phone.

  1. Entry *GetNewEntry() (I don't understand this - at the end of the function an address to the newOne entry is returned - does the * add to this "return" value perhaps)

Entry * is the return type. It could alternatively be written as Entry* GetNewEntry(). This function returns a pointer to an Entry (or the address of an Entry; call it whichever you want). * in a type means "pointer to" (or "address of") the thing before it.

  1. Entry *n = GetNewEntry(); (With relation to the function having the pointer symbol on it - GetNewEntry either returns the newOne memory address, or the newOne pointer - and Entry *n being a struct Entry pointer would then be set either to that memory address (much like Entry *newOne = new Entry) or it would be set to the pointer to that address... ugh)

This means the same as Entry *n; n = GetNewEntry();. It declares the variable n (with type Entry*). Then it calls GetNewEntry(). Then it sets n to the return value from GetNewEntry()

  1. PrintEntry(n) refers back to PrintEntry(Entry *e)

This is a function call. I'm not sure what you're asking here.

user253751
  • 57,427
  • 7
  • 48
  • 90
  • Thanks, this should be enough for me to go off of. It helps a great deal to get specific help with something I'm working on instead of just reading more books and spending five times as much time getting the information. I've already been reading for hours. Wanting to ask a question for one thing is not a crime. Thank you. – max_b_f Jan 20 '16 at 04:02
  • Actually, function calls have me a bit confused, but that will have to wait. I'm sort of just confused about what goes INTO the function and how all that works. I'm not going to rush into that and instead will just feel satisfied that I'm starting to get a feel for pointers for now. – max_b_f Jan 20 '16 at 04:03