Segmentation fault (core dumped) below printf

#include <stdio.h>

int main(){
    int H, W, X, Y, cont=1, j=0, x, y;
    scanf("%d %d %d %d", &H, &W, &X, &Y);
    char matriz[H][W + 1];
    for(int i=0; i<H; i++){
        scanf("%s", matriz[i]);
    }
printf("%s", matriz[3]);
    while(j<4){
        while(1){
            y=y - 1;
            if(matriz[X][Y]==matriz[x][y])
             cont++;
            else
                break;
        }
        j++;
    }

    return 0;
}

hello I wanted to know why my code does not want to run that first printf, giving the error: "Segmentation fault (core dumped)" and if I remove everything below printf it will work again

Upvotes: 0

Views: 103

Answers (2)

Alireza Arbabi
Alireza Arbabi

Reputation: 23

Here when you give segmentation fault it means you are going out of the allocated storage for ((martiz[][])). for example:

if we have an array names x[4],

then we want to print x[5],

you will receive segmentation fault.

So look at your Code and find the line that program going out of allocated space. otherwise, notice that you should initialize your data types when you define them.

Upvotes: 0

user9706
user9706

Reputation:

x and y are not initialized so you are subject to undefined behavior.

Upvotes: 1

Related Questions