I've browsed to previously answered questions regarding pointers and matrices, but in these cases the matrices were seen as pointers to pointers. However, I am trying to create a function which read a matrix using a simple pointer and another function which prints it. This is my code, the read functions seems to work properly, but the program crashes at the printing part. If I remove the "*" from the printf statement the program works(i.e. it prints numbers from 4 to 4- I suppose this is alright, since an int is stored on 4 bytes).
void readm(int *p,int n) { p=(int *)malloc(sizeof(int)*n*n); for(int i=0;i<n*n;i++) scanf("%d",p+i); } void printm(int *p,int n) { for(int i=0;i<n;i++) { for(int j=0;j<n;j++) printf("%d ",*(p+(i*n)+j)); printf("\n"); } }
SEGFAULT? Could you also include the code using those functions ?malloc.