array with negative index

Hi, I have a simple question can I use 2D array with negative index? thanx

Simple answer, too. No.

And to make that simple answer more complex, yes you can, but you shouldn't because it won't do what you expect it to do. So better stay with the simple answer of NO.

Korman

Well the simple answer isn't always the whole truth. Nothing in the language prevents one from supplying negative array indexes. Combine this with the fact that pointers can be treated as arrays and the combined effect will provide you with a way to do what you want as long as you're careful to set thing up correctly.

const int   array[] = { -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5 };
int*        parray  = &array[5];

int         entry   = parray[-5];

The value of entry should be '-5'.

If you declare an array, then no, you can't (really, you shouldn't) subscript it with a negative index. However, a pointer roughly mid-way into an array is a very useful method of creating a lookup table for values with negative and positive values. (Yes, I have done this for a real-life application)

However, a pointer roughly mid-way into an array is a very useful method of creating a lookup table for values with negative and positive values.

If you do this, use plenty of comments, because I guarantee you that 6 months later, the code will be completely meaningless.

It's almost always easier, and generally reflects life, if the indexes are 0 or positive. An offset can be added to get the value for the index, if that is what is needed.

If you do this, use plenty of comments, because I guarantee you that 6 months later, the code will be completely meaningless.

Funny you should say that, because 18 years later, I dug out the CD with the code on, and the arithmetic made perfect sense, even without comments. It was virtually impossible to read the code and not realise what was going on.

The code was sufficiently complex, and the processor (one of about thirty in the project) sufficiently well-matched to the problem that even adding an offset would have meant a significant 'hit' on performance. (Huffman decoding)

Yes I can use offset but this array will store information about move robot in X,Y coordinates. turn left (array[x,y-1]) turn right (array[x,y+1]) if I use offset I must know maximal negative X and Y axis but the robot's task is the mapping unknown room.I cannot define the size of array.

sorry for my bad english

To avoid negative array, you should divide the area into 4 quadrants, so that all the coordinates are positive. You'll need to understand which quadrant you are in, in order to access the right arrays, but that shouldn't be too hard to keep track of. When X or Y from one quadrant goes negative, you have shifted into another quadrant.

On the other hand, the origin for the universe could be a corner of the area to be mapped, not the center, so the coordinates never go negative.

Funny you should say that, because 18 years later, I dug out the CD with the code on, and the arithmetic made perfect sense, even without comments. It was virtually impossible to read the code and not realise what was going on.

I would venture to guess, then, that this was not the first application you wrote.

No, but it was my first in C for a long time.

I cannot define the size of array.

This is exactly the situation where you shouldn't do such thing as using negative array indices. And while you're at it, don't forget you have just 2kb RAM in total on an ATmega328, that's some 45x45 cells if each uses one byte or 128*128 when using one bit only. So the maximum size very quickly will be determined by running out of memory.

Korman

Oh, I had assumed it was a constant table and would be in flash memory. Sorry. :(

If you declare an array, then no, you can't (really, you shouldn't) subscript it with a negative index.

This is typically thinking in C. In other languages like Pascal it is quite common to have an array with negative indices or two boundaries beyond 0 The advise is however (as stated above) only to be used if it makes perfectly sense for your application!! eg var NorthPoleTemperatureFrequency: array[-273..100] of integer; makes perfectly sense in Pascal. // document it!!

Mind you in Perl using associative arrays even strings can be used as an index: number = Phone("john");

OK Arduino's are not programmed in Perl or Pascal I know so:

If you really want to use negative indices (or non zero based positive) , wrap the C-array in a class and use a function to access it's elements. That function does the needed math and can optionally do the boundary checking. Also very usefull for sparse array's ...

If you really want to use negative indices (or non zero based positive) , wrap the C-array in a class and use a function to access it's elements. That function does the needed math and can optionally do the boundary checking.

A most excellent idea.

It's better not to mess with negative indices, because that can result (perhaps) in a memory leak. And that is not good if you have very complex code running in your arduino. The ATMEGA chip may self-reset itself or have a software crash.

If you are a C++ expert, then you should know what I'm talking about.

The language is a tool and can be a both a headache and a joy.

An out of range index into an array can be done off both ends and the programmer needs to be careful regardless.