2016-12-07 3 views
-4

Ich habe debugged und beobachtete mein "Watch Window", um eine Idee zu bekommen, wo das Problem war. Es wird jedoch nicht einmal der Code beendet. Während der Testsequenz, um das vom Benutzer eingegebene Wort zu finden, schlägt es fehl. Es hat einen SIGSEGV-Fehler bei der gefundenen Funktion.Struct/Malloc oder andere? SIGSEGV Fehler

Der Code soll ein Wort Puzzle übernehmen und dann dem Benutzer erlauben, die Wörter zu finden.

puzzleInput.txt:

M,N,O,S,L,I,W,E,R,E,L,Y,T,L,E,A,G,N 
A,H,O,O,V,E,R,T,A,Y,L,O,R,V,E,N,N,A 
D,F,D,R,O,O,S,E,V,E,L,T,O,N,O,M,I,M 
I,N,T,P,M,H,I,E,G,D,I,L,O,O,C,O,D,U 
S,O,N,L,I,J,Q,A,D,A,M,S,S,R,N,N,R,R 
O,X,L,O,G,E,F,F,M,O,I,R,E,E,G,R,A,T 
N,I,B,T,S,O,R,A,O,R,M,O,V,A,W,O,H,N 
F,N,H,U,R,R,B,C,R,N,L,R,E,G,B,E,W,E 
R,O,S,D,C,O,E,A,E,I,O,N,L,A,U,A,J,R 
K,W,U,N,L,H,H,F,N,I,O,S,T,N,S,W,A,U 
E,R,B,A,J,B,A,C,F,S,S,M,N,H,H,R,D,B 
N,E,W,L,O,T,O,N,K,E,C,E,I,H,T,H,A,N 
N,T,H,E,H,L,A,C,A,K,J,N,N,H,O,I,M,A 
E,R,G,V,N,C,A,F,I,N,G,H,U,H,A,J,S,V 
D,A,R,E,S,J,C,N,T,T,A,R,N,B,O,Y,A,E 
Y,C,A,L,O,D,L,N,O,S,I,R,R,A,H,W,E,O 
N,E,N,C,N,E,T,N,N,O,T,N,I,L,C,O,E,S 
D,A,T,Y,Y,P,O,L,K,G,A,R,F,I,E,L,D,R 

enter code here 

Code:

/* This program reads a puzzle from a file 
    and allows the user to define a word which 
    they want to find in the word puzzle.*/ 

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <ctype.h> 

void fileNam(char[]); 
char showMenu(); 
int fileData(char*); 
void alphaArray(); 
void getUser(); 
void fileError(char*); 
void findUserFirst(); 
int createString(); 
void found(int); 
void notFound(char*); 
void goToExit(); 
void display(); 

struct alphabet 
{ 
     int *A;int *B; int *C; int *D; int *E; int *F; 
     int *G; int *H; int *I; int *J; int *K; int *L; int *M; 
     int *N; int *O; int *P; int *Q; int *R; int *S; 
     int *T; int *U; int *V; int *W; int *X; int *Y; int *Z; 
     int a; int b; int c; int d; int e; int f; int g; int h; int i; int j; int k; int l; int m; 
     int n; int o; int p; int q; int r; int s; int t; int u; int v; int w; int x; int y; int z; 
}fill; 

struct user 
{ 
    struct alphabet* ptr; 
    int *attempt; 
    int testNum; 
    int columns; 
    int rows; 
    int userSize; 
    int puzzleSize; 
    char *puzzleArray; 
    char userWord[45]; 
}test; 


