Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #111492
| Message-Id | <2msc0e-j06.ln1@walter.foo> |
|---|---|
| From | "Peter \"Shaggy\" Haywood" <phaywood@alphalink.com.au> |
| Subject | Re: sizeof(x) / sizeof(x[0]) |
| Newsgroups | comp.lang.c |
| Date | 2017-06-04 18:24 +1000 |
| References | <1420cd8f-e5cb-4a8f-93e6-d80a8b050650@googlegroups.com> <59252557$0$15332$426a74cc@news.free.fr> <2cnnvd-ub5.ln1@walter.foo> <592ab9f3$0$31632$426a74cc@news.free.fr> |
Groovy hepcat Patrick.Schluter was jivin' in comp.lang.c on Sun, 28 May
2017 9:52 pm. It's a cool scene! Dig it.
> Le 27.05.2017 à 09:44, Peter "Shaggy" Haywood a écrit :
>> Groovy hepcat Patrick.Schluter was jivin' in comp.lang.c on Wed, 24
>> May 2017 4:16 pm. It's a cool scene! Dig it.
[Snip...]
>>> As other have explained, that macro falls apart when applied to a
>>> pointer instead of an array. Something that can happen quite easily.
>>
>> Patrick, pointers don't come into the equation here. Alla_ is
>> simply
>> confusing things. Though you are correct, it's irrelevant to the
>> question the OP has asked.
>
> No, it's not! (and we can do the Monty Pyton sketch to continuer :-)
The OP asked about determining the number of an array's elements by
dividing the size of the array by the size of an element. What do
pointers have to do with that?
>>> The thing to do in practice and if you use gcc is to use an
>>> extension
>>
>> No, the thing to do in practice is usually to try to write code as
>> portable as practical. There are situations in which portability will
>> not be practical, but this is not one of those situations.
>
> In practice, one has often to do with a foreign and big codebase one
> doesn't (can't) know every corner of. Replacing the portable macro
> x/x[0] by the implementation specific gnu thingy (when possible) can
> have surprizing results.
Yes, and more often than not in ways you don't want. It's better to
take the path of least surprise. If there's a bug, you don't find and
eradicate it by masking it with non-standard stuff.
> It happens sometimes that the code works
> somehow not completely correctly but enough so that it doesn't blow in
> your face directly.
And then blows up in the client's face after shipping, yeah!
> By using that macro (lifted from Linux kernel), an
> error of the compiler will take care of you, because the unsavory
> thing with the x/x[0] formula is that it compiles when x is a pointer
> and that is very rarely what you want.
So don't use it with a pointer. You wouldn't use it with an int, a
double, a char, an unsigned long, etc. Why use it with a pointer? I
can't see why anyone would use it with anything but an array, unless
they lack a basic understanding of C (which, yes, Alla_ obviously
does).
--
----- Dig the NEW and IMPROVED news sig!! -----
-------------- Shaggy was here! ---------------
Ain't I'm a dawg!!
Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread
Re: sizeof(x) / sizeof(x[0]) "Patrick.Schluter" <Patrick.Schluter@free.fr> - 2017-05-24 08:16 +0200
Re: sizeof(x) / sizeof(x[0]) "Peter \"Shaggy\" Haywood" <phaywood@alphalink.com.au> - 2017-05-27 17:44 +1000
Re: sizeof(x) / sizeof(x[0]) "Patrick.Schluter" <Patrick.Schluter@free.fr> - 2017-05-28 13:52 +0200
Re: sizeof(x) / sizeof(x[0]) "Peter \"Shaggy\" Haywood" <phaywood@alphalink.com.au> - 2017-06-04 18:24 +1000
csiph-web