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;
}
- 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!
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 *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)
PrintEntry(n) refers back to PrintEntry(Entry *e)
As you can see I'm confused.