-4

When i run my code it runs but it's return value is not 0. So i tried debugging mode. And the problem is caused by if(yol[i][j]==' ') part. When compiler comes here, segmentation fault error is shown up. Why is this happening guys?

void randomElma(char yol[32][44]){
    srand(time(NULL));
    int i,j;
    for(i=0;i<32;i++){
        for(j=0;j<44;j++){
            if(yol[i][j]==' '){
                if((rand()%10)==1){
                    yol[i][j]='O';
                }
            }
        }
    }
}

Here is my main function:

int main(int argc, char *argv[]) {
    int N=31, M=43;
    int i=0, j=0;
    char ch;
    int map[32][44];
    char yol[32][44];
    puan=0;
    
    FILE *dosya;
    dosya=fopen("harita.txt","r");
    
    while ((ch = fgetc(dosya)) != EOF){
        yol[i][j]=ch;
        if(ch==' ')
            map[i][j]=1;
        else
            map[i][j]=0;
        if(ch=='\n'){
            i++; j=0;
        }
        else{
            j++;
        }
    }
    
    for(i=0;i<31;i++){
        for(j=0;j<44;j++){
            printf("%d",map[i][j]);
        }
        printf("\n");
    }
    fclose(dosya);
    map[29][1]=1;
    map[29][41]=1;
    
    randomElma(yol[32][44]);
    
    DFS(map,29,1,yol); // 29, 41
    
    return 0;
}

Also i added time.h library

Baguviks
  • 1
  • 2
  • 5
    Please post a [mcve]. – Sourav Ghosh Jun 02 '22 at 20:37
  • 3
    You may want to read [srand() — why call it only once?](https://stackoverflow.com/questions/7343833/srand-why-call-it-only-once) in case you call it multiple times. – Retired Ninja Jun 02 '22 at 20:39
  • 1
    The problem is with the array in the caller. Show how you're declaring that array and calling the function. – Barmar Jun 02 '22 at 20:40
  • Questions seeking debugging help should generally provide a [mre] of the problem, which includes a function `main` and all `#include` directives. In particular, we need to see how you are calling the function `randomElma`. – Andreas Wenzel Jun 02 '22 at 20:43
  • Have a look [here](https://replit.com/@robertwharvey/MajesticButteryAgents#main.c) – Robert Harvey Jun 02 '22 at 20:49
  • 1
    Yeah, now we'll need to see your input file. Have a look at the repl.it I linked in the comment above this one. – Robert Harvey Jun 02 '22 at 20:51
  • I just edited the question and added main function. btw I there is nothing wrong with file operations. when I print yol array, nothing is wrong – Baguviks Jun 02 '22 at 20:54
  • 2
    The line `randomElma(yol[32][44]);` will attempt to pass a `char` to `randomElma`, instead of a pointer to an array. You should probably write `randomElma( yol );` instead. In that case, `yol` will [decay](https://stackoverflow.com/q/1461432/12149471) to a pointer to the first sub-array. – Andreas Wenzel Jun 02 '22 at 20:54
  • I pasted my code to replit. Here is my code https://replit.com/join/cswncrcxsu-baguviks – Baguviks Jun 02 '22 at 21:00
  • ohhhh thank you so much! I wish i posted all my code in the first place. – Baguviks Jun 02 '22 at 21:01
  • Your compiler should have warned you about you passing the wrong type (`char` instead of pointer) to `randomElma`. I suggest that you make sure that you have all warnings enabled. You may want to read this: [Why should I always enable compiler warnings?](https://stackoverflow.com/q/57842756/12149471) – Andreas Wenzel Jun 02 '22 at 21:03
  • Note that after your most recent edit, you still have not provided a [mre]. It is not complete. For example, you are not showing the `#include` directives and you are not showing the function `DFS`. Also, when posting a [mre], it should be posted as a single code snippet. For this reason, it is unlikely that your question will be reopened in its current state. A link to an external site does not qualify, as questions should be self-contained. – Andreas Wenzel Jun 03 '22 at 22:45

0 Answers0