-2

I have tried a few different things to get this to work but basically I have a name that is 6 letters long (maximum) and I need to input it into the char* car in place of the '-'. For example if the name was Bob the car should look like ~O=Bob----o>

typedef struct Racer_S {

    int row;       ///< vertical row or "racing lane" of a racer

    int distance;  ///< column of rear of car, marking its position in race

    char *graphic; ///< graphic is the drawable text of the racer figure

} Racer;

Racer * make_racer( char *name, int row ){
        Racer *newRacer;
        char *car = "~O=-------o>";
        for (int i = 0; i < strlen(name); ++i)
            car[i+3] = name[i];
    //    printf("%s\n",car);
    //    newRacer->row = row;
    //    newRacer->distance = 0;
    //    newRacer->graphic = car;
        return newRacer;
    }
Jak
  • 57
  • 2
  • 9
  • What exactly is `Racer`? Is it something you have control over? – Mad Physicist Nov 13 '18 at 03:03
  • 1
    On many platforms, you won't be able to modify that buffer. Even if you were able to, you A) discard it, and B) it's on the stack. – Mad Physicist Nov 13 '18 at 03:04
  • https://www.geeksforgeeks.org/strstr-in-ccpp/ – Bwebb Nov 13 '18 at 03:05
  • Is Racer a pointer to a char *? Please provide the definiton – Hogstrom Nov 13 '18 at 03:09
  • Side-note: Relying on the compiler to compute `strlen` only once is not a great idea. I'd strongly recommend computing `strlen(name)` exactly once *outside* the loop, storing it to a variable, then using that for the loop conditional test, rather than making your loop potentially `O(n**2)` by repeating the `strlen` test `strlen` times. – ShadowRanger Nov 13 '18 at 04:05
  • don't put `strlen(name)` in the loop, as it'll be called unnecessarily again and again – phuclv Nov 13 '18 at 04:05

2 Answers2

1

car points to a string constant. These are read only, so you can't modify them.

Instead, allocate memory for a string, then modify that:

char *car = strdup("~O=-------o>");

You also need to allocate memory for newRacer:

Racer *newRacer = malloc(sizeof(*newRacer));

Don't forget to check the return value of these functions in case they fail.

dbush
  • 205,898
  • 23
  • 218
  • 273
  • This has been answered multiple times before. Instead of posting yet another answer, you should go to the [C wiki FAQ](https://stackoverflow.com/tags/c/info), pick a canonical duplicate from the list, section "strings", and close the question. – Lundin Nov 13 '18 at 11:48
  • That being said, teaching newbies to use `strdup` without a disclaimer that it's non-portable isn't a good idea. – Lundin Nov 13 '18 at 11:49
0

The issue lies in the way in which you declare car. When defined as a char* with a string literal, what's really happening is you're storing a reference to a string in an area of read-only memory. To circumvent this, you simply need to declare your variable as a char[], like so:

char car[] = "~O=-------o>";

This is basically syntactic sugar for the following, equivalent code:

char car[] = { '~', 'O', '=', '-', '-', '-', '-', '-', '-', '-', 'o', '>', '\0' };
kamoroso94
  • 1,713
  • 1
  • 16
  • 19