negative subscripts for 2D array of floats

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

  1. lloyd

    lloyd Guest

    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
    #1
    1. Advertising

  2. lloyd

    Eric Sosman Guest

    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
    #2
    1. Advertising

  3. lloyd

    lloyd Guest

    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
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Cameron, Charles B.

    VHDL Subscripts

    Cameron, Charles B., Feb 26, 2004, in forum: VHDL
    Replies:
    2
    Views:
    649
    Cameron, Charles B.
    Feb 26, 2004
  2. Pete
    Replies:
    7
    Views:
    429
  3. Replies:
    25
    Views:
    710
    George Sakkis
    Jun 11, 2006
  4. Kosio

    Floats to chars and chars to floats

    Kosio, Sep 16, 2005, in forum: C Programming
    Replies:
    44
    Views:
    1,342
    Tim Rentsch
    Sep 23, 2005
  5. order of array subscripts

    , Mar 25, 2008, in forum: C Programming
    Replies:
    9
    Views:
    377
Loading...

Share This Page