Pointer arithmetic and (array [0])

Today I looked at some source code (it was an example file explaining the use of the software environment) and found a lot of code like this:

int* array = new int[10]; // or malloc, who cares. Please, no language wars. This is applicable to both languages for ( int* ptr = &(array[0]); ptr <= &(array[9]); ptr++ ) { ... } 

So, basically, they did "take the address of the object that is at array + x ."

Normally, I would say that this is simple stupidity, since writing array + 0 or array + 9 does the same. I would even always rewrite such code in a size_t for loop, but this is a style issue.

But the overuse of this made me wonder: am I missing something obviously obvious or something hidden in the dark corners of the language?

For anyone who wants to take a look at the source code, with all its unpleasant goto , malloc and, of course, this thing with a pointer, do not hesitate to look at it on the Internet .

+4
source share
5 answers

You have not missed anything, they mean the same thing.

However, in order to try to shed some light on this, I must say that I also write similar expressions from time to time, for greater clarity.

I personally tend to think in terms of object-oriented programming, which means that I prefer to refer to the “address of the n element of the array” rather than the n th offset from the very beginning the address of the array. ”Despite the fact that these two things equivalent in C when I write code, I mean the first - so I express it.

Perhaps this is the reasoning of the person who wrote this.

+2
source

Yes, there is no good reason for the first. It is the same:

 int *ptr = array; 

I agree on the second one, I can also just write:

 ptr < (array + 10) 

Of course, you can also just do a for loop from 0 to 9 and set the temp pointer to point to the beginning of the array.

 for(int i = 0, *ptr = array; i < 10; ++i, ++ptr) /* ... */ 

This, of course, suggests that ptr does not change inside the body of the loop.

+5
source

Edit: this is partially incorrect. Read the comments.


The problem with &(array[0]) is that it expands to &(*(array + 0)) , which is due to dereference. Now, each compiler will obviously optimize this in the same style as array + 0 , but as for the language, dereferencing can cause UB in places where array + 0 will not.

+1
source

I think the reason they wrote it like this was because

 &(array[0]) 

and

 &(array[9]) 

just look alike. Another way is to write it down

 array + 0 

and

 array + 9 

respectively. As you mentioned, they essentially do the same thing (at least most compilers consider this one and the same, hopefully).

You can interpret two different types of expressions differently: the first can be read as "element address 0/9". The second can be read as "array pointer with element offset 0/9". The first sounds higher, the second sounds lower. However, most people prefer to use the second form.

Now, since array + 0 is of course the same as array , you can simply write array . I think the point here is that the beginning and end of the cycle look “similar” to each other. A matter of personal taste.

+1
source

According to classical math:

 Array[n] 

refers to the nth element of the array.

To "accept the address" of the nth element, the & or address of operator is used:

  &Array[n] 

To clear any suspected ambiguities, parentheses are added:

  &(Array[n]) 

For the reader reading from left to right, this expression matters:
Returns the address of an element at position 'n'

Insurance may have been designed as protection against old failed compilers.

Some people find this more readable than:

  Array + n 

Sorry, but I'm old school and prefer to use the '&' versions, paren or not. I will spend time making the code easier to read than worrying about which version takes more time to compile or which version is more efficient.

In a clear section with comments, the code has a higher return on investment than the code section, which is optimized for efficiency or uses sections of the language that are not appropriate for language lawyers.

+1
source

All Articles