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: 380 Reputation: 11 sizeof 2D array Hi guys, How can I calculate, how many numbers is in 2D array element? Or it's impossible? 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: Code: `printf( "%d", sizeof( array1[ 0 ] ) );` Thanks!
 04/07/2012, 12:57 PM #2 Vince Spam Machine     Join Date: Sep 2007 Location: Belgium Posts: 11,005 Reputation: 2647 Re: sizeof 2D array 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: 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 } ;``` __________________ [Tutorial] Database normalization: Vehicle components - Weapon data - GeoIP and rangebans - Association tables Useful: How NOT to store passwords
 04/07/2012, 01:09 PM #3 zgintasz Huge Clucker   Join Date: Oct 2011 Posts: 380 Reputation: 11 Re: sizeof 2D array 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: 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
Spam Machine

Join Date: Sep 2007
Location: Belgium
Posts: 11,005
Reputation: 2647
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.
__________________

 04/07/2012, 01:19 PM #5 zgintasz Huge Clucker   Join Date: Oct 2011 Posts: 380 Reputation: 11 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 Spam Machine     Join Date: Sep 2007 Location: Belgium Posts: 11,005 Reputation: 2647 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. 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: 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]) );``` __________________ [Tutorial] Database normalization: Vehicle components - Weapon data - GeoIP and rangebans - Association tables Useful: How NOT to store passwords
 04/07/2012, 01:33 PM #7 zgintasz Huge Clucker   Join Date: Oct 2011 Posts: 380 Reputation: 11 Re: sizeof 2D array Thanks! It works fine if I put 0 in the element end. Is it possible without 0(zero)? 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 : 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```
 05/07/2012, 08:41 AM #8 zgintasz Huge Clucker   Join Date: Oct 2011 Posts: 380 Reputation: 11 Re: sizeof 2D array 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 ] )?
05/07/2012, 09:35 AM   #9
Y_Less
Spam Machine

Join Date: Jun 2008
Location: 629 - git.io/Y
Posts: 14,666
Reputation: 3061
Re: sizeof 2D array

Quote:
 Originally Posted by Vince 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: 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.
__________________
 | Worst. Exit. Ever. |
 | git.io/Y | y-less.com | 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 12:27 AM Roko_foko Help Archive 2 11/07/2011 03:55 PM Deji Help Archive 4 22/01/2011 06:56 PM LarzI Help Archive 2 22/12/2009 04:32 AM UsaBoy91 Help Archive 1 13/10/2009 06:01 PM

All times are GMT. The time now is 10:41 PM.

 -- (EN) English -- (RU) Русски Contact Us - SA-MP - Archive - Top