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;

Recommended Answers

All 4 Replies

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.

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

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;

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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.