OPs question is answered, so I suppose I can proceed to constructively derail this thread.
-> is a dereference operator, because the variable
ptr is a pointer type pointing to a struct.
In order to explain what -> means, I must first explain what pointers are. If you already think you're familiar, skip ahead to the next book icon.
Introduction to Pointers
C comes with fundamental datatypes. void, char, short, long, int, float, double, and
pointers. That's basically it.
Consider the following:
What this is telling the computer to do is: "Give me 16 bits of memory and initialise it to 42". For the sake of example, lets say the computer decided to store those 16 bits at address 0x400120. Why 16 bits? Because sizeof(short)
is at least 2 bytes = 16 bits.
Thus, if we were to look at the raw memory, it would be something like this:
0000'0000'0010'1010....
^ ^
| |
0x400120 0x400122
The bit sequence representing the number 42 begins at address 0x400120 and ends 16 bits later (assuming this is a byte addressable memory map - all x86 based computers are). The variable
thing is an
identifier - it literally represents the 16 bits, you can think of it as an alias.
In C/C++, it is possible to obtain the address of any known identifier. This is done with an ampersand:
ptr, in this example, would be 0x400120 because that's where the 16 bits begin in memory.
If you have a pointer and wish to know what it's pointing at, you have to
dereference it. This is done with the dereference operator:
This tells the computer: "Go fetch me the memory located at whatever ptr is pointing to." You can then assign it to something:
short another_thing = *ptr;
another_thing would now be 42.
You can also assign something to it:
This instructs the computer to overwrite the 16 bits with a new number, 52.
Now, the important bit. It took me months before I realized this simple fact. You will notice I deliberately omitted the declaration type of
ptr. I did this to demonstrate the problem with dereferencing something: How does the computer know how long the sequence of bits being pointed to is? In this case we know it's 16 bits because it's a short, but what if it were a long? How would the computer know that it has to fetch 32 bits and not 16? Or what if it's a type way more complicated than that, like a struct?
The computer can't know. That's why we have to tell the computer what type a particular pointer is pointing at:
This tells the computer: "
ptr will be pointing at a sequence of bits representing a short". If it were a long instead of a short, we would write long* ptr;
Structs
Plain datatypes are not the only datatypes that can be created. Structs allow you to create new datatypes out of combinations of plain datatypes. Consider the following:
struct my_struct_t
{
int x;
float y;
};
/* later on... */
struct my_struct_t my_struct = {2, 3.14159};
Just as with the very first example, this tells the computer: "Give me sizeof(my_struct_t) bytes of memory and initialise x with 2 and y with 3.14159.
The difference now is,
my_struct has
members, namely
x and
y, which can be accessed with the "." operator (much like in DBP):
my_struct.x = 23;
my_struct.y = 2.7172;
And just like you can get the address to plain datatypes, you can also get the address of a struct. Nothing changes with the syntax, only the name of the datatype changes:
struct my_struct_t* ptr = &my_struct;
ptr now holds the memory location of wherever
my_struct is stored.
If you've followed me this far, then you will probably understand what this means:
struct my_struct_t copy = *ptr;
This will dereference the memory location pointed to by
ptr (which is of type my_struct_t), fetch the contents, and copy them into a new struct called
copy. The members can then be accessed normally with copy.x and copy.y.
The next logical step is: What if I wanted to access the members directly from a pointer? Well, you can! You'd just replace "copy" with "*ptr":
(*ptr).x = 36;
(*ptr).y = 5.3234;
This would re-write the contents of the original struct,
my_struct, with x=36 and y=5.3234, since
ptr is pointing to the memory location of
my_struct.
C programmers decided that writing (*ptr).x looks ugly. Therefore, they came up with a better way:
ptr->x = 36; /* same as writing (*ptr).x = 36; */
ptr->y = 5.3234; /* same as writing (*ptr).y = 5.3234; */
Back to the original question
Quote: "That being the case, what's the point (

) of the following line:"
if ( strlen(ptr->szShortFilename)==iFindFilenameLength )
ptr is of type
tagData*, which is a struct and contains a member named
szShortFilename.
szShortFilename is of type
char* and points to a string.
strlen() will therefore evaluate the length (number of characters) of the string stored in the member
szShortFilename of
ptr, then compare it to
iFindFilenameLength.
Why? No idea, strcmp() does that check already anyway.
I like offending people. People who get offended should be offended. --
Linus Torvalds
Why is my sig being erased? It wasn't over 600x120 even with the image.