double (*buf)[2] = (double (*)[2])malloc(count*sizeof(double [2]));// Explain this
This
double (*buf)[2]
defines buf
to be a pointer to an array of 2 double
s.
This
(double (*)[2])malloc(count*sizeof(double [2]));
can (and shall) be rewritten as
malloc(count * sizeof(double [2]));
The above line allocates memory with the size of count
times "size for an array of 2 double
s".
This
=
assigns the latter to the former.
It all ends up with buf
pointing to an array of count * 2
double
s.
Access its elements like this
(*buf)[0][0];
Note that this approach creates a pointer to a "linear" array, that is an array where all elements are store in one continues block of memory.
Whereas the approch you 1st mention in you question creates a "scattered" array that is an array where each row might be located in a seperate block of memory.
This
printf("%d, %d, %d, %d \n",sizeof(double));
provokes undefined behaviour, as from its 1st (format) parameter the printf
expects four addtional parameters and is being passed only one.
The size of a double
typically is 8.
This
printf(" %d",sizeof(buf[0]));
prints the size of the first element that buf
points to. As buf
points to an array of 2 double
s, it is expected to print 2 times "size of a double
" which 2 * 8 = 16.
This
printf(" %d", sizeof(buf));
prints the size of buf
. As buf
is defined as a pointer, the size of a pointer on is printed. This typically is 4 for a 32bit implementation and 8 for 64bit implementation.
Note: The value of count
does not appear in any of the sizes printed above, not directly, nor indireclty, as In C it is not possible to derive from a pointer how much memory had been allocated to it.