0

Hello,
I'm relatively new to c++, so I would appreciate any help.
This must be a very basic issue, but how is it that one can assign a stuct or class member to a pointer instead of that structs or class member's address (via &)?

(from stroustrup)

struct name{
char* string;
name* next;
double value;
};

in other function:

name* nn = new name;

3
Contributors
4
Replies
5
Views
9 Years
Discussion Span
Last Post by telemachos9
0

new returns a newly allocated address pointing to enough memory to hold a name structure.

BTW, your use of the word "member" threw me off at first because the members are string, next, and value, which don't enter into your question.

0

thank you for your answer, but isn't that still syntactically incorrect, assigning an object to equal a pointer?

0

It isn't assigning an object to equal a pointer. It is creating a pointer that points to a block of memory that has been allocated on the heap to hold the data in the struct.

name* nn; // declares a pointer to point to a name struct
nn = new name; // allocates a block of memory on the heap and stores the address to the block of memory (pointer) in nn;

0

ok, thank you, gents. I was banging my head on that for a while. So it's really just like passing an address, &name?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.