sizeof 2D array
#1

Hi guys,

How can I calculate, how many numbers is in 2D array element? Or it's impossible?
pawn Code:
new
    array1[ ][ ] = {
        { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, // there is 9 numbers
        { 1, 2, 3, 4, 5 }, // there is 5 numbers
        { 1, 2, 3, 4, 5, 6 }, // there is 6 numbers
    }
;
If I use sizeof, I get errors:
pawn Code:
printf( "%d", sizeof( array1[ 0 ] ) );
Thanks!
Reply
#2

pawn Code:
sizeof(array) // First dimension
sizeof(array[]) // second dimension
sizeof(array[][]) // third dimension
In your case, the second dimension would always return 9, though, because that's the highest number of cells.
Basically, your code will compile as:

pawn Code:
new
    array1[3][9] = {
        { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, // there is 9 numbers
        { 1, 2, 3, 4, 5, 0, 0, 0, 0 }, // there is 5 numbers
        { 1, 2, 3, 4, 5, 6, 0, 0, 0 }, // there is 6 numbers
    }
;
Reply
#3

pawn Code:
new
    array1[ 3 ][ 9 ] = {
        { 1, 2, 3, 4, 5, 6, 7, 8, 9 }, // there is 9 numbers
        { 1, 2, 3, 4, 5, 0, 0, 0, 0 }, // there is 5 numbers
        { 1, 2, 3, 4, 5, 6, 0, 0, 0 }, // there is 6 numbers
    }
;
printf( "%d", sizeof( array1 ) );
printf( "%d", sizeof( array1[] ) );
printf( "%d", sizeof( array1[][] ) );
It prints 3, 9 and 1, but should print 9, 5 and 6. But if it'll return correct numbers, I can't use this. This is what I want to make:

pawn Code:
new
    index = 0,
    count
;
count = /* somehow count how many numbers there is in element in array. The best way would be "sizeof( array1[ index ]", but it gives errors */
for( new i; i < count; i++ )
{
    //...
}
Reply
#4

No, no, you don't get it. sizeof(array[]) returns the size of the second dimension, not the second element!

Quote:
Originally Posted by Vince
View Post
In your case, the second dimension would always return 9, though, because that's the highest number of cells.
Reply
#5

I understand this, but I don't need the size of the second dimension. I wrote what I want to make, read a little bit above .
Reply
#6

Hmm, in that case you should be fine with strlen, I suppose. Should work as long as you don't have any 0 somewhere in the middle of your element (e.g. 1, 2, 0, 3) as that is regarded as the null terminator and will affect the return of strlen.

pawn Code:
count = strlen(array1[index]); // second element
for( new i; i < count; i++ )
{
    //...
}
Edit: Just tested that, but it does require a null terminator. This will produce the output you're looking for:

pawn Code:
new array1[][] = {
        { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }, // there is 9 numbers
        { 1, 2, 3, 4, 5, 0 }, // there is 5 numbers
        { 1, 2, 3, 4, 5, 6, 0 } // there is 6 numbers
    };
   
    printf( "%d", strlen(array1[0]) );
    printf( "%d", strlen(array1[1]) );
    printf( "%d", strlen(array1[2]) );
Reply
#7

Thanks! It works fine if I put 0 in the element end. Is it possible without 0(zero)?
pawn Code:
new
    array1[ ][ ] = {
        { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }, // there is 9 numbers
        { 1, 2, 3, 4, 5, 0 }, // there is 5 numbers
        { 1, 2, 3, 4, 5, 6, 0 } // there is 6 numbers
    }
;
EDIT:
if the array is with Float tag, strlen gives tag mismatch :
pawn Code:
new
    Float:array1[ ][ ] = {
        { 1.1, 9.8, 2.5, 0.0 }, // there is 3 numbers
        { 1.6, 5.1, 0.0 }, // there is 2 numbers
        { 1.2, 5.3, 6.0, 5.3, 0.0 } // there is 4 numbers
    }
;
printf( "%d", strlen( array1[ 0 ] ) ); // tag mismatch
printf( "%d", strlen( array1[ 1 ] ) ); // tag mismatch
printf( "%d", strlen( array1[ 2 ] ) ); // tag mismatch
Reply
#8

pawn Code:
// array
new
    Float:array1[ ][ ] = {
        { 1.1, 9.8, 2.5, 0.0 }, // there is 3 numbers
        { 1.6, 5.1, 0.0 }, // there is 2 numbers
        { 1.2, 5.3, 6.0, 5.3, 0.0 } // there is 4 numbers
    }
;
// function
stock len( Float:array[ ] )
{
    new
        i = 0,
        count
    ;
    while( array[ i ] != 0.0 )
    {
        count++;
        i++;
    }
    return count;
}
// printing
printf( "%d", len( array1[ 0 ] ) );
It works perfect, but I think there should be a better solution. This is unoptimized, because I should store the lenght in another array in the first time checking, then check if it was checked and if it was, return lenght from another array to don't use while loop again . Why in PAWN sizeof can't simply work like sizeof( array[ 0 ] )?
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)