Teen Programmers Unite  
 

 

Return to forum top

c++ pointers

Posted by dipsmav [send private reply] at May 27, 2002, 01:34:08 PM

Can any explain me the arrow operator in c++.thanx

Posted by Psion [send private reply] at May 27, 2002, 03:56:32 PM

a->b is the same thing as (*a).b

Posted by sphinX [send private reply] at May 27, 2002, 04:07:29 PM

Or if you would like a slightly more elaborate explaination:
If you have a pointer to a structure, you cannot access its elements with the normal dot operator, you must use the -> operator. For example:

int main() {
struct some_struct a;
struct some_struct *b;

b=&a;
a.some_element = 0; /* Correct */
a->some_element = 0; /* Incorrect, a is not a pointer */
b.some_element = 0; /* Incorrect, b is a pointer */
b->some_element = 0; /* Correct */

return 0;
}

I hope that code's correct .. I'm completely exhausted at the moment.

Posted by sphinX [send private reply] at May 27, 2002, 04:08:33 PM

Or like Psion said, if you actually reference the pointer data ie (*b), then you can use the dot operator.

Posted by ScaryGuy [send private reply] at May 29, 2002, 01:40:53 AM

A pointer is a variable that points to the address of where something is at. So if you did:

int *p;
cout << p;

it would print the address of where the value of p is, not the actual value. In order to print out the value of p, you would have to dereference it with the * operator:

cout << *p;

Now, with a struct (or class), you usually can do this:

struct
{
} yo;

yo.whateva

to get to the variable and functions inside, since yo is a variable with the actual value of the struct. If you did:

struct
{
} *ScaryGuyIsASexGod;

then you couldnt do:

ScaryGuyIsASexGod.something

cause you would be using the address of the struct, and that would just suck. So you have t dereference it:

(*ScaryGuyIsASexGod).something

But instead of doing that, do that shortcut:

ScaryGuyIsASexGod->something

which does the dereferencing for you, but is slower.

Posted by CViper [send private reply] at May 29, 2002, 04:47:59 AM

"which does the dereferencing for you, but is slower."

Uhm... actually both (should) result in the same code.

Posted by vikram_1982 [send private reply] at May 29, 2002, 05:55:58 AM

Exactly, both are the same things that are done in different ways(???), but there is no big difference in the time taken to accomplish it.

Posted by unknown_lamer [send private reply] at May 29, 2002, 08:18:08 AM

You need -> because * as a lower precendence than ..

Posted by Psion [send private reply] at May 29, 2002, 09:08:42 AM

You don't need -> at all. Like in my first reply in this thread, a->b is DEFINED AS (*a).b. They WILL produce identical code in any non-bizarre compiler.

Posted by unknown_lamer [send private reply] at May 29, 2002, 01:43:08 PM

Well, then I'll correct my response: -> exists because * has a lower precendence than ., and -> makes your code more readable than (*). so you should use it.../me wonders why the hell he still uses a language that has its origins as a set of assembler macros for an ancient PDP

Posted by ScaryGuy [send private reply] at May 29, 2002, 09:58:53 PM

Couldnt said it gooder myself, -> is more readable than (*)., just that it's slower.

Posted by gian [send private reply] at May 30, 2002, 12:52:11 AM

Why would it be slower? Are you talking about at compile or run time? Surely, if it is generating the same code, then there is no difference at runtime, and I can't imagine that parsing -> would be any more difficult than parsing (*).

Posted by unknown_lamer [send private reply] at May 30, 2002, 06:26:17 AM

There is absolutely no different between the two notations. -> is merely "syntatic sugar" for (*pointer).member...

You must be logged in to post messages and see which you have already read.

Log on
Username:
Password:
Save for later automatic logon

Register as a new user
 
Copyright TPU 2002. See the Credits and About TPU for more information.