Hello everyone
I am new to C and I am having a few problems with pointers. It wouuld be nice if someone could help me out
This is what I am trying to do
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int n,i,j;
int a[3][4],**Trial=NULL;
Trial = (int **)malloc(3*sizeof(int*));
for(i=0;i<3;i++)
{
*(Trial+i) = (int*)malloc(4*sizeof(int));
}
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("Enter the %d,%d coefficient\n",i,j);
scanf("%d",&Trial[i][j]);
}
printf("Enter the result of the equation\n");
scanf("%d",&Trial[i][3]);
}
for(i=0;i<3;i++)
{
for(j=0;j<4;j++)
{
printf("The value at position (%d,%d) is %d\n",i,j,Trial[i][j]);
}
}
return 0;
}
This program works fine as long as I do not use pointer arithmetic
but when I use pointer arithmetic I keep getting a segemntation fault
I replace the
Code:
scanf("%d",&Trial[i][j]);
with
Code:
scanf("%d",*(Trial+(i+j)));
and I get a segmentation fault after the first outer loop.
Can someone please tell me what I am doing wrong
Thanks