C question: Nested sturctures and arrays with Xcode?

Member
Posts: 254
Joined: 2005.10
Post: #16
Right, if you omit the *, you are telling C to copy the value from RLLBlock to the block member of thePiece. Your Piece_Init function passes a sprite_ptr, which is different than passing the actual sprite. In order to obtain the value of a pointer, you have to de-reference the pointer using the * operator. So in your Piece_Init function you needed piece->block = *sprite. Perhaps looking at it another way might help. An alternative way to do that would be to change your function signature and how you call your function.
Code:
void GameApp::Piece_Init(t_piece_ptr piece, sprite block)
{
    piece->block = block;
}
A couple of different ways to call the function.
Code:
t_piece_ptr piece;
sprite aSprite
sprite_ptr aSpriteReference;

Piece_Init(piece, aSprite);
Piece_Init(piece, *aSpriteReference); // Function expects a value, so we must de-reference here.
Quote this message in a reply
Apprentice
Posts: 12
Joined: 2008.01
Post: #17
Blacktiger Wrote:Right, if you omit the *, you are telling C to copy the value from RLLBlock to the block member of thePiece. Your Piece_Init function passes a sprite_ptr, which is different than passing the actual sprite. In order to obtain the value of a pointer, you have to de-reference the pointer using the * operator. So in your Piece_Init function you needed piece->block = *sprite. Perhaps looking at it another way might help. An alternative way to do that would be to change your function signature and how you call your function.
Code:
void GameApp::Piece_Init(t_piece_ptr piece, sprite block)
{
    piece->block = block;
}
A couple of different ways to call the function.
Code:
t_piece_ptr piece;
sprite aSprite
sprite_ptr aSpriteReference;

Piece_Init(piece, aSprite);
Piece_Init(piece, *aSpriteReference); // Function expects a value, so we must de-reference here.

Just leaving it blank didn't work. So I looked back and combined what worked before into one line, the (sprite_ptr)& from the function prototype, and the asterisk from the line itself, and surprisingly it worked:

piece->block = *(sprite_ptr)&RLLBlock;

Thanks for the help!
Quote this message in a reply
Member
Posts: 254
Joined: 2005.10
Post: #18
I wonder if you'd be willing to post the entire source, as that line of code is really strange (and as far as I can tell, doesn't actually do anything).
Quote this message in a reply
Moderator
Posts: 1,562
Joined: 2003.10
Post: #19
That technique may be necessary if piece->block is a different type of struct than RLLBlock. I've certainly used it more than a few times. However, is this actually the case? And if it is, are you sure it's what you really meant to do?
Quote this message in a reply
Post Reply