Raised This Month: $ Target: $400
 0% 

Sizeof 3 dimensional arrays


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
yan1255
Senior Member
Join Date: Jul 2011
Old 11-06-2015 , 11:18   Sizeof 3 dimensional arrays
Reply With Quote #1

I am using 3d arrays and i am trying to find the maximum size of a certain value inside of them but I keep getting errors when I am using 'sizeof'

Code:
#include < amxmodx >

enum _: enumfirst
{
	enum1,
	enum2,
	enum3,
	enum4
};

enum _: enumsecond
{
	enumone,
	enumtwo,
	enumthree
};

new szHello[ enumfirst ][ enumsecond ][ ] =
{
	{ "one",	{ 1, 2, 4 }, { 3,4 } },
	{ "two",	{ 1, 2, 4 }, { 3,4 } },
	{ "three",	{ 1, 2, 4 }, { 3,4 } },
	{ "four",	{ 1, 2, 4 }, { 3,4 } }
};

public plugin_init()
{
	register_plugin( "Plugin", "1.0", "Rejack" );
	
	register_clcmd( "say /max", "CmdMax" );
}

public CmdMax( const index )
{
	client_print( index, print_chat, "Sizeof: %i", sizeof szHello[ enum1 ][ enumtwo ] );
	
	return PLUGIN_HANDLED;
}
Code:
Error: Expected token: "]", but found "-identifier-" on line 35
Warning: Expression has no effect on line 35
Error: Expected token: ";", but found "]" on line 35
Error: Invalid expression, assumed zero on line 35
Error: Too many error messages on one line on line 35
How can I check the size of the value ?
__________________

Last edited by yan1255; 11-06-2015 at 12:28.
yan1255 is offline
Bugsy
AMX Mod X Moderator
Join Date: Feb 2005
Location: NJ, USA
Old 11-06-2015 , 19:57   Re: Sizeof 3 dimensional arrays
Reply With Quote #2

You need to be careful when you define arrays using an enumerator. In your case you did not define what you think you did and you will sometimes see no compile errors, your code will just not do what you expect.

See if this helps:
PHP Code:

#include < amxmodx >

enum _enumfirst
{
    
enum1,
    
enum2,
    
enum3,
    
enum4
};

enum _enumsecond
{
    
enumone],  //sized at 6 to hold the largest possible string, in this case 'three'
    
enumtwo],  //sized to hold 3 numbers
    
enumthree//sized to hold 2 numbers
};

new 
szHelloenumfirst ][ enumsecond ] =
{
    { 
"one"       , { 12}    , { 4,} },
    { 
"two"       , { 67}    , { 9,10 } },
    { 
"three"  , { 111213 } , { 14,15 } },
    { 
"four"   , { 161718 } , { 19,20 } }
};

public 
plugin_init()
{
    for ( new 
enumfirst i++ )
    {
        
server_print"String: %s" szHello][ enumone ] );
        
server_print"int: %d %d %d" szHello][ enumtwo ][ ] , szHello][ enumtwo ][ ]  , szHello][ enumtwo ][ ] );
        
server_print"int: %d %d" szHello][ enumthree ][ ] , szHello][ enumthree ][ ] );
    }
    
    
server_print"sizeof main array: %d" sizeofszHello ) );
    
    
server_print"sizeof enumone in enumsecond: %d" sizeofszHello[][ enumone ] ) );
    
server_print"sizeof enumtwo in enumsecond: %d" sizeofszHello[][ enumtwo ] ) );
    
server_print"sizeof enumthree in enumsecond: %d" sizeofszHello[][ enumthree ] ) );

    return 
PLUGIN_HANDLED;

Output
Code:
String: one
int: 1 2 3
int: 4 5
String: two
int: 6 7 8
int: 9 10
String: three
int: 11 12 13
int: 14 15
String: four
int: 16 17 18
int: 19 20
sizeof main array: 4
sizeof enumone in enumsecond: 6
sizeof enumtwo in enumsecond: 3
sizeof enumthree in enumsecond: 2
__________________

Last edited by Bugsy; 11-06-2015 at 20:20.
Bugsy is offline
Reply



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 Jump


All times are GMT -4. The time now is 18:14.


Powered by vBulletin®
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Theme made by Freecode