473,842 Members | 1,535 Online

# some pointer issues....

double * X[5]

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?

Aug 12 '07 #1
34 1918
On 12 Aug., 17:24, "sumedh.... ." <sumedhsak...@g mail.comwrote:
double * X[5]

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_doub le[] = {0.5, 1.5, 2.5, 3.5, 4.5};
X[0] = &array_of_5_dou ble[0];
X[1] = &array_of_5_dou ble[1];
X[2] = &array_of_5_dou ble[2];
X[3] = &array_of_5_dou ble[3];
X[4] = &array_of_5_dou ble[4];
#if 0 // why does this not work?
X = {&array_of_5_do uble[0], /* array_of_5_doub le */ \
&array_of_5_dou ble[1], \
&array_of_5_dou ble[2], \
&array_of_5_dou ble[3], \
&array_of_5_dou ble[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_dou ble);

printf("sizeof( X[0]) = %d // X[0] = %p // &array_of_5_dou ble[0]
= %p\n", sizeof(X[0]), X[0], &array_of_5_dou ble[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

Aug 12 '07 #2
On Aug 12, 9:21 pm, anon.a...@gmail .com wrote:
On 12 Aug., 17:24, "sumedh.... ." <sumedhsak...@g mail.comwrote:
double * X[5]
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_doub le[] = {0.5, 1.5, 2.5, 3.5, 4.5};
X[0] = &array_of_5_dou ble[0];
X[1] = &array_of_5_dou ble[1];
X[2] = &array_of_5_dou ble[2];
X[3] = &array_of_5_dou ble[3];
X[4] = &array_of_5_dou ble[4];
#if 0 // why does this not work?
X = {&array_of_5_do uble[0], /* array_of_5_doub le */ \
&array_of_5_dou ble[1], \
&array_of_5_dou ble[2], \
&array_of_5_dou ble[3], \
&array_of_5_dou ble[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_dou ble);

printf("sizeof( X[0]) = %d // X[0] = %p // &array_of_5_dou ble[0]
= %p\n", sizeof(X[0]), X[0], &array_of_5_dou ble[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
gr8 explanation:
just wanted to know why does
sizeof(main) -1
sizeof(main()) -sizeof(int)???? ?

Aug 12 '07 #3

"sumedh.... ." <su**********@g mail.comwrote in message
news:11******** *************@i 13g2000prf.goog legroups.com...
double * X[5]
Array of five pointers to type double
>
size of X->??
size of X is reported by

sizeof X

or

sizeof(double *[5])

I recommend the first method.
size of X[0]->??
size of X[0] is reported by

sizeof X[0]

or

sizeof(double)

Again, I recommend the first method.
>
double (*X)[5]
Pointer to an array of five doubles
size of X->??
size of X is reported by

sizeof X

or

sizeof(double(* )[5]

Again, I recommend the first method.
size of X[0]->??
size of X[0] is reported by

sizeof X[0]

or

sizeof(double[5])

Again, I recommend the first method.
>

Also if i want to print sizeof(main) it gives me 1
and sizeof(main()) gives me 4 why?
'main' (without parentheses) yields the address of
the function 'main()'. So sizeof(main) will give
the size of a pointer to a function. 'main()' invokes
the function main(), and returns its return value. This
type is 'int'. So 'sizeof(main()) ' gives the size of
type 'int'.

Note that these type sizes will vary among platforms (and
could conceivably vary among implementations for the same
platform).

-Mike
Aug 12 '07 #4

"sumedh.... ." <su**********@g mail.comwrote in message
news:11******** **************@ i38g2000prf.goo glegroups.com.. .
On Aug 12, 9:21 pm, anon.a...@gmail .com wrote:
sizeof(main) -1
sizeof(main()) -sizeof(int)???? ?
The type of 'main' is a pointer type.
The type of 'main()' is int.

Obviously, the sizes of these two different types
are not the same on your platform.

-Mike

Aug 12 '07 #5
On Aug 12, 10:42 pm, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
"sumedh.... ." <sumedhsak...@g mail.comwrote in message

news:11******** **************@ i38g2000prf.goo glegroups.com.. .
On Aug 12, 9:21 pm, anon.a...@gmail .com wrote:
sizeof(main) -1
sizeof(main()) -sizeof(int)???? ?

The type of 'main' is a pointer type.
The type of 'main()' is int.

Obviously, the sizes of these two different types
are not the same on your platform.

-Mike
i have read that size of pointer is 2bytes,
its 4bytes on my compiler... not an issue...
now...
If its a pointer type? why an integer/float/void/double pointer
declared inside main yields there size as 4 and main as 1?
so is this pointer diff than others?

Aug 12 '07 #6
Mike Wahler wrote, On 12/08/07 18:42:
"sumedh.... ." <su**********@g mail.comwrote in message
news:11******** **************@ i38g2000prf.goo glegroups.com.. .
>On Aug 12, 9:21 pm, anon.a...@gmail .com wrote:
sizeof(main) -1
sizeof(main( )) -sizeof(int)???? ?

The type of 'main' is a pointer type.
Wrong. The time of main is a function, not a pointer. sizeof(main) is
actually a constraint violation and a diagnostic is required. Of course,
with some C compilers (e.g. gcc) you have to poke the compiler hard
enough to get all the required diagnostics. Personally I think this gcc
extension is pointless.
The type of 'main()' is int.

Obviously, the sizes of these two different types
are not the same on your platform.
Indeed.
--
Flash Gordon
Aug 12 '07 #7
sumedh..... wrote, On 12/08/07 19:17:
On Aug 12, 10:42 pm, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
>"sumedh..... " <sumedhsak...@g mail.comwrote in message

news:11******* *************** @i38g2000prf.go oglegroups.com. ..
>>On Aug 12, 9:21 pm, anon.a...@gmail .com wrote:
sizeof(main ) -1
sizeof(main() ) -sizeof(int)???? ?
The type of 'main' is a pointer type.
The type of 'main()' is int.

Obviously, the sizes of these two different types
are not the same on your platform.

-Mike

i have read that size of pointer is 2bytes,
Whatever you read is implementation specific, not part of C. Also
irrelevant.
its 4bytes on my compiler... not an issue...
As you see. I've used implementations where the sizeof a pointer is 1.
now...
If its a pointer type? why an integer/float/void/double pointer
declared inside main yields there size as 4 and main as 1?
so is this pointer diff than others?
Because main is not a pointer and Mike was wrong.
--
Flash Gordon
Aug 12 '07 #8

"sumedh.... ." <su**********@g mail.comwrote in message
news:11******** *************@i 13g2000prf.goog legroups.com...
On Aug 12, 10:42 pm, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
>"sumedh..... " <sumedhsak...@g mail.comwrote in message

news:11******* *************** @i38g2000prf.go oglegroups.com. ..
On Aug 12, 9:21 pm, anon.a...@gmail .com wrote:
sizeof(main) -1
sizeof(main()) -sizeof(int)???? ?

The type of 'main' is a pointer type.
The type of 'main()' is int.

Obviously, the sizes of these two different types
are not the same on your platform.

-Mike

i have read that size of pointer is 2bytes,
its 4bytes on my compiler... not an issue...
The C language does not specify particular
sizes for pointer types. This depends upon your
implementation.
now...
If its a pointer type? why an integer/float/void/double pointer
declared inside main yields there size as 4 and main as 1?
so is this pointer diff than others?
There are several different pointer types in C. E.g.
'pointer to int', 'pointer to double', 'pointer to function',
etc. There's no requirement that the sizes of these pointers
are the same?

BTW why are you so concerned about these sizes? If some
part(s) of your program needs to know, just use the sizeof
operator. The specific sizes should not concern you.

-Mike
Aug 12 '07 #9

"Flash Gordon" <sp**@flash-gordon.me.ukwro te in message
news:0r******** ****@news.flash-gordon.me.uk...
sumedh..... wrote, On 12/08/07 19:17:
>On Aug 12, 10:42 pm, "Mike Wahler" <mkwah...@mkwah ler.netwrote:
>>"sumedh.... ." <sumedhsak...@g mail.comwrote in message

news:11****** *************** *@i38g2000prf.g ooglegroups.com ...

On Aug 12, 9:21 pm, anon.a...@gmail .com wrote:
sizeof(mai n) -1
sizeof(main( )) -sizeof(int)???? ?
The type of 'main' is a pointer type.
The type of 'main()' is int.

Obviously, the sizes of these two different types
are not the same on your platform.

-Mike

i have read that size of pointer is 2bytes,

Whatever you read is implementation specific, not part of C. Also
irrelevant.
>its 4bytes on my compiler... not an issue...

As you see. I've used implementations where the sizeof a pointer is 1.
>now...
If its a pointer type? why an integer/float/void/double pointer
declared inside main yields there size as 4 and main as 1?
so is this pointer diff than others?

Because main is not a pointer and Mike was wrong.
Huh? Are you saying that the name of a function (without parentheses)
does not yeild a pointer value?

-Mike
Aug 12 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.