Ich habe versucht herauszufinden, was mit meinem Code falsch ist. Ich glaube, dass ich den Schuldigen gefunden habe. es ist dieser Teil des Codes hier, dass esWie löse ich diesen Segmentierungsfehler?
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);
}
}
}
}
}
von dem, was ich weiß, Segmentierungsfehler bedeutet ein Speicherproblem verursacht. Ich habe dafür gesorgt, dass es genug Platz für alles gibt. Was geht eigentlich vor?
Mögliche Duplikate von [Wie Segmentierungsfehler zu debuggen?] (Http://stackoverflow.com/questions/29324796/how-to-debug-segmentierung-fault) – OrangeDog
Ähm, Sie haben noch keinen Speicher zugeordnet. 'firstInput' ist immer noch 0. –
Welche Zeile segfault? Was ist das Backtrace? – fluter