Author Topic: [C++] question about -> operator  (Read 1346 times)

0 Members and 1 Guest are viewing this topic.

Offline neuron

  • /dev/null
  • *
  • Posts: 6
  • Cookies: 0
    • View Profile
[C++] question about -> operator
« on: March 27, 2014, 06:23:17 pm »
So I'm learning C++ from the tutorial that Kulverstukas posted. I already know a little C so I understand the extreme basics like variable types, structs, conditional statements, etc. However, pointers to structs don't seem to make much sense. I understand that a struct is really just a collection of variables grouped together under one name and that those variables must have memory addresses. However, how can you make a pointer to a struct as a whole? Stemming from that, how does the -> operator actually work when accessing members of a struct? I understand what it does, but I would really like to know how it actually works when compiled. I'd try to figure it out with GDB but it's not installed on my computer (yet) and I don't have internet access. If anyone knows, help would be greatly appreciated!

Offline ca0s

  • VIP
  • Sir
  • *
  • Posts: 432
  • Cookies: 53
    • View Profile
    • ka0labs #
Re: [C++] question about -> operator
« Reply #1 on: March 27, 2014, 06:36:50 pm »
Go
Code: [Select]
http://gcc.godbolt.org/
Paste
Code: [Select]
#include <stdio.h>

struct herp {
    int a;
    int b;
  } gderp;

int main()
{
  struct herp derp;
 
  struct herp *pderp = &derp;
 
  gderp.a = 1;
  gderp.b = 2;
 
  derp.a = 5;
  derp.b = 6;
 
  pderp->a = 6;
  pderp->b = 5;
 
  return 0;
}

Clear out the "Compiler options" field.
And check the output, each line of C code will be colored with the same color than its ASM output.

Offline neuron

  • /dev/null
  • *
  • Posts: 6
  • Cookies: 0
    • View Profile
Re: [C++] question about -> operator
« Reply #2 on: March 27, 2014, 09:23:53 pm »
Ok, I think I understand now. The pointer points to the first variable in the struct. When   -> is used then the compiler looks at the original struct and then sees how much it must increment the pointer. Then the pointer is derefrenced and the execution continues. Hopefully I'm right, but thanks anyway!

Offline bluechill

  • Cybermancer
  • Royal Highness
  • ****
  • Posts: 682
  • Cookies: 344
  • I am the existence in these walls
    • View Profile
Re: [C++] question about -> operator
« Reply #3 on: March 28, 2014, 02:44:00 pm »
That's not quite right... but I guess you could think of it that way.

http://www.cplusplus.com/doc/tutorial/pointers/
I have dreamed a dream, but now that dream has gone from me.  In its place now exists my own reality, a reality which I have created for myself by myself.

Offline neuron

  • /dev/null
  • *
  • Posts: 6
  • Cookies: 0
    • View Profile
Re: [C++] question about -> operator
« Reply #4 on: March 28, 2014, 04:37:12 pm »
@bluechill Would you recommend that tutorial over the one Kulverstukas posted? It seems to have a lot more detail. I also remember you saying  that the posted one was missing some crucial information and it seems like you know C++ pretty well so I trust your judgement. 
« Last Edit: March 28, 2014, 04:43:07 pm by neuron »

Offline bluechill

  • Cybermancer
  • Royal Highness
  • ****
  • Posts: 682
  • Cookies: 344
  • I am the existence in these walls
    • View Profile
Re: [C++] question about -> operator
« Reply #5 on: March 28, 2014, 07:11:25 pm »
@bluechill Would you recommend that tutorial over the one Kulverstukas posted? It seems to have a lot more detail. I also remember you saying  that the posted one was missing some crucial information and it seems like you know C++ pretty well so I trust your judgement. 

Kulver doesn't know C++ that well honestly and these people seem to know what they're doing but in a lot of ways it's less in depth then some tutorials... basically I'd just wait until I finish writing my C++ tutorial.
I have dreamed a dream, but now that dream has gone from me.  In its place now exists my own reality, a reality which I have created for myself by myself.