473,464 Members | 1,588 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

negative array index

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?
Nov 14 '05 #1
6 18066
David Osborn wrote:
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

--
Artie Gold -- Austin, Texas
http://it-matters.blogspot.com (new post 12/5)
http://www.cafepress.com/goldsays
Nov 14 '05 #2
David Osborn <sp**@habitualhiatus.com> wrote:
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
--
\ Jens Thoms Toerring ___ Je***********@physik.fu-berlin.de
\__________________________ http://www.toerring.de
Nov 14 '05 #3
On Mon, 23 May 2005 18:40:58 GMT, David Osborn
<sp**@habitualhiatus.com> wrote in comp.lang.c:
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[i]; /* all valid */
}

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

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #4

"David Osborn" <sp**@habitualhiatus.com> wrote

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.
Nov 14 '05 #5
On Tue, 24 May 2005 06:27:18 +0000 (UTC), "Malcolm"
<re*******@btinternet.com> wrote:

"David Osborn" <sp**@habitualhiatus.com> wrote

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>>
Nov 14 '05 #6

"Barry Schwarz" <sc******@deloz.net> wrote

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".
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: dan | last post by:
I was recently surprised, and quite shocked in fact, to find that Python treats negative indices into sequence types as if they were mod(length-of-sequence), at least up to -len(seq). This fact...
2
by: Mantorok Redgormor | last post by:
Are negative values used as an index for an array valid or is this illegal and if it is illegal where does it state it in the standard? The only way I can see where a negative value can be used...
8
by: Joakim Hove | last post by:
Hello, I have the following code: #define N 99 double *ptr; double *storage; int index; storage = calloc(N , sizeof(double));
1
by: illegal.prime | last post by:
So I see from the documentation here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfSystemCollectionsArrayListClassBinarySearchTopic.asp That the code uses the...
1
by: Ryan Liu | last post by:
Hi, When my code run to DataRow.Delete() It throws: System.ArgumentOutOfRangeException: Non-negative number required. Parameter name: length at System.Array.Copy(Array sourceArray, Int32...
35
by: Martin Wells | last post by:
Plain char may be signed or unsigned. Typical ranges could be: CHAR_MIN == -128, CHAR_MAX == 127 CHAR_MIN == 0, CHAR_MAX == 255 The Standard says that the behaviour is undefined if we...
7
by: klays | last post by:
Hi all, I have created char array, when I use tab compiler shouts out: error: size of array 'tab' is negative I tried to cheat compiler and a little modify this code. Look at the code: ...
1
by: santoshsri | last post by:
Hi All, My C# web application calls a webservice to process a report. It sends XMLs as parameter and in response gets an XML node which stores Binay datatype bin.base64. It makes an instance of...
6
by: shashi shekhar singh | last post by:
Respected Sir, I am facing problem when i try to deploy my website on iis 7.0 on test page. i have to display some .mht files on iframe in gridview and error looks like below, Server Error in...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.