Valhalla Legends Forums Archive | C/C++ Programming | [C]Concat "Strings"?

AuthorMessageTime
CrAzY
Hi all,

http://nopaste.gamedev.pl/?id=3727

Specifically looking at:

[code]char *show_pizza(int pizza)
  {
      char *output = (char*)malloc(100*sizeof(char));
      output[0] = '\0';
      int i;
      for(i=0; i<8; i++)
      {
        if((input[pizza]>>i)&1)
        {
            //printf("%s", *strcat("hi", "pepperoni "));
            if(i==0)
              strcat(output, "pepperoni ");
            else if(i==1)
              strcat(output, "cheese ");
            else if(i==2)
              strcat(output, "mushrooms ");
            else if(i==3)
              strcat(output, "onions ");
            else if(i==4)
              strcat(output, "green peppers ");
            else if(i==5)
              strcat(output, "olives ");
            else if(i==6)
              strcat(output, "ham ");
            else if(i==7)
              strcat(output, "sausage ");
        }
      }
   
      if(output[0]=='\0')
        strcat(output, "no toppings");
      printf("%s", output);
      return output;
  }[/code]

The strings don't concat correctly.  I either get a weird output or nothing at all.

Any suggestions?

Thanks,

Tim
May 1, 2009, 8:01 PM
Yegg
When you say they don't concat correctly, is that when you try outputting them with printf() within that function, or when you try working with what that function returns?

When you concat one of those string literals over to your output variable, the '\0' you originally set to be the first character gets overwritten.

'\0' is a null character (also plainly known as, 0) and when you call strcat() with output, the string you are appending to it overwrites any existing null character and puts a new one at the end of the returned string, and at the end of the modified output variable.

Edit: What does this "weird output" look like, if you can post it here?
May 1, 2009, 9:16 PM

Search