int main() 
{ 
    int wordsFound = 0; 
    int fileErr = 0; 
    int wordF = 0; 
    char option; 
    char fileLoc[100] = {0}; 

    do 
    { 
     fflush(stdin); 
     option = showMenu(); 
     fflush(stdin); 

     if(fileErr == -1) 
     { 
      fileError(fileLoc); 
     } 

     switch(option) 
     { 
      case 'a':; 
       fileNam(fileLoc); 
       strcat(fileLoc,"\\puzzleInput.txt"); 
       fileErr = fileData(fileLoc); 
       break; 
      case 'b': 
       display(); 
       break; 
      case 'c': 
       getUser(); 
       findUserFirst(); 
       wordF = 0; 
       do{ 
        wordF = createString(); 
        if(wordF > 0) 
        { 
         found(wordF); 
         wordsFound++; 
        } 
        else 
        { 
         test.ptr; 
        } 
        }while(*test.attempt <= test.testNum); 
        if(wordF < -1) 
         notFound(fileLoc); 
       break; 
      case 'd': 
       goToExit(wordsFound); 
       break; 
      default: 
       printf("Invalid selection.\nPlease choose again.\n"); 
       break; 
     } 
    }while(option != 'd'); 


    return 0; 
} 

char showMenu() 
{ 
    char option; 
    printf("Choose one of the following:\n"); 
    printf("\ta. Enter file location(full path)\n"); 
    printf("\tb. Display the puzzle\n"); 
    printf("\tc. Find a word\n"); 
    printf("\td. Exit\n"); 
    option = getchar(); 
    return option; 
} 

void goToExit(int found) 
{ 
    printf("\nYou found %d words!\n", found); 
    if(found > 5) 
    { 
     printf("Awesome job! Come back to play again!"); 
    } 
    else{ 
     printf("Better luck next time!"); 
    } 
    printf("Press any key to close window."); 
    getchar(); 
    free(fill.A);free(fill.B); free(fill.C); free(fill.D); free(fill.E); free(fill.F); 
    free(fill.G); free(fill.H); free(fill.I); free(fill.J); free(fill.K); free(fill.L); free(fill.M); 
    free(fill.N); free(fill.O); free(fill.P); free(fill.Q); free(fill.R); free(fill.S); 
    free(fill.T); free(fill.U); free(fill.V); free(fill.W); free(fill.X); free(fill.Y); free(fill.Z); 
    free(test.puzzleArray); 
} 

void display() 
{ 
    int i, j, k = 0; 
    for(i=0; i <= test.columns; i++) 
    { 
     for(j=0; j <= test.rows; j++) 
     { 
      printf("%c, ", test.puzzleArray[k]); 
      k++; 
     } 
     printf("\n"); 
    } 

} 


void fileNam(char fileLoc[]) 
{ 
    printf("When entering file location, do not \n"); 
    printf("include the file name.\nLOCATION:\n"); 
    gets(fileLoc); 
} 

void fileError(char* fileName) 
{ 
    printf("There was an error with the file location"); 
    printf(" that you typed in.\nPlease make sure file "); 
    printf("location and name are correct.\nIf name is wrong, "); 
    printf("please change the name of your file to match.\n"); 
    printf("FILE LOCATION AND NAME: \"%s\"\n\n", fileName); 
} 

int fileData(char* fname) 
{ 
    int i = 0; 
    char curr; 
    FILE * fPtr; 
    fPtr = fopen(fname, "r"); 

    char buffer[1000] = {0}; 

    if(fPtr == NULL)//test if file opened 
    { 
     printf("There has been an error in opening %s!\n", fname); 
     return -1; 
    } 

    while (!feof(fPtr)) 
    { 
     fgets(buffer, 1000, fPtr); 
     test.rows++; 
    } 

    rewind(fPtr); 

    while(fgetc(fPtr) >= 65 && fgetc(fPtr) <= 90) 
    { 
     test.puzzleSize++; 
    } 

    test.puzzleArray = (char*) malloc((test.puzzleSize) * sizeof(char)); 
    printf("%d", test.puzzleSize); 
    rewind(fPtr); 
    i = 0; 
    while ((curr = fgetc(fPtr)) != EOF) 
    { 
     if(curr == ',' || curr == '\n') 
     { 
      if(curr == '\n') 
       test.columns++; 
     } 
     if(curr >= 65 && curr <= 90) 
     { 
      test.puzzleArray[i] = curr; 
      i++; 
     } 

    } 

    fclose(fPtr); 
    alphaArray(); 
    return 0; 
} 

