Page 1 of 1

declaring a string

PostPosted: Wed Dec 07, 2011 9:03 pm
by Fojam
i feel like im declaring a string wrong, because when i run this function, my game freezes and crashes. Here is the code for the function i made.

Code: Select all
void p1AnimFinish(int characterNo, int dir, int attackNo)
{
    //dir=1 is left, dir=2 is right
    //attackNo is whatever the attacks is in the attacks array
    char charName;
    char animCharName;
    char stand;
    char crouch;
    char fall;
    switch(characterNo)
    {
        case 1:
        sprintf(charName, "mario");
        sprintf(animCharName, "mario");
        break;
    }
    switch(dir)
    {
        case 1:
        switch(p1OnGround)
        {
            case 1:
            switch(p1Down)
            {
                case 0:
                sprintf(stand, "%s_stand_left", animCharName);
                ChangeAnimation(charName, stand, NO_CHANGE);
                break;
 

               case 1:
                sprintf(crouch, "%s_crouch_left", animCharName);
                ChangeAnimation(charName, crouch, NO_CHANGE);
                break;
            }
            break;
 
            case 0:
            sprintf(fall, "%s_fall_left", animCharName);
            ChangeAnimation(charName, fall, NO_CHANGE);
            break;
        }
        break;
 
        case 2:
        switch(p1OnGround)
        {
            case 1:
            switch(p1Down)
            {
                case 0:
                sprintf(stand, "%s_stand_right", animCharName);
                ChangeAnimation(charName, stand, NO_CHANGE);
                break;
 

               case 1:
                sprintf(crouch, "%s_crouch_right", animCharName);
                ChangeAnimation(charName, crouch, NO_CHANGE);
                break;
            }
            break;
 
            case 0:
            sprintf(fall, "%s_fall_right", animCharName);
            ChangeAnimation(charName, fall, NO_CHANGE);
            break;
        }
        break;
 
    }
    canDo=1;
    attacks[attackNo]=0;
}


Im declaring a string like this:

Code: Select all
char stringName;


what is the correct way to do it?

Re: declaring a string

PostPosted: Wed Dec 07, 2011 9:08 pm
by DarkParadox
When you're using char, you need to either,
  • Declare how many letters maximum;
    Code: Select all
    char stringName[256];
  • Or declare a "infinite" one;
    Code: Select all
    char*stringName;
I suggest the second one.

Re: declaring a string

PostPosted: Wed Dec 07, 2011 9:16 pm
by SuperSonic
@DarkParodox: That's cool. So you can have a char that has no limit? :)
BTW: did you get the PM I sent you? =D

Re: declaring a string

PostPosted: Wed Dec 07, 2011 9:21 pm
by skydereign
A char* will allow you to allocate the amount of space the string will take, but you have to allocate it first. So, while it allows you to choose the size when you use it, the array one still has the same size limit (as in whatever number you choose). So unless you are going to use realloc on the char*, I wouldn't suggest using it. char arrays are more likely to not get misused than char*s. Mostly because there is a known limit to its size. Though, if you are already familiar with memory allocation, then using char* can be more efficient, and you shouldn't have any memory leaks.

Re: declaring a string

PostPosted: Wed Dec 07, 2011 9:31 pm
by SuperSonic
Thank you sky. +1 :D
edit: Just 3 more and your score will be 333 :P

Re: declaring a string

PostPosted: Wed Dec 07, 2011 10:00 pm
by Fojam
the array one seemed to work thank you for your reply and +1