# negative subscripts for 2D array of floats

Discussion in 'C Programming' started by lloyd, Nov 5, 2011.

1. ### lloydGuest

Hi,
I would like to set up a 2-D array of floats representing a function
of integer coordinates from the half-plane above (and including) the x-
axis. To be specific, I will calculate a function for each of the
integer points inside (and on the boundary of) the rectangle formed by
(-100,0) to (100,0) to (100,100) to (100,-100). This is a 101x201
rectangle which I would declare as follows:

double A[201][101];

I think it's possible to define a pointer p referring to the middle of
this array, so that I can use intuitive coordinates like p[-43][21]
inside my program to write to and access the array relative to the
pointer. Could someone tell me how to figure out what to define p as?
If I only had a one-dimensional array I think I could put

int p=A+100;

(or would I need to multiply 100 by the size of a float because it's
an array of floats, not ints?)

Thanks. I always use arrays instead of pointers and so there are some
gaps in my knowledge...

lloyd, Nov 5, 2011

2. ### Eric SosmanGuest

On 11/5/2011 3:14 PM, lloyd wrote:
> Hi,
> I would like to set up a 2-D array of floats representing a function
> of integer coordinates from the half-plane above (and including) the x-
> axis. To be specific, I will calculate a function for each of the
> integer points inside (and on the boundary of) the rectangle formed by
> (-100,0) to (100,0) to (100,100) to (100,-100).

Are you sure about that final coordinate? I'll assume it is
supposed to be (-100,100); if that's not right I've misunderstood
your intent and you should probably stop reading.

> This is a 101x201
> rectangle which I would declare as follows:
>
> double A[201][101];
>
> I think it's possible to define a pointer p referring to the middle of
> this array, so that I can use intuitive coordinates like p[-43][21]
> inside my program to write to and access the array relative to the
> pointer. Could someone tell me how to figure out what to define p as?
> If I only had a one-dimensional array I think I could put
>
> int p=A+100;
>
> (or would I need to multiply 100 by the size of a float because it's
> an array of floats, not ints?)

What you've shown is just a garble, not to be made meaningful
but anything as simple as a multiplication.

> Thanks. I always use arrays instead of pointers and so there are some
> gaps in my knowledge...

Here's a way to build a solution to your problem. Begin by
defining a name for the type representing "one column of values
along the Y dimension:"

typedef double Column[101];

Now your two-dimensional grid can be thought of as a one-dimensional
array of Columns:

Column A[201];

Then you can point to the midmost column:

Column *origin = &A[100];

.... and now you can write `origin[-43][21]'. It's possible to
dispense with the typedef and collapse everything into one
declaration, but doing it in small steps may avoid confusion,
particularly if you're leery of pointers.

--
Eric Sosman
d

Eric Sosman, Nov 5, 2011

3. ### lloydGuest

On Nov 5, 4:00 pm, Eric Sosman <> wrote:
>      Are you sure about that final coordinate?  I'll assume it is
> supposed to be (-100,100); if that's not right I've misunderstood
> your intent and you should probably stop reading.

You're right, sorry for that slip.

>      Here's a way to build a solution to your problem.

[snip]
> ... and now you can write `origin[-43][21]'.

That's fantastic, Eric, just what I needed. Thank you.

Lloyd.

lloyd, Nov 5, 2011