Skip to contentSkip to conversation

&array[0] vs array

DISCUSSION Tobin C. Harding 3 participants 4 messages Mar 30, 2017 – Apr 3, 2017

lore
Share & tools
lore
#0 lore raw

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.

thanks,
Tobin.

#1 lore raw
Quoted 11 lines · Tobin C. Harding

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.

Signature · 13 lines
So many immigrant groups have swept through our town
that Brooklyn, like Atlantis, reaches mythological
proportions in the mind of the world - RI Safir 1998
http://www.mrbrklyn.com

DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002
http://www.nylxs.com - Leadership Development in Free Software
http://www2.mrbrklyn.com/resources - Unpublished Archive
http://www.coinhangout.com - coins!
http://www.brooklyn-living.com

Being so tracked is for FARM ANIMALS and and extermination camps,
but incompatible with living as a free human being. -RI Safir 2013
#2 lore raw
Quoted 7 lines · Ruben Safir
Quoted 6 lines · Tobin C. Harding

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;

Quoted 8 lines
Quoted 5 lines
$ 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

<snip>
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)
<snip>

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.

thx!
hofrat

#3 lore raw
Quoted 43 lines · Nicholas Mc Guire
Quoted 7 lines · Ruben Safir
Quoted 6 lines · Tobin C. Harding

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;

Quoted 8 lines
Quoted 5 lines
$ 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

<snip>
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)
<snip>

Quoted 4 lines

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.