> With the pointer loop, you need to understand the context of the
> surrounding for loop to understand what this statement is executing.
> I can’t tell anything from just this one line of code other than that
> a method is being called with what appear to be a pointer (based on
> the assumption that it is using a standard C pointer naming
> convention).
Again, read [1], section "The use of pointers". Maybe you don't agree with it, but almost
all the people here agree with it.
> Forgive my ignorance of the LEN macro, but doesn’t this rely on
> accessing a memory address outside of the allocated array? Wouldn’t
> that cause problems if you’re up against memory boundaries? I would
> personally write it as either
ANSI C standard, section 3.3.8 (Relational operators):
When two pointers are compared, the result depends on the
relative locations in the address space of the objects
pointed to. If the objects pointed to are members of the
same aggregate object, pointers to structure members declared
later compare higher than pointers to members declared
earlier in the structure, and pointers to array elements with
larger subscript values compare higher than pointers to
elements of the same array with lower subscript values. All
pointers to members of the same union object compare equal.
If the objects pointed to are not members of the same
aggregate or union object, the result is undefined, with the
following exception. If P points to the last member of an
array object and Q points to a member of the same array
object, the pointer expression P+1 compares higher than Q ,
even though P+1 does not point to a member of the array
object.
It is no casuality they added this rule to the standard. They added
it because is the idiomatic way of running over an array in C. Of
course CS teachers that liked pascal don't teach it anymore when
they were forced to move to C, but it is the best way for experienced
C programmers. Get used to it.
Regards,
[1]
http://www.lysator.liu.se/c/pikestyle.html
Received on Mon Apr 27 2015 - 10:21:20 CEST