PDA

View Full Version : [SOLVED] help with understanding linked lists



alegomaster
September 3rd, 2011, 12:32 AM
Hi, I am wondering if someone can tell me how the "->" operator works. A code sample demonstrating it in an simple manner would be helpful too.

Thanks in ahead,
Alex

sisco311
September 3rd, 2011, 12:34 AM
You forgot to specify the language. :)

alegomaster
September 3rd, 2011, 12:37 AM
You forgot to specify the language. :)


Wow i am so stupid](*,). I program in C, so C would be nice to see.

schauerlich
September 3rd, 2011, 01:01 AM
Do you know how structs work, and how to access fields? '->' is used to access fields on POINTERS to structs. If you have a struct named "foo" with a field "bar", you can access is like this:


struct foo_t foo;
foo.bar = 42;

Now, if we use a pointer:


struct foo_t *blah = &foo;
blah->bar = 256;

alegomaster
September 3rd, 2011, 01:13 AM
struct foo_t *blah = &foo;
blah->bar = 256;

Oh, now I get it. I was thinking of it wrongly. Blah is a pointer to foo, and you need the "->" operator to emulate the "." operator.

So "->" is used for pointer of structs, and "." is used for structs.

schauerlich
September 3rd, 2011, 01:14 AM
Yup. foo->bar is equivalent to (*foo).bar