2

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?

Community
  • 1
  • 1
arr
  • 301
  • 1
  • 3
  • 5
  • 1
    Dup of [Are there are any platforms where pointers to different types have different sizes?](http://stackoverflow.com/questions/916051/are-there-are-any-platforms-where-pointers-to-different-types-have-different-size), [Can the Size of Pointers Vary Depending on what's Pointed To?](http://stackoverflow.com/questions/1473935/can-the-size-of-pointers-vary-depending-on-whats-pointed-to) – outis Nov 16 '10 at 09:24
  • 1
    you should split this into two questions so that they can be well answered separately – Nathan Fellman Nov 16 '10 at 09:31
  • Duplicate of [are all data pointers of the same size in one platform?](http://stackoverflow.com/questions/1241205/are-all-data-pointers-of-the-same-size-in-one-platform) – John Bode Nov 16 '10 at 13:20

2 Answers2

4

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.

Chris Lutz
  • 73,191
  • 16
  • 130
  • 183
  • -1 Again, incorrect. See http://c-faq.com/ptrs/generic.html – fmark Nov 16 '10 at 09:31
  • 1
    I think -1 for an unclear wording is a bit harsh. I honestly forgot all about function pointers until the second paragraph of my answer, so I didn't think to make the distinction earlier, especially since the OP didn't seem particularly interested in function pointers. – Chris Lutz Nov 16 '10 at 09:35
  • Is `char arr[];` valid in c89? – arr Nov 16 '10 at 10:51
  • @arr - As valid as it is in C90. C89 == C90 in pretty much all respects. – Chris Lutz Nov 16 '10 at 10:53
  • Can you elaborate more about []?what do you mean by "you have to declare it before you can use sizeof."? – arr Nov 16 '10 at 11:01
  • @arr - `char arr[]; int main(void) { printf("%s\n", arr); return 0; } char arr[] = "Hello, world!";` is valid. We can use `arr` even though it isn't defiend, but it is an incomplete type, so it would be invalid to take the `sizeof(arr)` until after we define it (with `char arr[] = "Hello, world!";`). Basically, `char arr[]` isn't tremendously useful in C89/C90, and in C99 is only useful for flexible array members (which you can do with some work in C89/C90 anyway). – Chris Lutz Nov 16 '10 at 11:13
  • Are you sure this is valid? I would agree if you make it `extern char arr[];` but `char arr[];` is a definition and not merely a declaration, and thus I don't think it's valid to leave the `[]` empty. – R.. GitHub STOP HELPING ICE Nov 16 '10 at 15:18
  • @R.. - I believe it is. At least, it works when I compile it with `gcc -ansi -pedantic -Wall -Wextra -Werror`. I forget where exactly I read that it worked (probably on SO somewhere). – Chris Lutz Nov 17 '10 at 23:18
2

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?).

Flinsch
  • 4,296
  • 1
  • 20
  • 29
  • BTW,is char arr[]; valid in c90? – arr Nov 16 '10 at 09:19
  • That is correct but the size of the pointer is platform dependent – the_drow Nov 16 '10 at 09:19
  • 2
    Really? Would you like to cite your source for that claim ("pointers are of the same size")? As in: which bit of the ISO standard mandates this? – paxdiablo Nov 16 '10 at 09:23
  • In practice this is true, but some peripheral searching suggests that it's not necessarily the case. – Chris Lutz Nov 16 '10 at 09:29
  • 1
    -1, Incorrect. See http://c-faq.com/ptrs/generic.html – fmark Nov 16 '10 at 09:30
  • @pax: No, but could you give an example for the case pointers are not the same size? Of course, the concrete size of pointers is platform dependent, as drow already said. But, then, sizeof(T*) is always equal to sizeof(U*) for any T and U. If not, please provide an example. I'd be pretty interested in it. – Flinsch Nov 16 '10 at 09:31
  • 2
    http://c-faq.com/null/machexamp.html - I didn't downvote you, BTW, I wanted to give you a chance to rectify it, especially since most of those architectures are dinosaurs (though DOS is still in use a little). – paxdiablo Nov 16 '10 at 09:32