Why am I getting Segmentation Fault error

Asked

Viewed 30 times

0

I’m learning C in one of the test programs I’m doing, I came across a problem. The challenge is to fill a vector[2][2] with values typed by the user, but this should be done by a function and using pointers. It would be very easy to do this with a loop for within another loop for. In my case my function rotates and can fill up to 3 values of the matrix. In the 4th it gives error:

Segmentation Fault (Dumped Core)

and I have no idea why this is happening.

If anyone has any ideas, please help me.

It follows below all my code:

int testef(int i, int j, int **teste){

    if(i == 2){
        return 0;
    }
    if(j == 2){
        return testef(i+1,0,&teste);
    }
    else{
        printf("Valor[%d][%d]: ",i,j);
        scanf("%d",&teste[i][j]);
        return testef(i,j+1,&teste);
    }
}


int main() {

    int **teste;
    int i = 0;
    int j = 0;

    testef(i,j,&teste);
    printf("%d",&teste);


    return 0;
  • You are using pointers but have not noticed any memory allocation. What is the sense of printf("%d",&teste); whereas it stated int **teste;? Is there a requirement to use a recursive function?

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.