SA-MP Forums sizeof 2D array
 New Account Members List Search Today's Posts Mark Forums Read

 04/07/2012, 12:40 PM #1 zgintasz Huge Clucker   Join Date: Oct 2011 Posts: 394 Reputation: 8 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!
 04/07/2012, 12:57 PM #2 Vince Godfather     Join Date: Sep 2007 Location: Belgium Posts: 6,505 Reputation: 1694 Re: sizeof 2D array pawn Code: `sizeof(array) // First dimensionsizeof(array[]) // second dimensionsizeof(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    };` __________________ I will rarely post a ready made solution in your thread, but rather I try to make you realize your own mistakes so you can fix them yourself. The average intellect of the average SA-MP forums user.
 04/07/2012, 01:09 PM #3 zgintasz Huge Clucker   Join Date: Oct 2011 Posts: 394 Reputation: 8 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++ ){    //...}`
04/07/2012, 01:17 PM   #4
Vince
Godfather

Join Date: Sep 2007
Location: Belgium
Posts: 6,505
Reputation: 1694
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 In your case, the second dimension would always return 9, though, because that's the highest number of cells.
__________________
I will rarely post a ready made solution in your thread, but rather I try to make you realize your own mistakes so you can fix them yourself.
The average intellect of the average SA-MP forums user.

 04/07/2012, 01:19 PM #5 zgintasz Huge Clucker   Join Date: Oct 2011 Posts: 394 Reputation: 8 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 .
 04/07/2012, 01:27 PM #6 Vince Godfather     Join Date: Sep 2007 Location: Belgium Posts: 6,505 Reputation: 1694 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 elementfor( 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]) );` __________________ I will rarely post a ready made solution in your thread, but rather I try to make you realize your own mistakes so you can fix them yourself. The average intellect of the average SA-MP forums user.
 04/07/2012, 01:33 PM #7 zgintasz Huge Clucker   Join Date: Oct 2011 Posts: 394 Reputation: 8 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 mismatchprintf( "%d", strlen( array1[ 1 ] ) ); // tag mismatchprintf( "%d", strlen( array1[ 2 ] ) ); // tag mismatch`
 05/07/2012, 08:41 AM #8 zgintasz Huge Clucker   Join Date: Oct 2011 Posts: 394 Reputation: 8 Re: sizeof 2D array pawn Code: `// arraynew    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    };// functionstock len( Float:array[ ] ){    new        i = 0,        count    ;    while( array[ i ] != 0.0 )    {        count++;        i++;    }    return count;}// printingprintf( "%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 ] )?
05/07/2012, 09:35 AM   #9
Y_Less
Beta Tester

Join Date: Jun 2008
Location: 629 - git.io/Y
Posts: 17,543
Reputation: 2438
Re: sizeof 2D array

Quote:
 Originally Posted by Vince pawn Code: `sizeof(array) // First dimensionsizeof(array[]) // second dimensionsizeof(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.
__________________
 | YSI 3.1 | YSI 4.0 beta | sscanf | fixes.inc | | git.io/Y | y-less.com | #YSI | jump.pk |

 Thread Tools Display Modes Linear Mode

 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 Forum Rules

 Similar Threads Thread Thread Starter Forum Replies Last Post Crystal_Speed Русский/Russian 16 18/11/2011 01:27 AM Roko_foko Help Archive 2 11/07/2011 03:55 PM Deji Help Archive 4 22/01/2011 07:56 PM LarzI Help Archive 2 22/12/2009 05:32 AM UsaBoy91 Help Archive 1 13/10/2009 06:01 PM

All times are GMT. The time now is 11:55 AM.

 -- (DE) Deutsch -- (EN) English -- (ES) Espaсol -- (FR) Franзais -- (NL) Dutch -- (RU) Русски Contact Us - SA-MP - Archive - Top