Home Help Search Login Register


Gamedev.org  |  Tech of game development  |  Beginners board (Moderators: mikkom, SDGamer)  |  Topic: Pointers and structures « previous next »
Pages: [1] Print
Author

Pointers and structures

 (Read 346 times)
TheDarkSide
Jr. Member
**
Posts: 36


Unleash The Force


View Profile
Pointers and structures
« on: February 03, 2010, 05:26:07 pm »

Hi, everyone. I have a question for you.
I'm quite good at playin' around with C++. But I'm doubtful about pointers, which I think it's the hardest topic about C++.
I know that pointers point to an address in memory. The address of a variable in taken thanks to the "&" symbol. I also know that the value pointed by a pointer is taken by the "*" symbol.

Let's analyse this case

Code:
void savefile(char *filename) {}
           int main(){
                 int a;
                   savefile(a);
}

In this case I understand that I assign to the VALUE OF THE POINTER the value of a
just like     *filename = a

I was just wondering why this does not work with structures

eg.
   
Code:
struct f {
         char x;
         };

void savefile(struct f *filename) {}
           int main(){
                 
                struct f a;  // I declare give A the attributes of the F structure.
                   savefile(&a);
}

Why do I have to put the REFERENCE OP.  &  when using a structure in the void parameter, and why should not, when using a variable in the void parameter?

savefile(a)  //if variable
savefile(&a) //if structure

I hope I was clear enough. Thanks
Logged
hymerman
Global Moderator
Gamedev guru
*****
Posts: 6750


booboo-be-doo


View Profile WWW
Re: Pointers and structures
« Reply #1 on: February 04, 2010, 01:09:18 pm »

I think you've mostly got the right idea, but... not quite. Your first example won't compile, as an int can't be implicitly converted to a pointer type. A pointer may be just a memory address but you won't be allowed (aside from reinterpret_cast) to convert any old number to one. Except 0 but we'll gloss over that. What would work would be taking the address of a ("&a") and passing it to savefile, since an int is convertible to a char (a char is really a numeric type, despite sounding like 'character'), savefile is expecting a pointer to one, and you're giving it a pointer. I don't think that's what you want though since the char* you want is probably an array of chars rather than a pointer to just one.

In your second example, it seems you're confused about types; "struct f ..." is a type declaration; after that, any time you write "f" that's a type. You don't need to put "struct" in front of it any more (though Visual Studio accepts this). When you write "f a;", you're creating on the stack a variable of type f, called a. It's not a pointer to an f, it's just an f (it's what's called a 'value' type). Savefile is expecting a pointer to an f, so if you call it as "savefile(a)", you're passing in a value where a pointer is expected, and it won't compile. You need the & in front of a to take its address, which you can then pass in to savefile where the pointer will have that address. Note that if savefile's parameter was "f filename" rather than "f* filename", you could pass in a as it is since it's now the correct type.

Also I don't know what you mean by "void parameter" - firstly you can't have a parameter that's void, you can have a function that takes void (i.e. takes no arguments, like your main), and secondly savefile doesn't take void.

Last thing; where you say "// if variable // if structure", they're both variables, but one is of class type (structs are class types) and one is of type int, a primitive type. Their type has nothing to do with whether you have to take their address though.

Hope that helps, I appreciate some of that's a bit confusing but then C++ is a confusing language Smiley
Logged

Anders
Sr. Member
****
Posts: 356


I'm a llama!


View Profile WWW
Re: Pointers and structures
« Reply #2 on: February 04, 2010, 10:42:17 pm »

As Hymerman wrote you just type void when you have a function that you do not want to send or return any data.

void EndGame (void);  //Dont send any data to EndGame function and nothing in return

void Test (int a);   //Send an integer we call a to Test function

int TestMore (void);   //TestMore returns an integer but doesn't get any data

The reason that you use the & in the function call is that the function savefile is expecting a pointer, not the direct value. Let's say you have a sack of gold. Sending a pointer to a function (as with the &) is like giving someone a map to where the sack of gold is. Sending the data directly to a function would be like giving someone the sack of gold directly.

Don't worry, pointers take a while to grasp but are very powerful.

You could also do like this:

Code:
f *Ptr=NULL;   //This is a pointer to the structure f
savefile (Ptr);

But please note that the Ptr pointer in the end is empty so you have to allocate memory for it first and fill it with valid data.
Logged

hymerman
Global Moderator
Gamedev guru
*****
Posts: 6750


booboo-be-doo


View Profile WWW
Re: Pointers and structures
« Reply #3 on: February 05, 2010, 11:40:12 am »

"This is a pointer to the structure f" would be better written as "This is a pointer to an instance of type f". I know that's what you meant of course, I just don't want Mr. TheDarkSide getting confused Smiley
Logged

Anders
Sr. Member
****
Posts: 356


I'm a llama!


View Profile WWW
Re: Pointers and structures
« Reply #4 on: February 05, 2010, 01:17:07 pm »

"This is a pointer to the structure f" would be better written as "This is a pointer to an instance of type f". I know that's what you meant of course, I just don't want Mr. TheDarkSide getting confused Smiley

Yep, it's good that you clarify that, thanks  Grin
Logged

Pages: [1] Print 
Gamedev.org  |  Tech of game development  |  Beginners board (Moderators: mikkom, SDGamer)  |  Topic: Pointers and structures « previous next »
Jump to:  


Login with username, password and session length

Powered by MySQL Powered by PHP Gamedev.org | Powered by SMF 1.0.7.
© 2001-2005, Lewis Media. All Rights Reserved.
Valid XHTML 1.0! Valid CSS!