Cast to pointer to multidimensional array

Discussion in 'C Programming' started by Szabolcs Borsanyi, Jun 6, 2008.

  1. Dear All,

    there have been several threads on multidimensional arrays and pointers
    to arrays, there is still something I could not fully understand.
    (My point here I have raised already, but stayed unanswered)

    Let's have an array.

    float A[256];

    And you would like to consider this as 32x8 matrix.
    You can of course do A[32*i+j] for the (i,j)-th element, but it is
    much more convenient to put it into an array form:

    float (*B)[8]=(void*)A;

    The (void*) here is merely to shut up the compiler (ie. to fulfil the language
    constraints). B is a pointer to the first one of a series (float[8]) arrays.

    Then, after using this layout of the data, I realise that I need an other
    matrix format: 16x16, so I write.

    float (*C)[16]=(void*)A;

    So far there was no cast to a pointer to multidimensional array,
    but you could actually want a 4x4x4 tensor and declare:

    float (*D)[4][4]=(void*)A;

    Is there any undefined behaviour in these casts or the subsequent access
    to the members B[j] and C[j] or D[j][k] (i,j,k within bounds)?

    Final question:

    If my initial array is given by a restricted pointer to its first element,
    how can I legally proceed to do these casts?

    Szabolcs
     
    Szabolcs Borsanyi, Jun 6, 2008
    #1
    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. shane
    Replies:
    2
    Views:
    8,294
    shane
    Jun 4, 2004
  2. Kobu
    Replies:
    4
    Views:
    473
    CBFalconer
    Jan 19, 2005
  3. c19h28o2

    Multidimensional array pointer problem

    c19h28o2, Sep 26, 2006, in forum: C Programming
    Replies:
    6
    Views:
    478
    Barry Schwarz
    Sep 28, 2006
  4. Szabolcs Borsanyi
    Replies:
    6
    Views:
    665
    Ben Bacarisse
    May 23, 2008
  5. Hansen
    Replies:
    3
    Views:
    1,106
    rep_movsd
    Apr 24, 2010
Loading...

Share This Page