AlliedModders

AlliedModders (https://forums.alliedmods.net/index.php)
-   Scripting Help (https://forums.alliedmods.net/forumdisplay.php?f=11)
-   -   Sizeof 3 dimensional arrays (https://forums.alliedmods.net/showthread.php?t=274346)

yan1255 11-06-2015 11:18

Sizeof 3 dimensional arrays
 
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 ?

Bugsy 11-06-2015 19:57

Re: Sizeof 3 dimensional arrays
 
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



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

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