FRIHOST • FORUMS • SEARCH • FAQ • TOS • BLOGS • COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


What does -> in C/Windows Programming mean?





jmi256
I've tried googling this, but I can't seem to come up with a good answer since the minus sign followed by the less-than sign seems to be ignored in the search.

But does anyone know what -> means/does when placed between two variable names? I'm working in C for this project but am more familiar with Java, but I can't recall using this before. Here is an example:

_tprintf(_T("%10d"), pFileData->nFileSizeLow);

I know this is going to print/display the value to 10 places, but what value does it print (i.e. what does -> do to pFileData and nFileSizeLow, if anything)?

BTW, if it makes any difference, pFileData is of type LPWIN32_FIND_DATA, and nFileSizeLow if one of the "elements" within the LPWIN32_FIND_DATA structure and is of type DWORD.


Thanks
thnn
pFileData will be a pointer (probably to a structure), and so -> is dereferencing the pointer, allowing you to access the element within it.

It would be equivalent to using (*pFileData).nFileSizeLow, however using -> is just easier and nicer.
loremar
The left of -> is a pointer to an object. In that case pFileData.
The right of -> is a member of that object. In that case nFileSizeLow.

An object is an instance of a class. While a member is a variable or a function that belongs to an object. A class is a set of member functions and member variables.

pFileData->nFileSizeLow means the value assigned to the member variable nFileSizeLow that belongs to the object pointed to by pFileData.

Example:
Quote:
#include <iostream>
using namespace std;
class myclass {
int i;
public:
myclass(int j) {
i = j;
}
int getInt() {
return i;
}
};
int main()
{
myclass ob(86), *objectPointer;
objectPointer = &ob; // get address of ob
cout << objectPointer->getInt(); // use -> to call getInt()
return 0;
}

source:http://www.java2s.com/Code/Cpp/Pointer/PointerstoObjects.htm

In this case myclass is a class. ob is an object, an instance to myclass with 86 assigned to j. objectPointer is a pointer to ob, meaning it is assigned with the address of ob.
objectPointer->getInt() means it will call the function getInt() that belongs to ob. In that case it will return the value of i which is equal to 86 since i=j.
Fire Boar
In addition to what others have said, note particularly that nFileSizeLow here must represent a public member variable of the object pFileData, and not a variable within the current scope. In C++, -> is like the . operator in Java (in case you are already familiar with Java).
jmi256
Fire Boar wrote:
In addition to what others have said, note particularly that nFileSizeLow here must represent a public member variable of the object pFileData, and not a variable within the current scope. In C++, -> is like the . operator in Java (in case you are already familiar with Java).


I thought that -> might be equivalent to the . in Java, but what was throwing me off was that the program does use the . operator elsewhere in the code. I’m just trying to study the code to see how it works and then make some small modifications, so I might try substituting -> with . to see what happens. I’ll let you all know.

Thanks everyone.
loremar
The difference between -> and . is that the left of -> is a pointer to an object while the left of . is an object. Meaning in order to use -> you have to get the address of that object an assign it to a pointer and use -> to access the members of that object. On the other hand, to use . you don't have to make a pointer just directly use . after the object to access its members. Why use pointers? I dunno.
Fire Boar
Yes, thnn's answer sums up the difference between -> and . quite nicely. In Java, everything is a primitive or an object reference, which is the equivalent of a pointer.

As for the value of using pointers? Memory and consistency. If you have a method that takes an object as a parameter, then change the object somehow, those changes don't reflect in the rest of the program because what actually happens is that a clone of the object is created and passed. This copy also takes up memory - quite a lot if a big object was copied.

A method which takes an object pointer as a parameter is different: only the pointer is copied. That's usually 4-8 bytes of memory, depending on whether it's a 32- or 64-bit program. Any changes to the object are made via a pointer, which may be one of many, all of which are referring to the same object and so all will see any changes made. This is similar to the behaviour of Java, and is usually the desired behaviour.

Another note: methods which take a read-only parameter should use the const keyword in its declaration. See this article on const correctness for more information.
Related topics
Reply to topic    Frihost Forum Index -> Computers -> Operating Systems

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.