Array of pointers, pointer to array... 
Author Message
 Array of pointers, pointer to array...

I've figured out that "char *(foo[16])" declares an array of 16 char
pointers, and "char (*foo)[16]" declares a pointer to an array of 16 chars,
but does this not seem logical to anyone else?  Thanks for any input.

Art



Mon, 10 Jul 2000 03:00:00 GMT  
 Array of pointers, pointer to array...

Quote:

>I've figured out that "char *(foo[16])" declares an array of 16 char
>pointers, and "char (*foo)[16]" declares a pointer to an array of 16
>chars, but does this not seem logical to anyone else?  Thanks for any
>input.

The first `char *(foo[16])' has superfluous parentheses.  It is more
common to use `char *foo[16]'.  The FAQ explains how to form and read
complicated declarations inside out.
(See question 1.21 at http://www.eskimo.com/~scs/C-faq/top.html).

Here's a snippet I happened to have lying around.  Maybe it will help.
Maybe it will be hopelessly confusing.

For extra credit, why did I use a macro for `show()' in the program
instead of defining a function?

/*
 * Description:
 *     Consider (1) a simple variable A, (2) a two-dimensional array
 *     B[2][2], and (3) a four-dimensional array C[2][2][2][2] of
 *     double.  Write declarations of A, B, and C so that element
 *     C[i][j][k][l] may be accessed by expressions A[i][j][k][l] and
 *     B[i][j][k][l].  Explain, in detail, how A[i][j][k][l] and
 *     B[i][j][k][l] access C[i][j][k][l].
 */

#include <stdio.h>

/*
 * Declarations:
 *     Let A be a pointer (which is a simple variable) to an array of
 *     2 pointer to an array of 2 double.
 *     Let B be an array of 2 of an array of 2 pointer to an array of
 *     2 double.
 */

static double C[2][2][2][2] = { { { { 1.0,  2.0 }, { 3.0,  4.0 } },
                                  { { 5.0,  6.0 }, { 7.0,  8.0 } } },
                                { { { 9.0, 10.0 }, {11.0, 12.0 } },
                                  { {13.0, 14.0 }, {15.0, 16.0 } } } };

static double (*B[2][2])[2] = { { C[0][0], C[0][1] }, { C[1][0], C[1][1] } };

static double (*(*A)[2])[2] = B;

#define show(D)\
do {\
  int i, j, k, l;\
  for(i = 0; i < 2; i++)\
    for(j = 0; j < 2; j++)\
      for(k = 0; k < 2; k++)\
        for(l = 0; l < 2; l++)\
          printf("%3.0f",D[i][j][k][l]);\
  printf("\n");\

Quote:
} while(0)

int main (void)
{
  printf("A = "); show(A);
  printf("B = "); show(B);
  printf("C = "); show(C);

  return(0);

Quote:
}

/*
 * Explanation:
 * double (*(*A)[2])[2] => A is a pointer to an array 2 of pointer to an
 *                         array 2 of double. (a pointer is a simple
 *                         variable)
 *
 * double (*B[2][2])[2] => B is an array 2 of an array 2 of pointer to an
 *                         array 2 of double. (2x2 array of pointers)
 *
 * double C[2][2][2][2] => C is an array 2 of an array 2 of an array 2 of
 *                         an array 2 of double. (2x2x2x2 array of double)
 *
 * Pictorially, the initializations are:
 *
 *     +---+
 *   A | X |
 *     +---+
 *       |
 *       !
 *     +---|---+ +---|---+
 *   B | X | X | | X | X |
 *     +---|---+ +---|---+
 *       |   |     |   `-----------------------------------.
 *       |   |     `-----------------------.               |
 *       |   `-----------.                 |               |
 *       |               |                 |               |
 *       !               !                 !               !
 *     +---+---:---+---|---+---:---+---+ +---+---:---+---|---+---:---+---+
 *   C | 1 | 2 : 3 | 4 | 5 | 6 : 7 | 8 | | 9 |10 :11 |12 |13 |14 :15 |16 |
 *     +---+---:---+---|---+---:---+---+ +---+---:---+---|---+---:---+---+
 *
 *   Notice that B and C are represented as two groups with gaps between
 *   them.  Both B and C are allocated contiguous storage in memory.
 *   The groups represent the first level of dereference.  B[0] and C[0]
 *   represent the first group.  B[1] and C[1] takes us to the second
 *   group.  The second level of dereference is indicated with three |'s
 *   in a consecutive vertical direction.  The third is indicated with
 *   three vertical :'s.  So, for example, C[0][1][1][0] == 7, and
 *   C[1][0][1][1] == 12.
 *
 *   Now, notice that:
 *   (1) A points to an array of 2 pointers to an array of 2 doubles.
 *       B is an array of 2 arrays of 2 pointers to an array of 2
 *       doubles.  Thus, A[0], B[0], A[1], B[1] all have the same type.
 *       Furthermore, A[0] == B[0], and A[1] == B[1], since A == B.
 *   (2) Since, B[i] is an array of 2 pointers to an array of 2 doubles,
 *       for i in {0,1}, B[i][j] is a pointer to an array of 2 doubles.
 *       Thus, B[i][j][k] and C[i][j][k] have the same type and are equal
 *       for i,j,k in {0,1}, since B[i][j] == C[i][j].
 *   (3) This, A[i][j][k][l] equals B[i][j][k][l] equals C[i][j][k][l]
 *       for i,j,k,l in {0,1}.
 */

--

http://www.cs.wustl.edu/~jxh/        Washington University in Saint Louis

Quote:
>>>>>>>>>>>>> I use *SpamBeGone* <URL:http://www.internz.com/SpamBeGone/>



Tue, 11 Jul 2000 03:00:00 GMT  
 
 [ 2 post ] 

 Relevant Pages 

1. Dereferencing f-pointers, arrays of f-pointers, pointers to f-pointers

2. array pointer/pointer array

3. arrays pointers array of pointers

4. Pointer of Pointers was Pointer of arrays...

5. Pointers: return of pointer to array of pointers to main

6. Pointer to array of pointers

7. pointers to pointers to arrays

8. Pointer to an array of pointers to functions

9. limit on pointer to array of pointers?

10. Pointer to Array of Pointers to Strings.

11. pointer to array vs pointer

12. pointers to an array and accesing elements of the array

 

 
Powered by phpBB® Forum Software