Here is the function I have, “Sprite” is an object in the program, and “GetSpriteAtPosition” just returns a pointer to the correct sprite at the coordinates.
My problem is that I store a letter in each sprite, in the form of an integer. 0 is a, and 25 is z, with everything in between respectively. I need my function to return a char* that gives me the letters of a row of sprites, so if in the program the sprites spell out “abcdefgh”, then that’s what I need this function to print out. There’s an 8×8 grid of sprites, and I’m getting the coordinates correctly, but I get an error that I can’t convert an int to a char* in the marked line. What can I do to get this to work?
Thanks in advance!
char* RowLetters(int row)
{
char* pointer;
for( int i = 0; i < 8; i++)
{
Sprite* selectedSprite = SpriteAtPosition(row*50, i * 50);
if(selectedSprite != NULL)
{
char* temp = (char)(selectedSprite->Frame() + 97); //error here
pointer = strcat(pointer, temp);
}
else
{
pointer = strcat(pointer, "test");
}
}
return pointer;
}
Try this:
I’ve changed the variable into a standard
charrather than a pointer and then passed a reference tostrcat()with the&operator.EDIT:
As pointed out in the comments, this doesn’t work because
&tempisn’t NULL terminated. I used to get around this when I programmed more C by doing the following.Of course, the
temparray could be declared outside thefor()loop for a little better performance (in theory).None of this addresses the other problems with the code like
pointernever being declared. I think a broader understanding of the calling function would be in order to determine whetherpointershould be allocated within this function or passed in by the caller.