-2

i have been trying to figure out what is wrong with my piece of code. So taking in and out of my code everywhere, i think i have found the culprit. it is this chunk of code here that is causing it

   if (strcmp(input, "load 1") != 0 || strcmp(input, "load 2") != 0 || strcmp(input, "quit") != 0)
    {
        Player player;
        Position position;
        Direction direction;

        char* tok;
        char* firstInput=0;
        char* secondInput=0;
        char* thirdInput=0;
        char* fourthInput=0;
        int x;
        int y;
        char str[10];
        char str2[10];


        tok = strtok(input, " ");
        strcpy(firstInput, tok);            
        tok = strtok(NULL, ", ");
        strcpy(secondInput, tok);          
        tok = strtok(NULL, ", ");
        strcpy(thirdInput, tok);          
        tok = strtok(NULL, "");
        strcpy(fourthInput, tok);            

        strcpy(str, secondInput);
        x = atoi(str);
        strcpy(str2, thirdInput);
        y = atoi(str2);

        if(strcmp(firstInput, "init") == 0 )
        {
            if(x >= 0 && x <= 9)
            {
                if(y >= 0 && y <= 9)
                {
                    if (strcmp(fourthInput,"north") == 0 || (strcmp(fourthInput,"south")) || (strcmp(fourthInput,"west") == 0) || (strcmp(fourthInput,"east") == 0))
                    {
                        position.x = x;
                        position.y = y;

                        if(strcmp(fourthInput,"north") == 0)
                        {
                            direction = NORTH;
                        }
                        if(strcmp(fourthInput,"south") == 0)
                        {
                            direction = SOUTH;
                        }
                        if(strcmp(fourthInput,"west") == 0)
                        {
                            direction = WEST;
                        }
                        if(strcmp(fourthInput,"east") == 0)
                        {
                            direction = EAST;
                        }
                        initialisePlayer(&player, &position, direction);
                        placePlayer(board, position);
                        displayDirection(direction);
                    }
                }
            }
        }                      
    }

from what i know segmentation fault means a memory problem. I have made sure there is enough space for everything. what is actually going on?

2 Answers2

1

As mentioned in the comments, firstInput and its siblings don't have any memory allocated to the where you could strcpy to. They are NULL, which leads to a segmentation fault.

You could allocate memory or make these variables local arrays, but you don't really need that. strtok returns pointers into input. These are valid as long as input isn't overwritten. You use these pointers only to analyse the current line before reading the next line into input, so that's okay.

Get rid of the intermediary tok and store the result of the tokens directly:

    char* firstInput = NULL;
    char* secondInput = NULL;

    firstInput = strtok(input, " ");
    if (firstInput) secondInput = strtok(NULL, ", ");

    // analyse input

When dealing with pointers, make sure to test for NULL before accessing them. There is no guarantee that user input contains exactly four tokens (or any tokens at all).

M Oehm
  • 28,726
  • 3
  • 31
  • 42
0

You could start from here:

char* firstInput=0;
...
strcpy(firstInput, tok);

where firstInput is still NULL, and no memory is allocated for it, but you are trying to copy tok to it.

Use malloc() to allocate memory, like this:

firstInput = malloc(sizeof(char) * (strlen(tok) + 1));
if (firstInput==NULL)
  exit (1);

This should get you started with your debugging. :)

gsamaras
  • 71,951
  • 46
  • 188
  • 305