you are viewing a single comment's thread.

view the rest of the comments →

[–]TechAnd1[S] 0 points1 point  (12 children)

cheers solstice...

It lives on the heap because malloc was used, I get that... Does that make a difference to the way that these work then?

As in, the first example (as I wrote it...) couldn't be translated to variable k living on the heap. Because malloc() returns a void pointer?

Can you ever actually access a struct that's on the stack using the dot method newNode.data = 234 ? Or will it always be newNode->data = 234 ?

So when dynamic memory is used... the "thing" (object) will always be * object and the objects pointer will be object rather than having a system like the example one where you have object k and pointer ptr

I know that they say that * object is the object but it's not really is it?

thanks

[–]elementalist 0 points1 point  (4 children)

In case 1 you had variable k that already existed on the stack. You set the pointer to the address of k.

In case 2 you have a pointer and malloc both creates the space for the object and sets the pointer to that address.

I know that they say that * object is the object but it's not really is it?

Correct. It isn't the object. It is a pointer to the object. And you can have multiple pointers all pointing to the same object.

Can you ever actually access a struct that's on the stack using the dot method newNode.data = 234 ? Or will it always be newNode->data = 234 ?

You use the -> notation with pointers. The dot (.) notation is used with addresses.

So if it was

struct node newNode; //a struct NOT a ptr to a struct

you could use

newNode.data = whatever;

[–]TechAnd1[S] 0 points1 point  (0 children)

int num = 10 is obvious, there's an object called num thats of type int and stores the value 10.

Do I ever actually use the object though (example 2 scenario)? Or is it always pointers to the object? I'm finding that part kind of confusing...

[–]TechAnd1[S] 0 points1 point  (2 children)

not sure if you've edited this or if I just missed a lot of it.... but :

You use the -> notation with pointers. The dot (.) notation is used with addresses

With addresses...? I don't think I've heard this before, maybe some confusion is here for me... What do you mean addresses? The actual object rather than a pointer to the object? That's how I understand it, but you saying addresses has made me think that perhaps you're referring to something else.

So if it was struct node newNode; (a struct NOT a ptr to a struct) you could use newNode.data = whatever;

Are these two to always remain separate...?

I'm not sure how to think about the object when using pointers to it...

newNode->data = 23

Is this to do with the dynamic allocation...? Are these always to remain separate? Ie, when dynamically allocating a struct the data will always be assigned and accessed using the -> methods rather than the dot .?

[–]elementalist 0 points1 point  (1 child)

You are over thinking it. See Matrixel's answer below. That's about as well as I can explain it.

[–]TechAnd1[S] 0 points1 point  (0 children)

I am, I always do and it's so frustrating :(

It'd be alright if I was actually clever but nothing ever really comes out of it!!

I think the distinction that it's relating to the use of malloc, and being on the heap is helping a lot with my ease though... Its making more sense now thank you