3

I'm learning C language, and I'm confused in 2D array pointer.

I have the following declaration

int a[3][5];
int *b[3][3];
int (*c)[3][5];
int *(d[3][5]);
int (*e[3])[5];

could anyone help me clarify

  1. if there're valid declaration or not
  2. the sizeof a,b,c,d,e (assume on 64-bit machine, address id 8 bytes)
  3. what they point to
  4. how to access the element in the 2D array

I'm totally confused about the usage of pointer in 2D array.....and I guess some of them are equivalent...but some might not be good practice

5
  • Possible duplicate of What do parentheses in a C variable declaration mean? Commented Oct 16, 2017 at 21:36
  • 1
    b and d would be the same if b were [3][5] instead of [3][3]. I assume you intended it to be [3][5]? Commented Oct 16, 2017 at 21:38
  • A 2D array is an array of 1D arrays. E.g an array of rows number of 1D arrays. int *b[3][3] is a 2D array of pointers to type int. int (*c)[3][5] is a pointer to a 3x5 array of int int *(d[3][5]); is the same as *d[3][5] and int (*e[3])[5]; is an *array of pointers to 3 int (5 of them) Commented Oct 16, 2017 at 21:43
  • 1
    @DavidC.Rankin No: d is the same as b (except for the 3 vs. 5) - the parentheses have no effect, and e is an array of 3 pointers, which point to arrays of 5 ints. In particular, sizeof(e) is 3*8 = 24. Commented Oct 16, 2017 at 21:45
  • Good catch Tom. Commented Oct 16, 2017 at 21:46

1 Answer 1

3

These

int a[3][5];
int *b[3][5];

(I think you mean indeed int *b[3][5] instead of int *b[3][3] as it is written in your question) two declarations of two-dimensional arrays.

Elements of the first array have type int. Elements of the second array have type int *.

To access elements of the arrays you can use for example subscripting like

a[i][j] or b[i][j] where i is an index in the range [0,3) and j is an index in the range [0, 5).

For the second array to access objects pointed to by the elements of the array you can use expressions like *b[i][j]

sizeof( a ) is equal to 3 * sizeof( int[5] ) that in turn is equal to 3 * 5 * sizeof( int ).

sizeof( b ) is equal to 3 * sizeof( int *[5] ) that in turn is equal to 3 * 5 * sizeof( int *).

This

int (*c)[3][5];

is a declaration of a pointer to a two-dimensional array of the type int[3][5]. You can write for example

int (*c)[3][5] = &a;

where a is the two-dimensional array declared above.

To access elements of the pointed array you can use this syntax

( *c )[i][j]

This

int *(d[3][5]);

a declaration of a two-dimensional array elements of which have type int *. This declaration is equivalent to the declaration shown above that is to

int *b[3][5];

You may enclose declarators in parentheses. So you could even write the declaration of the array d like

int * (((d)[3])[5]);

This

int (*e[3])[5];

is a declaration of an array with 3 elements of pointers to arrays of the type int[5]. Using the typedef

typedef int T[5]; 

the array declaration can be rewritten like

T * e[3];

Here a demonstrative program that shows how elements of the array e can be accessed.

#include <stdio.h>

int main( void )
{
    int a1[5] = { 1, 2, 3, 4, 5 };
    int a2[5] = { 10, 20, 30, 40, 50 };
    int a3[5] = { 100, 200, 300, 400, 500 };

    int(*e[3])[5] = { &a1, &a2, &a3 };

    for (size_t i = 0; i < sizeof( e ) / sizeof( *e ); i++)
    {
        for (int j = 0; j < sizeof(*e[i]) / sizeof(**e[i]); j++)
        {
            printf( "%3d ", ( *e[i] )[j] );
        }
        putchar( '\n' );
    }

    return 0;
}

The program output is

  1   2   3   4   5
 10  20  30  40  50
100 200 300 400 500
Sign up to request clarification or add additional context in comments.

4 Comments

thanks for very detailed explanation! But is there any rules to parse the pointer declaration? I don't want to learn by rote. Maybe for a C expert it's obvious but for me it's hard to understand.
@ZiqiLiu Just substitute the left most array to a pointer. For example int ( a[n1] )[n2][n3]; int ( *p )[n2][n3] = a;
Does this declaration set aside the memory space as well? Does it create space in memory for 3 int array pointers and y number of integers at each pointer? If so, how would this memory be organised?
@etienz There is created an array of three pointers an nothing more.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.