Path: csiph.com!feeder.erje.net!2.us.feeder.erje.net!feeder.usenetexpress.com!feeder1.iad1.usenetexpress.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!buffer1.nntp.dca1.giganews.com!news.giganews.com.POSTED!not-for-mail NNTP-Posting-Date: Sun, 04 Jun 2017 16:39:16 -0500 Message-Id: <2msc0e-j06.ln1@walter.foo> From: "Peter \"Shaggy\" Haywood" Subject: Re: sizeof(x) / sizeof(x[0]) Newsgroups: comp.lang.c Date: Sun, 04 Jun 2017 18:24:01 +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> User-Agent: KNode/0.10.9 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8Bit Lines: 64 X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-Wgj8XzjVWS4GcVjbJc05ccwif/fyUE+7ZYh/jxJzozfD/gaB79O9VtdyJMhVLCTF4S27DGbVQVVtZhN!MwHDLQmRtUIE+6tA8z6zphB6ul2H36UFOQnpEAtf29paq+Wx X-Complaints-To: abuse@giganews.com X-DMCA-Notifications: http://www.giganews.com/info/dmca.html X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 3677 Xref: csiph.com comp.lang.c:111492 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!!