void alphaArray() 
{ 
    int i = 0; 
    char current = 0; 


    while(test.puzzleArray[i] != '\0') 
    { 
     current = test.puzzleArray[i]; 
     switch(tolower(current))//accidentally coded lowercase, quick fix 
     { 
     case 'a': 
      fill.a++; 
      break; 
     case 'b': 
      fill.b++; 
      break; 
     case 'c': 
      fill.c++; 
      break; 
     case 'd': 
      fill.d++; 
      break; 
     case 'e': 
      fill.e++; 
      break; 
     case 'f': 
      fill.f++; 
      break; 
     case 'g': 
      fill.g++; 
      break; 
     case 'h': 
      fill.h++; 
      break; 
     case 'i': 
      fill.i++; 
      break; 
     case 'j': 
      fill.j++; 
      break; 
     case 'k': 
      fill.k++; 
      break; 
     case 'l': 
      fill.l++; 
      break; 
     case 'm': 
      fill.m++; 
      break; 
     case 'n': 
      fill.n++; 
      break; 
     case 'o': 
      fill.o++; 
      break; 
     case 'p': 
      fill.p++; 
      break; 
     case 'q': 
      fill.q++; 
      break; 
     case 'r': 
      fill.r++; 
      break; 
     case 's': 
      fill.s++; 
      break; 
     case 't': 
      fill.t++; 
      break; 
     case 'u': 
      fill.u++; 
      break; 
     case 'v': 
      fill.v++; 
      break; 
     case 'w': 
      fill.w++; 
      break; 
     case 'x': 
      fill.x++; 
      break; 
     case 'y': 
      fill.y++; 
      break; 
     case 'z': 
      fill.z++; 
      break; 
     default: printf("\n"); 
     break; 
     } 
    i++; 
    } 

    fill.A = (int *)malloc(sizeof(int)*fill.a); fill.B = (int *)malloc(sizeof(int)*fill.b); 
    fill.C = (int *)malloc(sizeof(int)*fill.c); fill.D = (int *)malloc(sizeof(int)*fill.d); 
    fill.E = (int *)malloc(sizeof(int)*fill.e); fill.F = (int *)malloc(sizeof(int)*fill.f); 
    fill.G = (int *)malloc(sizeof(int)*fill.g); fill.H = (int *)malloc(sizeof(int)*fill.h); 
    fill.I = (int *)malloc(sizeof(int)*fill.i); fill.J = (int *)malloc(sizeof(int)*fill.j); 
    fill.K = (int *)malloc(sizeof(int)*fill.k); fill.L = (int *)malloc(sizeof(int)*fill.l); 
    fill.M = (int *)malloc(sizeof(int)*fill.m); fill.N = (int *)malloc(sizeof(int)*fill.n); 
    fill.O = (int *)malloc(sizeof(int)*fill.o); fill.P = (int *)malloc(sizeof(int)*fill.p); 
    fill.Q = (int *)malloc(sizeof(int)*fill.q); fill.R = (int *)malloc(sizeof(int)*fill.r); 
    fill.S = (int *)malloc(sizeof(int)*fill.s); fill.T = (int *)malloc(sizeof(int)*fill.t); 
    fill.U = (int *)malloc(sizeof(int)*fill.u); fill.V = (int *)malloc(sizeof(int)*fill.v); 
    fill.W = (int *)malloc(sizeof(int)*fill.w); fill.X = (int *)malloc(sizeof(int)*fill.x); 
    fill.Y = (int *)malloc(sizeof(int)*fill.y); fill.Z = (int *)malloc(sizeof(int)*fill.z); 

    fill.a = 0;fill.b =0; fill.c =0; fill.d =0; fill.e =0; fill.f =0; 
    fill.g =0; fill.k =0; fill.o =0; fill.s =0; fill.w =0; 
    fill.h =0; fill.l =0; fill.p =0; fill.t =0; fill.x =0; 
    fill.i =0; fill.m =0; fill.q =0; fill.u =0; fill.y =0; 
    fill.j =0; fill.n =0; fill.r =0; fill.v =0; fill.z =0; 
    i = 0; 

    while(test.puzzleArray[i] != '\0') 
    { 
     current = test.puzzleArray[i]; 
     switch(tolower(current))//accidentally coded lowercase, quick fix 
     { 
     case 'a': 
      fill.A[fill.a] = i; 
      fill.a++; 
      break; 
     case 'b': 
      fill.B[fill.b] = i; 
      fill.b++; 
      break; 
     case 'c': 
      fill.C[fill.c] = i; 
      fill.c++; 
      break; 
     case 'd': 
      fill.D[fill.d] = i; 
      fill.d++; 
      break; 
     case 'e': 
      fill.E[fill.e] = i; 
      fill.e++; 
      break; 
     case 'f': 
      fill.F[fill.f] = i; 
      fill.f++; 
      break; 
     case 'g': 
      fill.G[fill.g] = i; 
      fill.g++; 
      break; 
     case 'h': 
      fill.H[fill.h] = i; 
      fill.h++; 
      break; 
     case 'i': 
      fill.I[fill.i] = i; 
      fill.i++; 
      break; 
     case 'j': 
      fill.J[fill.j] = i; 
      fill.j++; 
      break; 
     case 'k': 
      fill.K[fill.k] = i; 
      fill.k++; 
      break; 
     case 'l': 
      fill.L[fill.l] = i; 
      fill.l++; 
      break; 
     case 'm': 
      fill.M[fill.m] = i; 
      fill.m++; 
      break; 
     case 'n': 
      fill.N[fill.n] = i; 
      fill.n++; 
      break; 
     case 'o': 
      fill.O[fill.o] = i; 
      fill.o++; 
      break; 
     case 'p': 
      fill.P[fill.p] = i; 
      fill.p++; 
      break; 
     case 'q': 
      fill.Q[fill.q] = i; 
      fill.q++; 
      break; 
     case 'r': 
      fill.R[fill.r] = i; 
      fill.r++; 
      break; 
     case 's': 
      fill.S[fill.s] = i; 
      fill.s++; 
      break; 
     case 't': 
      fill.T[fill.t] = i; 
      fill.t++; 
      break; 
     case 'u': 
      fill.U[fill.u] = i; 
      fill.u++; 
      break; 
     case 'v': 
      fill.V[fill.v] = i; 
      fill.v++; 
      break; 
     case 'w': 
      fill.W[fill.w] = i; 
      fill.w++; 
      break; 
     case 'x': 
      fill.X[fill.x] = i; 
      fill.x++; 
      break; 
     case 'y': 
      fill.Y[fill.y] = i; 
      fill.y++; 
      break; 
     case 'z': 
      fill.Z[fill.z] = i; 
      fill.z++; 
      break; 
     default: printf("\n"); 
     break; 
     } 
    i++; 
    } 
} 

