Quantcast
Channel: Active questions tagged gcc - Stack Overflow
Viewing all articles
Browse latest Browse all 22014

C Segmentation Fault While using scanf for 2D array

$
0
0

As suggested by a book of "Gottfried", I tried to input an array and display the contents of array in matrix form :

#include<stdio.h>

#define row 2
#define col 3

int main(){
    int (*a)[col];
    int i,j;

    for(i=0;i<row;i++){
        for(j=0;i<col;j++){
            printf("Enter a(%d,%d)",i,j);
            scanf("%d",(*(a+i)+j));
        }
    }

    return 0;
}

I get the following output after inputting an element :

Segmentation fault (core dumped)

What is the problem in the code? Was it working in previous version of GCC so the writer wrote it down? What is the correct way to solve the problem with the same level of simplicity?


Viewing all articles
Browse latest Browse all 22014

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>