Menu

HELP (NEWBIE) CONVERT CHAR TO CHAR* ERROR

Anonymous
2007-12-05
2012-09-26
  • Anonymous

    Anonymous - 2007-12-05

    I'm trying to create my first game using DEV-C++ I'm getting the error (invalid conversion from char' tochar*') I tried many things but it continue with the same error.

    Here is the part of the code:

    THESE BELOW ARE THE GLOBAL VARIABLES:

    struct mappointlabel
    { char mappoint;
    int color;
    };
    mappointlabel maplab[22][80];

    typedef struct mapslist
    { mappointlabel maplab[22][80];
    char mapname[20];
    int mapnumber;
    mapslist next;
    }maps;
    maps novoa,
    nnew, first, last;

    AND HERE IS THE PROCEDURE WITH ERROR IN THE LAST LINE (IN STRCPY):

    void allocatemap(int)
    { nnew=(maps *)malloc(sizeof(maps));
    if (nnew == NULL)
    { printf("\n Insulficient memory");
    exit(1);
    }
    first=nnew;
    last=nnew;
    nnew->mapnumber=1;
    strcpy((nnew->mapname), novoa.mapname);
    for (i=0; i<80; i++)
    for (j=0; j<22; j++)
    strcpy((nnew->maplab[i][j].mappoint), (maplab[i][j].mappoint));
    }

     
    • Anonymous

      Anonymous - 2007-12-05

      YES. You're right. I made a mistake. I spent a long time to discover it. I inverted these 2 values.

      THANK YOU

      I need to pay more attention in what I'm doing. :p

       
    • Osito

      Osito - 2007-12-05

      mappoint is a char, not char*. you can't use strcpy on a single char like that - it takes pointer to char. If all you're trying to do is copy one char at a time, use:

      nnew->maplab[i][j].mappoint = maplab[i][j].mappoint;

      If you wanted those to be strings instead of single characters, your definition in the first struct is wrong.

       
    • Anonymous

      Anonymous - 2007-12-05

      I have tried that before, the program is compiled but when I run this procedure the program crash.

      I don't know why this is happing. :(

       
      • Anonymous

        Anonymous - 2007-12-05

        Well you need to determine where it is crashing and why. Since this is not the complete program, it is impossible to say why. As Osito has pointed out, you either intended mappoint to be a string or a character. If you intended it to be a character, while Osito's fix would allow it to compile, if you elsewhere treated it as a pointer it is entirely likely to fail. Replacing something that crashes with something that does not even compile is not progress! I suggest that you ask us to help with the code that crashes, not the code that fails to compile. The crash is almost certainly not occurring in the code you posted (even if it is related to the cause).

        If you made mappoint a char* but never set it to point to a valid memory location if would also fail.

        You need to post far more information if you want further help. "I tried that it did not work" is not really very helpful. Show what your tried, explain how it did not work. Consider using the debugger to locate the problem.

        Remember a char will hold a single 8 bit integer. A char points to such an integer, exactly whether that pointer refers to a single character, an array of characters, or a null-terminated string is entirely dependent on context, if you use it in the 'wrong', way failure is likely. Be aware also that a char declaration does not reserve any space for what it might refer to - creating that space is your responsibility. You have not provided enough information to indicate what you are doing, and have not clarified what you expect mmappoint to hold; is it supposed to be a string or a small integer or character?

        Clifford

         
    • Osito

      Osito - 2007-12-05

      My guess would be the crash is a different issue. Perhaps this line:

      strcpy((nnew->mapname), novoa.mapname);

      You haven't posted all your code, so I can't tell if you've initialized novoa.mapname or not.

       
    • Anonymous

      Anonymous - 2007-12-05

      I also tried to delete all other lines and maintain only the last line and the proplem is the same. The last line.

      The code is big:
      more than 1400 lines.

      novoa is a maps variable (struct).

      typedef struct mapslist
      { mappointlabel maplab[22][80];
      char mapname[20];
      int mapnumber;
      mapslist next;
      }maps;
      maps (----HERE---->) novoa,
      nnew, first, last;

       
      • Osito

        Osito - 2007-12-05

        You changed the last line to what I suggested and it crashed? Maybe I didn't look at it right.

        The problem is not where you declare novoa. It's that if you don't initialize its mapname to something valid before using it in strcpy, it will contain garbage and could easily cause a crash.

        Somewhere before doing the strcpy that uses novoa.mapname, at least do strcpy(novoa.mapname,"Uninitialized"); or something like that. Alternately, novoa.mapname[0]=0; to make it empty.

         
    • Osito

      Osito - 2007-12-05

      Yeah, I didn't look close enough. Your last line would cause a crash too. You would need to swap i and j:

      nnew->maplab[j][i].mappoint = maplab[j][i].mappoint;

      You have i going to 80 and j to 22, but you have 22 and 80 in your declaration. Actually, it would be better to change your for loops, and have i go to 22 and j go to 80, so that you still have:

      nnew->maplab[i][j].mappoint = maplab[i][j].mappoint;

       
    • Anonymous

      Anonymous - 2007-12-05

      I think I discovered something, if I remove the 2 fors for (i=0; i<80; i++) and for (j=0; j<22; j++) the thing work, the problem is not in novoa couse this part of code before the allocatemap:

      void savemap(int)
      { if (mapopen==0)
      { centralize(10, "THERE'S NO MAP OPEN TO SAVE");
      centralize(12, "PRESS ANY KEY TO RETURN");
      getch();
      centralize(10, " ");
      centralize(12, " ");
      }
      else
      { system("cls");
      centralize(10, "WRITE THE NAME OF YOUR MAP");
      centralize(11, "REMENBER TO USE ONLY NUNBERS AND LETERS\n\n");
      gets(novoa.mapname);
      allocatemap(1);
      system("cls");
      centralize(10, "FILE SAVED SUCESSFULLY");
      centralize(12, "PRESS ANY KEY TO CONTINUE");
      getch();
      mapeditor(1);
      }
      }

      So the problem must have relation with the fors.

      THIS WAY WORKS:

      void allocatemap(int)
      { nnew=(maps *)malloc(sizeof(maps));
      if (nnew == NULL)
      { printf("\n Insulficient memory");
      exit(1);
      }
      first=nnew;
      last=nnew;
      nnew->mapnumber=1;
      strcpy((nnew->mapname), novoa.mapname);
      nnew->maplab[i][j].mappoint = maplab[i][j].mappoint;
      }

      THIS WAY NOT:

      void allocatemap(int)
      { nnew=(maps *)malloc(sizeof(maps));
      if (nnew == NULL)
      { printf("\n Insulficient memory");
      exit(1);
      }
      first=nnew;
      last=nnew;
      nnew->mapnumber=1;
      strcpy((nnew->mapname), novoa.mapname);
      for (i=0; i<80; i++)
      for (j=0; j<22; j++)
      nnew->maplab[i][j].mappoint = maplab[i][j].mappoint;
      }

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.