EvilZone

Programming and Scripting => C - C++ => : neuron March 27, 2014, 06:23:17 PM

: [C++] question about -> operator
: neuron 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!
: Re: [C++] question about -> operator
: ca0s March 27, 2014, 06:36:50 PM
Go
:
http://gcc.godbolt.org/
Paste
:
#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.
: Re: [C++] question about -> operator
: neuron 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!
: Re: [C++] question about -> operator
: bluechill 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/ (http://www.cplusplus.com/doc/tutorial/pointers/)
: Re: [C++] question about -> operator
: neuron 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. 
: Re: [C++] question about -> operator
: bluechill 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.