void getUser() 
{ 
    test.testNum = 0; 
    test.userWord[45] = '\0'; 
    test.ptr = 0; 
    printf("\nWhat word would you like to search for?\n"); 
    scanf("%s", test.userWord); 
    test.userSize = strlen(test.userWord); 
} 

void findUserFirst() 
{ 
    switch(tolower(test.userWord[0])) 
    { 
    case 'a': 
     test.attempt = &fill.A[0]; 
     test.testNum = fill.a; 
     break; 
    case 'b': 
     test.attempt = &fill.B[0]; 
     test.testNum = fill.b; 
     break; 
    case 'c': 
     test.attempt = &fill.C[0]; 
     test.testNum = fill.c; 
     break; 
    case 'd': 
     test.attempt = &fill.D[0]; 
     test.testNum = fill.d; 
     break; 
    case 'e': 
     test.attempt = &fill.E[0]; 
     test.testNum = fill.e; 
     break; 
    case 'f': 
     test.attempt = &fill.F[0]; 
     test.testNum = fill.f; 
     break; 
    case 'g': 
     test.attempt = &fill.G[0]; 
     test.testNum = fill.g; 
     break; 
    case 'h': 
     test.attempt = &fill.H[0]; 
     test.testNum = fill.h; 
     break; 
    case 'i': 
     test.attempt = &fill.I[0]; 
     test.testNum = fill.i; 
     break; 
    case 'j': 
     test.attempt = &fill.J[0]; 
     test.testNum = fill.j; 
     break; 
    case 'k': 
     test.attempt = &fill.K[0]; 
     test.testNum = fill.k; 
     break; 
    case 'l': 
     test.attempt = &fill.L[0]; 
     test.testNum = fill.l; 
     break; 
    case 'm': 
     test.attempt = &fill.M[0]; 
     test.testNum = fill.m; 
     break; 
    case 'n': 
     test.attempt = &fill.N[0]; 
     test.testNum = fill.n; 
     break; 
    case 'o': 
     test.attempt = &fill.O[0]; 
     test.testNum = fill.o; 
     break; 
    case 'p': 
     test.attempt = &fill.P[0]; 
     test.testNum = fill.p; 
     break; 
    case 'q': 
     test.attempt = &fill.Q[0]; 
     test.testNum = fill.q; 
     break; 
    case 'r': 
     test.attempt = &fill.R[0]; 
     test.testNum = fill.r; 
     break; 
    case 's': 
     test.attempt = &fill.S[0]; 
     test.testNum = fill.s; 
     break; 
    case 't': 
     test.attempt = &fill.T[0]; 
     test.testNum = fill.t; 
     break; 
    case 'u': 
     test.attempt = &fill.U[0]; 
     test.testNum = fill.u; 
     break; 
    case 'v': 
     test.attempt = &fill.V[0]; 
     test.testNum = fill.v; 
     break; 
    case 'w': 
     test.attempt = &fill.W[0]; 
     test.testNum = fill.w; 
     break; 
    case 'x': 
     test.attempt = &fill.X[0]; 
     test.testNum = fill.x; 
     break; 
    case 'y': 
     test.attempt = &fill.Y[0]; 
     test.testNum = fill.y; 
     break; 
    case 'z': 
     test.attempt = &fill.Z[0]; 
     test.testNum = fill.z; 
     break; 
    default: printf("\n"); 
    } 
} 

