Raised This Month: $235 Target: $400
 58% 

String array problem


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
pan0s
Member
Join Date: Nov 2017
Old 04-28-2021 , 12:45   String array problem
Reply With Quote #1

Hi, I want to ask how to declare a 2D string array with initial values on SoucePawn?

on Java, it should be like that
Code:
String[][] g_sItems = 
{
	{"item1",},
	{"item2","item3",}
};
on SoucePawn, I tried

Code:
char g_sItems[][][] = 
{
	{"item1",},
	{"item2","item3",}
};
but the compiler gives me "error 018: initialization data exceeds declared size."

Last edited by pan0s; 04-28-2021 at 13:04.
pan0s is offline
MAGNAT2645
Senior Member
Join Date: Nov 2015
Location: AlliedMods.net
Old 04-28-2021 , 13:53   Re: String array problem
Reply With Quote #2

Code:
char g_sItems1[][] = {
    "ABC",
    "DEF",
    "123",
    "456"
};

char g_sItems2[][][] = {
    { "ABC", "DEF" },
    { "123", "456" },
    { "789", "QWE" }
};

public void OnPluginStart()
{
    for ( int i = 0; i < sizeof g_sItems1; i++ )
        PrintToServer( "%s", g_sItems1[i] );

    PrintToServer( "---" );

    for ( int i = 0, j; i < sizeof g_sItems2; i++ ) {
        for ( j = 0; j < sizeof g_sItems2[]; j++ )
            PrintToServer( "%s", g_sItems2[i][j] );
    }
}
Note that each row in 2D string array must have same number of strings (that's due to how compiler works).
If you want to have, for example, 2 strings in 1st row and 4 strings in 2nd row you can just put empty strings:
Code:
char g_sItems2[][][] = {
    { "ABC", "DEF", "",    ""    },
    { "123", "456", "GGG", "RRR" }
};
__________________

Last edited by MAGNAT2645; 04-28-2021 at 13:59.
MAGNAT2645 is offline
pan0s
Member
Join Date: Nov 2017
Old 04-28-2021 , 15:19   Re: String array problem
Reply With Quote #3

Quote:
Originally Posted by MAGNAT2645 View Post
Code:
char g_sItems1[][] = {
    "ABC",
    "DEF",
    "123",
    "456"
};

char g_sItems2[][][] = {
    { "ABC", "DEF" },
    { "123", "456" },
    { "789", "QWE" }
};

public void OnPluginStart()
{
    for ( int i = 0; i < sizeof g_sItems1; i++ )
        PrintToServer( "%s", g_sItems1[i] );

    PrintToServer( "---" );

    for ( int i = 0, j; i < sizeof g_sItems2; i++ ) {
        for ( j = 0; j < sizeof g_sItems2[]; j++ )
            PrintToServer( "%s", g_sItems2[i][j] );
    }
}
Note that each row in 2D string array must have same number of strings (that's due to how compiler works).
If you want to have, for example, 2 strings in 1st row and 4 strings in 2nd row you can just put empty strings:
Code:
char g_sItems2[][][] = {
    { "ABC", "DEF", "",    ""    },
    { "123", "456", "GGG", "RRR" }
};
Well, I have this idea too. However, I rejected that idea in a second.
When the length of the max is extended, I have to add the empty string for each array to match the size. If I have 100 arrays of it, I have to do it 100 times...
In this stage, I have two better ideas.

Code:
// --First way is split the 2D string array to 1D string array, and pushing it into array list.
char g_sMelees[][] = 
{                       			// Melee ID:
	"katana",           		// 0
	"fireaxe",          			// 1
	"machete",          		// 2
	"flamethrower",			// 3	
	"knife",            			// 4
	"chainsaw",         		// 5
	"pitchfork",			// 6
	"shovel",				// 7
	"golfclub",				// 8
	"electric_guitar",  		// 9
	"tonfa",            			// 10
	"baseball_bat",     		// 11
	"cricket_bat",      		// 12
	"frying_pan",       		// 13
	"crowbar",          		// 14
};

ArrayList g_listItems[SHOP_SIZE];

enum 
{      ...
	SHOP_MELEES					= 8,
        ...
	SHOP_SIZE					= 17,
};

public void OnPluginStart()
{
	for(int i=0; i<SHOP_SIZE; i++)
		g_listItems[i] = new ArrayList(ByteCountToCells(32));

	// melee weapons
	for(int i=0; i<sizeof(g_sMelees); i++) g_listItems[SHOP_MELEES].PushString(g_sMelees[i]);

	// --Second way, don't use the char array to declare, pushing the value to array list instead of it
	g_listItems[SHOP_MELEES].PushString("katana");
	g_listItems[SHOP_MELEES].PushString("fireaxe");
        //...
}
pan0s is offline
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

Forum Jump


All times are GMT -4. The time now is 23:25.


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