declaring a string

Game Editor comments and discussion.

declaring a string

Postby Fojam » Wed Dec 07, 2011 9:03 pm

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?
CLICK TO GIVE ME POINTS

My Latest Projects:
Super Smash Bros: viewtopic.php?f=6&t=12307 PLEASE help by making sprites!
User avatar
Fojam
 
Posts: 513
Joined: Thu Mar 19, 2009 10:02 pm
Location: under your bed!!!
Score: 69 Give a positive score

Re: declaring a string

Postby DarkParadox » Wed Dec 07, 2011 9:08 pm

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.
User avatar
DarkParadox
 
Posts: 457
Joined: Mon Jan 08, 2007 11:32 pm
Location: USA, Florida.
Score: 84 Give a positive score

Re: declaring a string

Postby SuperSonic » Wed Dec 07, 2011 9:16 pm

@DarkParodox: That's cool. So you can have a char that has no limit? :)
BTW: did you get the PM I sent you? =D
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1443
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Re: declaring a string

Postby skydereign » Wed Dec 07, 2011 9:21 pm

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.
User avatar
skydereign
 
Posts: 3510
Joined: Mon Jul 28, 2008 8:29 am
Score: 589 Give a positive score

Re: declaring a string

Postby SuperSonic » Wed Dec 07, 2011 9:31 pm

Thank you sky. +1 :D
edit: Just 3 more and your score will be 333 :P
A tree never hits an automobile except in self-defence.

Want to use your joystick or controller with Game Editor? Check out my controller engine =D
User avatar
SuperSonic
 
Posts: 1443
Joined: Fri Sep 24, 2010 9:24 pm
Location: Anywhere
Score: 72 Give a positive score

Re: declaring a string

Postby Fojam » Wed Dec 07, 2011 10:00 pm

the array one seemed to work thank you for your reply and +1
CLICK TO GIVE ME POINTS

My Latest Projects:
Super Smash Bros: viewtopic.php?f=6&t=12307 PLEASE help by making sprites!
User avatar
Fojam
 
Posts: 513
Joined: Thu Mar 19, 2009 10:02 pm
Location: under your bed!!!
Score: 69 Give a positive score


Return to GE - General

Who is online

Users browsing this forum: No registered users and 1 guest