int createString() 
{ 

    int i = 0, upRoom = 0, rightRoom = 0; 
    int leftRoom = 0, downRoom = 0; 
    int columnPos = 0, rowPos = 0; 
    int arPos = 0; 
    int moveup = 0, moveupr = 0; 
    int moveupl = 0, mover = 0; 
    int movel = 0, moved = 0; 
    int movedr = 0, movedl = 0; 
    char up[45]; 
    char upRight[45]; 
    char right[45]; 
    char downRight[45]; 
    char down[45]; 
    char downLeft[45]; 
    char left[45]; 
    char upLeft[45]; 


    arPos = *test.attempt; 

    columnPos = arPos/test.columns; 
    rowPos = arPos % test.rows; 

    upRoom = test.columns/2 - columnPos; 
    downRoom = test.columns/2 - columnPos; 
    rightRoom = test.rows/2 - rowPos; 
    leftRoom = test.rows/2 - rowPos; 

    moveup = -(test.rows); 
    moveupr = (test.rows - 1) * (-1), 
    moveupl = -(test.rows + 1); 
    mover = 1; 
    movel = 1; 
    moved = test.rows, 
    movedr = 1 * (test.rows + 1); 
    movedl = 1 * (test.rows - 1); 

    for(i=0; i < test.userSize; i++) 
    { 
     if(rightRoom >= 0) 
     { 
      right[i] = test.puzzleArray[arPos + i*mover]; 
      if(upRoom >= 0) 
       upRight[i] = test.puzzleArray[arPos + i*moveupr]; 
      if(downRoom >= 0) 
       downRight[i] = test.puzzleArray[arPos + i*movedr]; 
     } 
     if(leftRoom >= 0) 
     { 
      left[i] = test.puzzleArray[arPos - movel]; 
      if(upRoom >= 0) 
       upLeft[i] = test.puzzleArray[arPos + i*moveupl]; 
      if(downRoom >= 0) 
       downLeft[i] = test.puzzleArray[arPos + i*movedl]; 
     } 
     if(upRoom >= 0) 
      up[i] = test.puzzleArray[arPos + i*moveup]; 
     if(downRoom >= 0) 
      down[i] = test.puzzleArray[arPos + i*moved]; 
    } 

    for(i = 0; i < test.userSize; i++) 
    { 
     if(strcmp(up, test.userWord) == 0) 
      return 1; 
     if(strcmp(upRight, test.userWord) == 0) 
      return 2; 
     if(strcmp(right, test.userWord) == 0) 
      return 3; 
     if(strcmp(downRight, test.userWord) == 0) 
      return 4; 
     if(strcmp(down, test.userWord) == 0) 
      return 5; 
     if(strcmp(downLeft, test.userWord) == 0) 
      return 6; 
     if(strcmp(left, test.userWord) == 0) 
      return 7; 
     if(strcmp(upLeft, test.userWord) == 0) 
      return 8; 
     else 
      return -1; 
    } 
    return -1; 
} 

