SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help

Reply
 
Thread Tools Display Modes
Old 04/07/2012, 12:40 PM   #1
zgintasz
Huge Clucker
 
Join Date: Oct 2011
Posts: 394
Reputation: 8
Default sizeof 2D array

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!
zgintasz is offline   Reply With Quote
Old 04/07/2012, 12:57 PM   #2
Vince
Godfather
 
Vince's Avatar
 
Join Date: Sep 2007
Location: Belgium
Posts: 6,957
Reputation: 1824
Default Re: sizeof 2D array

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
    }
;
__________________

Won't reply to your topic if you use outdated methods (strtok, dini, non-threaded MySQL).
Vince is offline   Reply With Quote
Old 04/07/2012, 01:09 PM   #3
zgintasz
Huge Clucker
 
Join Date: Oct 2011
Posts: 394
Reputation: 8
Default Re: sizeof 2D array

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++ )
{
    //...
}
zgintasz is offline   Reply With Quote
Old 04/07/2012, 01:17 PM   #4
Vince
Godfather
 
Vince's Avatar
 
Join Date: Sep 2007
Location: Belgium
Posts: 6,957
Reputation: 1824
Default Re: sizeof 2D array

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.
__________________

Won't reply to your topic if you use outdated methods (strtok, dini, non-threaded MySQL).
Vince is offline   Reply With Quote
Old 04/07/2012, 01:19 PM   #5
zgintasz
Huge Clucker
 
Join Date: Oct 2011
Posts: 394
Reputation: 8
Default Re: sizeof 2D array

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 .
zgintasz is offline   Reply With Quote
Old 04/07/2012, 01:27 PM   #6
Vince
Godfather
 
Vince's Avatar
 
Join Date: Sep 2007
Location: Belgium
Posts: 6,957
Reputation: 1824
Default Re: sizeof 2D array

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]) );
__________________

Won't reply to your topic if you use outdated methods (strtok, dini, non-threaded MySQL).
Vince is offline   Reply With Quote
Old 04/07/2012, 01:33 PM   #7
zgintasz
Huge Clucker
 
Join Date: Oct 2011
Posts: 394
Reputation: 8
Default Re: sizeof 2D array

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
zgintasz is offline   Reply With Quote
Old 05/07/2012, 08:41 AM   #8
zgintasz
Huge Clucker
 
Join Date: Oct 2011
Posts: 394
Reputation: 8
Default Re: sizeof 2D array

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 ] )?
zgintasz is offline   Reply With Quote
Old 05/07/2012, 09:35 AM   #9
Y_Less
Beta Tester
 
Y_Less's Avatar
 
Join Date: Jun 2008
Location: 629 - git.io/Y
Posts: 18,445
Reputation: 2601
Default Re: sizeof 2D array

Quote:
Originally Posted by Vince View Post
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
    }
;
Because of this. You can't have different elements of the same dimension with different numbers of elements - they will all get padded to the same size. So doing "sizeof (array1[1])" will return 9, not 5, even though you only explicitly specified 5 of the numbers - the rest will be 0.
Y_Less is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Вопрос о sizeof() Crystal_Speed Русский/Russian 16 18/11/2011 01:27 AM
sizeof/strlen Roko_foko Help Archive 2 11/07/2011 03:55 PM
sizeof in multi-dimensional array - error? Deji Help Archive 4 22/01/2011 07:56 PM
[SOLVED]warning 224: indeterminate array size in "sizeof" expression (symbol "") LarzI Help Archive 2 22/12/2009 05:32 AM
How to read the 2 sizeof ? UsaBoy91 Help Archive 1 13/10/2009 06:01 PM


All times are GMT. The time now is 01:36 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.