M
moijes12
Hi
My program is shown below.It is basically to print out all the
elements of a 2-dimensional array using a pointer to the array.
*****************************
#include <stdio.h>
int main(){
int x[3][5] = {
{1,2,3,4,5},
{6,7,8,9,10},
{11,12,13,14,15}
};
int *n;
int i,j;
n = x;
for(i=0;i<3;i++)
for(j=0;j<5;j++)
printf("%d ",*(*(n+i)+j));
printf("\n");
return 0;
}
*****************************
However,I get a compilation error
exercise_f.c: In function ‘main’:
exercise_f.c:12: warning: assignment from incompatible pointer type
exercise_f.c:15: error: invalid type argument of ‘unary *’ (have
‘int’)
I am using gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)
I am unable to understand why this causes such an error.If i declare
the pointer as 'int **n' ,it compiles without errors but on running,it
throws segmentation fault.
Is this a compiler problem or a programming one ?
Please help me with this.
regards
Moses
My program is shown below.It is basically to print out all the
elements of a 2-dimensional array using a pointer to the array.
*****************************
#include <stdio.h>
int main(){
int x[3][5] = {
{1,2,3,4,5},
{6,7,8,9,10},
{11,12,13,14,15}
};
int *n;
int i,j;
n = x;
for(i=0;i<3;i++)
for(j=0;j<5;j++)
printf("%d ",*(*(n+i)+j));
printf("\n");
return 0;
}
*****************************
However,I get a compilation error
exercise_f.c: In function ‘main’:
exercise_f.c:12: warning: assignment from incompatible pointer type
exercise_f.c:15: error: invalid type argument of ‘unary *’ (have
‘int’)
I am using gcc version 4.4.3 (Ubuntu 4.4.3-4ubuntu5)
I am unable to understand why this causes such an error.If i declare
the pointer as 'int **n' ,it compiles without errors but on running,it
throws segmentation fault.
Is this a compiler problem or a programming one ?
Please help me with this.
regards
Moses