void found(int style) 
{ 
    int pos = 0, movement = 0; 
    int i, j, k = 0; 
    char *cpyPuzzle; 

    cpyPuzzle = (char*) malloc((test.puzzleSize) * sizeof(char)); 

    strcpy(cpyPuzzle, test.puzzleArray); 


    switch(style){ 
     case 1: 
      movement = test.rows; 
      break; 
     case 2: 
      movement = test.rows - 1; 
      break; 
     case 3: 
      movement = 1; 
      break; 
     case 4: 
      movement = test.rows + 1; 
      break; 
     case 5: 
      movement = test.rows; 
      break; 
     case 6: 
      movement = test.rows - 1; 
      break; 
     case 7: 
      movement = 1; 
      break; 
     case 8: 
      movement = test.rows + 1; 
      break; 
    } 

    for(i = 0; i < test.puzzleSize; i++) 
    { 
     cpyPuzzle[i] = '~'; 
    } 

    for(i=0; i < test.userSize; i++) 
    { 
     pos = test.attempt + (i*movement); 
     cpyPuzzle[pos] = test.userWord[i]; 
    } 

    for(i=0; i < test.columns; i++) 
    { 
     for(j=0; j < test.rows; j++) 
     { 
      printf("%c, ", cpyPuzzle[k]); 
      k++; 
     } 
     printf("\n"); 
    } 

    free(cpyPuzzle); 
} 
void notFound(char *fileN) 
{ 
    printf("In file %s %c was not found.\n", test.userWord, fileN); 
    printf("You can try finding another word"); 
    printf(" by pressing the option to show puzzle and\n"); 
    printf("then the option to find a word, once you've found one.\n"); 
} 
+1

In welcher Zeile sagt der Debugger, dass der Seg-Fehler aufgetreten ist? – Chimera

+1

'fflush (stdin)' ist schlecht - 'fflush' ist nur für Ausgabeströme gültig. –

+1

In Ihrem 'switch' == >>' Fall 'c': 'im' else' Teil haben Sie eine Aussage ohne Wirkung, das ist 'test.ptr;' Überprüfen Sie das. Was sollte 'test.ptr' hier tun? – Michi

Antwort

-1

(im Namen des OP) Eingestellt.

Gelöst! - Der Fehler war, dass ich nicht deneferenzierte und dann einen Zeiger referenzierte.