2

I'm using a library that creates a multidimensional array in this way:

const unsigned short arrayA[5] = {1, 2, 3, 4, 5};
const unsigned short arrayB[3] = {7, 8, 9};

const unsigned short *multiArray[] ={arrayA, arrayB};

I get the values of it and it works:

printf("03: %d\n", multiArray[0][3]); //4
printf("12: %d\n", multiArray[1][2]); //9

The problem comes when I need to get the size of any of the arrays, I've tried this:

printf("Size of first array: %ld \n", sizeof(multiArray[0])/sizeof(multiArray[0][0]));

It returns 2, probably because it's using addresses.

How can I get the size of the array then?

I have to do this trying not to change the way the arrays are declared, so the arrays are already there and I just need to get the size.

Any approaches?

Antonio MG
  • 20,382
  • 3
  • 43
  • 62

6 Answers6

4

It is not possible to deduce the size of the arrays pointed to by the pointers in multiArray since there is no way of performing any computations as the two arrays (arrayA and arrayB) will be stored in arbitrary locations. A good practice is to store the size of the arrays.

Ifthikhan
  • 1,484
  • 10
  • 14
2

You can't. You have to know the size of array in advance and pass it as additional argument.

aragaer
  • 17,238
  • 6
  • 47
  • 49
2

If you do sizeof on a pointer, you get the size of the actual pointer and not what it points to. You have to keep track of the size of the contained array some other way.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • @AntonioMG You can't. As soon as you as you loose track of the original array (either by using a pointer to it, or by passing it to a function, etc.) the compiler also loose track of the meta-data it has for the array, like its size. – Some programmer dude Feb 13 '13 at 09:50
2

It is possible to obtain both the amount of rows and columns of a matrix:

int main(void) {
     int row;
     int column;
     int matrix[4][5]={{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5},{1,2,3,4,5}};

     row=sizeof(matrix)/sizeof(matrix[0]); 
     column=sizeof(matrix[0])/row;

     printf("row %i \n",row);
     printf("column %i \n",column);
     return 0;
}
Mark Lapasa
  • 1,644
  • 4
  • 19
  • 37
edgar
  • 21
  • 1
1

You cant! you should hold this data and pass it to wherever you need.

BTW when you do this: sizeof(multiArray[0])/sizeof(multiArray[0][0])
you basically do sizeof(<pointer>)/sizeof(<unsigned short>) => 2 on your specific machine.

Roee Gavirel
  • 18,955
  • 12
  • 67
  • 94
1

As other have said, you can not, instead you can build your own type:

#include <stdio.h>

struct cont {
    const unsigned short *value;
    size_t len;
};

int main(void)
{
    const unsigned short arrayA[5] = {1, 2, 3, 4, 5};
    const unsigned short arrayB[3] = {7, 8, 9};

    struct cont arrays[] = {
        {arrayA, sizeof(arrayA) / sizeof(arrayA[0])},
        {arrayB, sizeof(arrayB) / sizeof(arrayB[0])},
    };

    printf("03: %d\n", arrays[0].value[3]);
    printf("12: %d\n", arrays[1].value[2]);


    printf("Size of first array: %ld \n", arrays[0].len);

    return 0;
}
David Ranieri
  • 39,972
  • 7
  • 52
  • 94