2

I have different sized 2D-arrays and want to store all of them into an array of pointer

int test0[][2] = {{0, 2},
                  {1, 3}};
int test1[][3] = {{10, 20, 30},
                  {40, 50, 60},
                  {70, 80, 90}};

int *t = malloc(2*sizeof(int));

t[0] = (int) test0;
t[1] = (int) test1;

free(t);

So now, the pointers adresses of test1 and test2 are stored in t[0] and t[1] respectively

But I can't access them like t[0][0][0]

Is it possible and how would you do it please?

3
  • 2
    t as declared is a pointer to an integer, not to a 2d array. No wonder the compiler is complaining. Also, maybe you should have listened to it when it lamented that you were trying to assign test0 and test1 to t[0] and t[1] since I'm sure that's the reason you cast them. Commented Sep 30, 2019 at 12:16
  • I tried int ***t = malloc(2*sizeof(int**)) but it won't work neither - I had then t[0] = test0 And I'm not sur if int*** is a real type ? Commented Sep 30, 2019 at 12:17
  • this type of thing only works with jagged arrays. If t[0] is an int**, how would it know the internal array dimensions? Commented Sep 30, 2019 at 12:19

2 Answers 2

4

You can not create an array of objects of different type/size, and in your case the arrays differs in the size.

You need something like:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int test0[][3] = {{0, 2},
                      {1, 3}};
    int test1[][3] = {{10, 20, 30},
                      {40, 50, 60},
                      {70, 80, 90}};

    int (**t)[3] = malloc(sizeof(*t) * 2);

    t[0] = test0;
    t[1] = test1;

    printf("t[1][1][1] = %d\n", t[1][1][1]);

    free(t);

    return 0;
}

Output:

50

But notice that you don't need to use dynamic memory (malloc) :

int (*t[2])[3]; // array 2 of pointer to array 3 of int

will also work.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but I don't understand how it works in the malloc, so t is like t[][3][3]
Are you sure? are you using the exact same code that I'm positng?
I must have made a mistake, the warning is no more. Thanks
1

If you have an array like this

int test0[][2] = { /*...*/ };

then a pointer to elements of the array looks the following way

int ( *p )[2] = test0;

Or

int test1[][3] = { /*...*/ };
int ( *p )[3] = test1;

and you can access elements of the array using the pointer the same way as you do with the array that is for example p[i][j].

Pay attention to that this code snippet

t[0] = (int) test0;
t[1] = (int) test1;

is unsafe and does not make sense. The size of a pointer can be greater than the size of the type int. And moreover the objects t[0] and t[1] are not pointers. They are integers.

You could define an array of pointers of the type void * and then explicitly cast each element of the array to the required type.

For example

#include <stdio.h>
#include <stdlib.h>

int main(void) 
{
    enum { N0 = 2, N1 = 3 };
    int test0[N0][N0] = 
    { 
        { 0, 2 }, 
        { 1, 3 }
    };

    int test1[N1][N1] = 
    {
        { 10, 20, 30 },
        { 40, 50, 60 },
        { 70, 80, 90 }
    };

    void **t = malloc( 2 * sizeof( void * ) );
    t[0] = test0;
    t[1] = test1;

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

    putchar( '\n' );

    for ( size_t i = 0; i < N1; i++ )
    {
        for ( size_t j = 0; j < N1; j++ )
        {
            printf( "%d ", ( ( int ( * )[N1] )t[1] )[i][j] );
        }
        putchar( '\n' );
    }

    putchar( '\n' );

    free( t );

    return 0;
}

2 Comments

In int ( *p )[3] = test1; are the parenthesis mandatory ? Because I tried something similar to this !
@Newtonus This a declaration of a pointer to elements of the type int[3]. Without the parentheses int *p[3] is an array of objects of the type int *. So these are two different types.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.