|
|
Pages: [1]
|
 |
|
Author
|
Pointers and structures (Read 346 times)
|
|
|
hymerman
Global Moderator
Gamedev guru
    
Posts: 6750

booboo-be-doo
|
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
|
|
|
|
|
Logged
|
|
|
|
|
|
hymerman
Global Moderator
Gamedev guru
    
Posts: 6750

booboo-be-doo
|
"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
|
|
|
|
|
Logged
|
|
|
|
|
|
|
Pages: [1]
|
|
|
|
|