I'm trying to dynamically allocate two dimensional array N+1xN with all elements double. Then I want to put value (1.6) for each element of an array. The last step is to print this array. My function
void inic(double **t)
{
for(int y=0;y<N;y++)
{
for(int x=0;x<N+1;x++)
{
t[x][y]=1.5;
}
}
}
isn't working as it should and I don't know why.
For example the (for n=3) result is:
[0][0][0][0]
[0][0][0][0]
[0][0][0][0]
And I expected:
[1.6][1.6][1.6][1.6]
[1.6][1.6][1.6][1.6]
[1.6][1.6][1.6][1.6]
[1.6][1.6][1.6][1.6]
Here is my code:
#include <stdio.h>
#include <stdlib.h>
int N;
void inic(double **t)
{
for(int y=0;y<N;y++)
{
for(int x=0;x<N+1;x++)
{
t[x][y]=1.5;
}
}
}
void print(double **t)
{
for(int y=0;y<N;y++)
{
for(int x=0;x<N+1;x++)
{
printf("%i ",t[x][y]);
}
printf("\n");
}
}
int main()
{
double **t;
N=3;
t=malloc((N+1)*sizeof(double*));
for(int i=0;i<N+1;i++)
{
t[i]=malloc(N*sizeof(double));
}
inic(t);
print(t);
printf("\n");
}
double **is not a 2D array and cannot point to one. You can allocate a 2D array dynamically without problem, why not use it? And "my code is not working" is not a specific problem description.1.6if you put everywhere1.5???? :)