2

I'm writing a simple game in C. So I'm trying to save some data to a 2dim array which I malloc'd correctly. The problem is though when I want to pass that variable I wrote to, to another function.

int readMap(FILE *eingabe, map_t *map, config_t *configstruct, pacman_t *pacman)
{
  int i = 0, j = 0, k = 0, pac = 0;
  map->mapdesign = (char**) malloc(sizeof(char*) * map->height);
  do
  {
    for (i = 0; i < map->height; i++)
    {
      map->mapdesign[i] = (char*) malloc(sizeof(char) * (map->width + 1));
      for (j = 0; j < map->width; j++)
      {
        fscanf(eingabe, "%c", &map->mapdesign[i][j]);
        printf("%c", map->mapdesign[i][j]);
        if (map->mapdesign[i][j] == configstruct->ghost)
          map->ghostcount++;
        else if (map->mapdesign[i][j] == configstruct->foodtypes[0]
            || map->mapdesign[i][j] == configstruct->foodtypes[1])
        {
          map->foodcount++;
        }
        for (k = 0; k < PAC; k++)
        {
          if (map->mapdesign[i][j] == configstruct->pacman[k])
          {
            pacman->cordinate.x = j;
            pacman->cordinate.y = i;
            if (pac > 1)
              return -1;
            pac++;
          }
          k++;
        }
      }
    }
  } while (!feof(eingabe));
  return 0;
}

When I use that printf in the function itself it prints out what I want. Then I decided that I want to use a function that prints that out for me which looks like that:

int renderMap(map_t *mapstr)
{
  int i = 0;
  clrscr();
  for (i = 0; i < mapstr->height; i++)
    puts(mapstr->mapdesign[i]);
  return 0;
}

This function above should print out that:

Pastebin 1

But it actually prints this:

Pastebin 2

Thanks in advance!

mch
  • 9,424
  • 2
  • 28
  • 42
iPh1ps99
  • 97
  • 1
  • 7
  • You cannot be sure that you malloc'd the array correctly, since you don't look if the malloc call was successful. – mch Dec 05 '14 at 15:20
  • @mch I looked it up on the Debugger and it's malloc'd correctly. Since the printf right after the fscanf is working I dont know what wrong. Btw how'd i check if its malloc'd correctly? – iPh1ps99 Dec 05 '14 at 15:27
  • puts() wants strings to be null terminted. I suspect that you have '\n' at the end of each line but no '\0'. – Brad S. Dec 05 '14 at 15:29
  • @BradS. I just added an if which checks if there is an \n or \r and if yes I replace it with a \0 but that doesn't change anything. – iPh1ps99 Dec 05 '14 at 15:34
  • @BradS. I also tried it with the printf function both with chars or %s. They didn't work either – iPh1ps99 Dec 05 '14 at 15:43
  • 1
    the code needs to ALWAYS have a null terminated string. suggest clearing each malloc'd area to 0x00s. BTW: checking a malloc is: if( NULL == (map->mapdesign = malloc( ... ) ) { // then malloc failed ... } You can use calloc() instead of malloc and the areas will be cleared for you – user3629249 Dec 05 '14 at 16:03
  • are map->height and map->width set to the correct values? – user3629249 Dec 05 '14 at 16:06
  • 1
    given that the input is in a file and each line in the file is terminated with a '\n', you would be much better off to read in the file using fgets() rather than reading char by char. – user3629249 Dec 05 '14 at 16:08
  • @user3629249 So this is the updated code. Now it only prints the first line of the map: http://pastebin.com/MWDQ4br9 – iPh1ps99 Dec 05 '14 at 16:45
  • @user2233233 ok. looked at the new code in pastebin. Now, you have a for loop that iterates over the map-height rows of the file inside of an do loop that goes until eof...just a guess...but, I think you may need to remove the do-while loop. – Brad S. Dec 05 '14 at 18:51
  • @BradS.Thanks. The next problem is that it only scans half of the map... – iPh1ps99 Dec 06 '14 at 10:10
  • @user2233233 I can't really debug your whole program for you here in comments....but, here are a few more things I see that are probably bugs. 1) that single `k++;` at the bottom of the `for(k =0;....)` loop is a bug. 2) the second argument to `fgets()` should be `map->width + 1`. Finally, are you checking the value returned by the readMap function? It maybe bailing out early. – Brad S. Dec 06 '14 at 18:14
  • Please also consider adding simple printf statements to your function to see what is happening....a sort of poorman's debugger if you will. For example, you might print out `map->height` some where at the top of the function - outside of the for loops. I'm going to also suggest that the function that prints out the map might benefit from a similar treatment. Finally,. refactor this readMap function so that all it does it reads in the map file. Move that other stuff out of this function. For example, write a separate function that counts the food...and another to count the pac. – Brad S. Dec 06 '14 at 18:15
  • @BradS. Thank you! You helped me alot. I ended up debugging and saw that there was no \n there was a \r because apperently it was a Windows file. – iPh1ps99 Dec 07 '14 at 11:04

1 Answers1

1

I advice that first get the differnce betwwen the two function of printf and puts as they they treat the character termination differently. Thats may be the error.

or send the reletively complete code so that the two function can be run independently I will debug it and return back to you.

Abhijatya Singh
  • 642
  • 6
  • 23