Reputation: 45
I've declared a matrix dynamically as follows
double **y;
y = (double **)malloc(n*sizeof(double *));
for(i=0;i<n;i++)
y[i]=(double*)malloc(m*sizeof(double));
Where m and n are integers declared before. Then, I need to compute a function that multiplies two different matrix, and I need to check if the number of rows on the first matrix coincides with the number of columns on the second matrix. So I need to know the numbers of columns and rows. So I computed what follows:
int k=sizeof(M[0])/sizeof(double);
But this integer k returns me 1. And no matther how long n and m are...
What am I doing wrong?
Thanks and sorry for my english.
Upvotes: 0
Views: 109
Reputation: 4402
Using sizeof(M[0])
only gets you the type size of M[0]
which is double *
which in your case identical to size of double
So in your case you have to save the size of the allocated array (n, m).
Another solution would be to use stack allocation where sizeof
can actually get the size of types like double[]
Upvotes: 3
Reputation: 29724
You cannot use sizeof operator on dynamically created array to get array size. If array is created dynamically the only option to know it's size is to store it somewhere.
Upvotes: 4