negative array index

D

David Osborn

I have a pointer "a" that points to an element in an array that is not
the first element. I want to use a negative index to derefence the
pointer, like so: a[-1]. Is this valid C99? If not, I can just say
*(a-1), right?
 
A

Artie Gold

David said:
I have a pointer "a" that points to an element in an array that is not
the first element. I want to use a negative index to derefence the
pointer, like so: a[-1]. Is this valid C99? If not, I can just say
*(a-1), right?

Yes, as long as your pointing to a valid location in an `array'.
[BTW a[-1] and *(a - 1) are completely equivalent; (-1)[a], though ugly,
would also mean the same thing.]

HTH,
--ag
 
J

Jens.Toerring

David Osborn said:
I have a pointer "a" that points to an element in an array that is not
the first element. I want to use a negative index to derefence the
pointer, like so: a[-1]. Is this valid C99? If not, I can just say
*(a-1), right?

a[ -1 ] and *(a-1) are identical, the second being what the compiler
converts the first to (already with C89). And yes, you can access
elements of the array also with negative indexes for 'a' as long as
you stay within the limits of the array 'a' is pointing into.

Regards, Jens
 
J

Jack Klein

I have a pointer "a" that points to an element in an array that is not
the first element. I want to use a negative index to derefence the
pointer, like so: a[-1]. Is this valid C99? If not, I can just say
*(a-1), right?

This is valid in any version of C, not just C99, as long as the
pointer itself is either within the bounds of the array or points to
one past the last element, and the combination of the pointer and the
subscript does point into the array, not past the end and not before
the beginning.

Examples:

int a[10] = { /* whatever */ };
int i;
b = a + 10; /* one past end */

for (i = -10; i < 0; i++)
{
int x = b; /* all valid */
}

....but b[-11] is invalid.
 
M

Malcolm

David Osborn said:
I have a pointer "a" that points to an element in an array that is not the
first element. I want to use a negative index to derefence the pointer,
like so: a[-1]. Is this valid C99? If not, I can just say *(a-1), right?
The form a[-1] is allowed.
In my opinion this is compileable gibberish rather than C, and it has to be
*(a-1), but other people may disagree.
 
B

Barry Schwarz

David Osborn said:
I have a pointer "a" that points to an element in an array that is not the
first element. I want to use a negative index to derefence the pointer,
like so: a[-1]. Is this valid C99? If not, I can just say *(a-1), right?
The form a[-1] is allowed.
In my opinion this is compileable gibberish rather than C, and it has to be
*(a-1), but other people may disagree.
Since the two are guaranteed to have the same meaning, along with
(-1)[a], other than aesthetics what do you use to determine gibberish?


<<Remove the del for email>>
 
M

Malcolm

Barry Schwarz said:
Since the two are guaranteed to have the same meaning, along with
(-1)[a], other than aesthetics what do you use to determine gibberish?
(-1)[a] is very clearly compileable gibberish.
A programmer who knew only a little C wouldn't have a clue what that
construct meant, whilst an experienced programmer would find such code hard
to read and to maintain.
It is a question of aethetics, but that doesn't mean "trivial" or
"unimportant".
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,187
Latest member
RosaDemko

Latest Threads

Top