sprintf or string handling problems

Non-platform specific questions.

sprintf or string handling problems

Postby feral » Sun Jul 13, 2008 1:48 am

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
User avatar
feral
 
Posts: 308
Joined: Sun Mar 16, 2008 6:27 am
Score: 47 Give a positive score

Re: sprintf or string handling problems

Postby DXT » Sun Jul 13, 2008 2:05 am

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.
Tremble in fear at my 3 kinds of ships!!!!
User avatar
DXT
 
Posts: 3
Joined: Thu Jun 05, 2008 8:55 am
Location: Omicron Persei 8
Score: 0 Give a positive score

Re: sprintf or string handling problems

Postby segwego12 » Sun Jul 13, 2008 3:08 am

U are listening to evil! DXT is trying to crush DST!
http://www.freewebs.com/primewaregames/

The Primeware Games website

Projects
Era of Rome: Done
segwego12
 
Posts: 167
Joined: Mon Jul 07, 2008 2:48 am
Location: My Website, please come to my website.
Score: 3 Give a positive score

Re: sprintf or string handling problems

Postby feral » Sun Jul 13, 2008 6:28 am

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;
}
User avatar
feral
 
Posts: 308
Joined: Sun Mar 16, 2008 6:27 am
Score: 47 Give a positive score


Return to General

Who is online

Users browsing this forum: No registered users and 1 guest

cron