Are pointers of the same size? [duplicate]
Possible Duplicates:
Are there are any platforms where pointers to different types have different sizes? are all data pointers of the same size in one platform?
sizeof(Bucket *)
is the same as sizeof(int *)
,right?
BTW,is char arr[];
valid in c90?
I believe there is no requirement that pointers are all the same size. The only requirement is that all object pointers can be stored in void *
, that when cast to void *
and back they'll be the same, and that all function pointers, if cast to another type of function pointer and back again, will be the same. (Casting function pointers to void *
is not guaranteed to work. Sorry if I indicated otherwise.)
In practice, I think the only time you'll find pointers of different sizes is if you care about C++'s member-function pointers, or you work on a more obscure architecture.
In C90, char arr[];
is a valid way to pre-declare a global array of to-be-determined length. Note that char arr[]
is an incomplete type, so you have to declare it before you can use sizeof
. It's not valid the way it is in C99, to make flexible array members.
Original post:
Yes, that's right. Pointers are of the same size.
But, that's wrong. I've shot too fast! I'm sorry for that. Some "cases" of different pointer sizes do exist, platform dependent and also type dependent: Have a look at http://c-faq.com/ptrs/generic.html and http://c-faq.com/null/machexamp.html for examples or at least general explanations. (Many thanks @fmark and @paxdiablo, I've also learned something). However, I never ever had a situation where pointers were not the same size (fortunately/unfortunately?).
精彩评论