Page 1 of 1

sprintf or string handling problems

PostPosted: Sun Jul 13, 2008 1:48 am
by feral
I am trying to create a function ( based on the getclone2() idea ) but i am having inconsistent results

the function is cloneID() and is global
Code: Select all
char *cloneID(const char *cname, int cindex)
{
char buffer[50];
sprintf(buffer,"%s.%d",cname,cindex);
return buffer;
}


the idea is to use the resulting string in any other function, not just Getclone
eg:

Code: Select all
int i;
for (i=4;i<11;i++)
{
DestroyActor(cloneID("test",i));
}


but, I think I am doing something wrong with the string types I am using or something.. the executable version appears to work OK.. but in 'Game Mode' it has varying results ( works sometimes, others not.. ) or is it just my computer :?

which means I have to compile each time I want to test it..

any ideas?
thanks

Re: sprintf or string handling problems

PostPosted: Sun Jul 13, 2008 2:05 am
by DXT
I think you just found the same problem i had with my puzzle game. something about the way ge handles the cloneindex array, that makes it unpredictable. I never figured it out though....

Cause i realized, i don't wanna program. I wanna make video games.

Re: sprintf or string handling problems

PostPosted: Sun Jul 13, 2008 3:08 am
by segwego12
U are listening to evil! DXT is trying to crush DST!

Re: sprintf or string handling problems

PostPosted: Sun Jul 13, 2008 6:28 am
by feral
thanks DST but I seemed to have solved it..

It appears the *buffer string does not like being initialized inside the function.... ( sorry if i have the wrong wording there)

this code works tho ( or seems to) , can any experienced C type person explain this ? I would rather the code was all kept within the function to make it more usable..

in the mean time I need to start a separate thread about this function in particular ( not this issue) so if you can fix the issues please post here..

new code
Code: Select all
char buffer[50];

char *cloneID(const char *cname, int cindex)
{
sprintf(buffer,"%s.%d",cname,cindex);
return buffer;
}