I would like to have a 1D pointer point to a M by N 2D dynamically allocated array. If the 2D is a constant array, everything works fine:
main()
{
int i, j;
double *b, A[3][2];
for (i = 0; i < 3; i++)
for (j = 0; j < 2; j++)
A[j] = 1.0;
b = &A[0][0];
}
/* b = 1.0 1.0 1.0 1.0 1.0 1.0 */
This works OK.
I think malloc allocates more memory than it needs because if A is now dynamically allocated, ..
main()
{
int i, j;
double *b, **A;
A = (double **) malloc(3*sizeof(double *) );
for (i = 0; i < 3; i++)
A = (double *) malloc(2*sizeof(double ) );
for (i = 0; i < 3; i++)
for (j = 0; j < 2; j++)
A[j] = 1.0;
b = &A[0][0];
}
/* b = 1.0 1.0 0.0 1.0 1.0 0.0 */
^ ^
Why are those 0's there? Is there a way where I can make a 1D pointer point to a NxM 2D allocated array?
Thanks
main()
{
int i, j;
double *b, A[3][2];
for (i = 0; i < 3; i++)
for (j = 0; j < 2; j++)
A[j] = 1.0;
b = &A[0][0];
}
/* b = 1.0 1.0 1.0 1.0 1.0 1.0 */
This works OK.
I think malloc allocates more memory than it needs because if A is now dynamically allocated, ..
main()
{
int i, j;
double *b, **A;
A = (double **) malloc(3*sizeof(double *) );
for (i = 0; i < 3; i++)
A = (double *) malloc(2*sizeof(double ) );
for (i = 0; i < 3; i++)
for (j = 0; j < 2; j++)
A[j] = 1.0;
b = &A[0][0];
}
/* b = 1.0 1.0 0.0 1.0 1.0 0.0 */
^ ^
Why are those 0's there? Is there a way where I can make a 1D pointer point to a NxM 2D allocated array?
Thanks