size of X->??
size of X[0]->??
double (*X)[5]
size of X->??
size of X[0]->??
Also if i want to print sizeof(main) it gives me 1
and sizeof(main()) gives me 4 why?
Try this test program:
/****************************/
#include <stdio.h>
double *X[5];
/* X is an array of 5 "pointers to double" */
double (*Y)[5];
/* Y is a pointer to an array of 5 doubles */
int main(void)
{
double array_of_5_double[] = {0.5, 1.5, 2.5, 3.5, 4.5};
X[0] = &array_of_5_double[0];
X[1] = &array_of_5_double[1];
X[2] = &array_of_5_double[2];
X[3] = &array_of_5_double[3];
X[4] = &array_of_5_double[4];
#if 0 // why does this not work?
X = {&array_of_5_double[0], /* array_of_5_double */ \
&array_of_5_double[1], \
&array_of_5_double[2], \
&array_of_5_double[3], \
&array_of_5_double[4]};
#endif
printf("sizeof(double) = %d // sizeof(double *) = %d //
sizeof(double (*)[5]) = %d\n", \
sizeof(double), sizeof(double *), sizeof(double (*)[5]));
/* 8 */
printf("sizeof(X) = %d // X = %p\n", sizeof(X), X,
&array_of_5_double);
printf("sizeof(X[0]) = %d // X[0] = %p // &array_of_5_double[0]
= %p\n", sizeof(X[0]), X[0], &array_of_5_double[0]);
// X[0] is the first element of our array: here it is a pointer to a
double with value 0.5
printf("sizeof(Y) = %d\n", sizeof(Y));
printf("sizeof(Y[0]) = %d\n", sizeof(Y[0])); // 40
printf("sizeof(main) = %d // sizeof(main()) = %d\n", sizeof(main),
sizeof(main()));
return 0;
}
/*
main is a function pointer!
main() is the invocation of the function "int main(void)" which will
return an integer of the following size:
sizeof(int) == sizeof(main())
*/
Explanations:
double *X[5];
/* X is an array of 5 "pointers to double" */
Thus sizeof(X) = 5*sizeof(double *)
"double *" is a pointer to a double (tip: move from right to left)
double (*Y)[5];
/* Y is a pointer to an array of 5 doubles */
Thus sizeof(Y) = sizeof(double (*)[5])
"double (*)[5]" is a pointer to an array of 5 doubles (tip: move from
inside to outside)
/*
/*
main is a function pointer!
main() is the invocation of the function "int main(void)" which will
return an integer of the following size:
sizeof(int)
This sizeof(main()) is the same size, i.e. the size of the returned
integer
*/
-anon.asdf