1. pointer manipulating 2-dimensional array

Hi.
I'm beginner with C and I'm having problem with the following source code:

Code:
```/*****************************************************************************************************/
main()
{

unsigned int lin=10;
unsigned int col=5;
unsigned int i,j;
double *grid;
double x;
grid=malloc(col*lin*sizeof(double));
if(!grid)
{
printf("Err\n");
exit(1);
}
for(i=0;i<lin;i++)
{
for(j=0;j<col;j++)
{
x=pow(i+j,2);
*(grid+(i*col+j)*sizeof(double))=x;
}
}
for(i=0;i<lin;i++)
{
for(j=0;j<col;j++)
{
printf("%10.7f\t",*(grid+(i*col+j)*sizeof(double)));
}
printf("\n");
}

/*****************************************************************************************************/```
It works nice for smaller lin and col but returns "segmentation fault" if them becomes lage.
I'm using gcc 4.2 with linux kernel 2.6.22.

Thanks.

PS I'm beginner in english language as well... :P

2. when you increment a double pointer you dont need to tell it how many bytes to increment by, thats done automatically.

Here:
Code:
```  for(i=0;i<lin;i++)
{
for(j=0;j<col;j++)
{
x=pow(i+j,2);
*(grid+(i*col+j)*sizeof(double))=x;
}
}```
You could do something like:
Code:
```double *p;
for(i=0;i<lin;i++)
{
for(j=0;j<col;j++)
{
x=pow(i+j,2);
*p=x;
p++;
}
}```
That should work and would also save some extra calculations. You generally only want to use sizeof() when allocating space.

 or doing it your way just change the pointer bit to:
Code:
`*grid+(i*col+j)=x;`
[/edit]

3. Thanks mike_g.
That helped me alot!!!