From mboxrd@z Thu Jan 1 00:00:00 1970 From: me@tobin.cc (Tobin C. Harding) Date: Mon, 3 Apr 2017 11:28:59 +1000 Subject: &array[0] vs array In-Reply-To: <20170330160551.GA32725@osadl.at> References: <20170330003044.GI25014@eros> <20170330160551.GA32725@osadl.at> Message-ID: <20170403012859.GJ3755@eros> To: kernelnewbies@lists.kernelnewbies.org List-Id: kernelnewbies.lists.kernelnewbies.org On Thu, Mar 30, 2017 at 04:05:51PM +0000, Nicholas Mc Guire wrote: > On Thu, Mar 30, 2017 at 09:04:35AM -0400, Ruben Safir wrote: > > On 03/29/2017 08:30 PM, Tobin C. Harding wrote: > > > Does the kernel community have a preference when using the address of > > > the first element of an an array? > > > > > > 1. addr = &array[0] > > > 2. addr = array; > > > > > > $ grep '\&.*\[0\]' | wc -l > > > 10077 > > > > > > style (1) is clearly used, I was not able to grep for instances where > > > style (2) is used. > > > > maybe there is a another reason why 2 is not used. > > > the second form is used - just not quite as often > with the below quick (and probably incomplete) coccinelle script you > can find a few hundred occurences of the second form in linux-next > > > virtual report > > @v2@ > identifier array,addr; > type T; > position p; > @@ > > ( > * T array[]; > | > * T array[] = ...; > ) > ... > * addr = array at p > > @script:python@ > p << v2.p; > @@ > print "%s:%s" % (p[0].file,p[0].line) > > > If its really obvious that its an array that you are manipulating > maybe the second version is fine - if its not so obvious the > first version makes it clear - and as readability is a key issue > for any complex code I suspect that readability explains the preference. Got it, readability over brevity. thanks, Tobin.