Raised This Month: $48 Target: $400
 12% 

Issue with multidimensional string arrays.


Post New Thread Reply   
 
Thread Tools Display Modes
Author Message
r3D w0LF
Senior Member
Join Date: Dec 2013
Location: Albania
Old 09-15-2018 , 17:00   Issue with multidimensional string arrays.
Reply With Quote #1

Hi,
So, i am using a KeyValues file to read contents, and im saving what i get from KvGetSectionName (or KvGetString) to some global arrays, which will then be used in a loop to perform other actions.
However Im stuck with this for the arrays:
Code:
new String:g_SectionName[255]
char g_subSectionValues[255][255];
Here is the code that puts strings in arrays:
Code:
KvGetString(hKeyValues, NULL_STRING, g_subSectionValues[iSection][iSubsectionVal], sizeof(g_subSectionValues));
However when I try and echo/print a value from g_subSectionValues[0][0] (or any other location) it behaves weirdly. When I add another [255] it seems to behave correctly.
Code:
char g_subSectionValues[255][255][255];
However this makes the compiled plugin 10 times bigger (from ~8KB to ~200KB) and increases compilation time aswell.
I could decrease the first 2 dimension sizes, but I still feel like im doing this wrong.
Maybe there is a better way to do this, as I need sectionName to match with subSectionValues as they will be used kinda like identifiers.

And is there any difference between "char str[255]" and "new String:str[255]"?


Here is the cfg file im reading from.
Code:
"Names"
{
	"Section1"
	{
		"a1_1"			"Name 1_1"
		"a1_2"			"Name 1_2"
		"a1_3"			"Name 1_3"
	}
	
	"Section2"
	{
		"a2_1"			"Name 2_1"
		"a2_2"			"Name 2_2"
	}
}

Last edited by r3D w0LF; 09-15-2018 at 17:01.
r3D w0LF is offline
Fyren
FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren FyrenFyrenFyrenFyrenFyren
Join Date: Feb 2106
Old 09-15-2018 , 17:57   Re: Issue with multidimensional string arrays.
Reply With Quote #2

Quote:
Originally Posted by r3D w0LF View Post
Code:
new String:g_SectionName[255]
char g_subSectionValues[255][255];
Here is the code that puts strings in arrays:
Code:
KvGetString(hKeyValues, NULL_STRING, g_subSectionValues[iSection][iSubsectionVal], sizeof(g_subSectionValues));
However when I try and echo/print a value from g_subSectionValues[0][0] (or any other location) it behaves weirdly. When I add another [255] it seems to behave correctly.
Your 2D array, as declared, holds 255 strings that are each 255 characters long. If that's what you want, then calling KvGetString or printing the strings using two indices rather than one is probably not what you want.

Last edited by Fyren; 09-15-2018 at 17:59. Reason: whoops
Fyren is offline
Crasher_3637
AlliedModders Donor
Join Date: May 2012
Old 09-15-2018 , 18:45   Re: Issue with multidimensional string arrays.
Reply With Quote #3

PHP Code:
KvGetString(hKeyValuesNULL_STRINGg_subSectionValues[iSection][iSubsectionVal], sizeof(g_subSectionValues)); 
Should be this:

PHP Code:
KvGetString(hKeyValuesNULL_STRINGg_subSectionValues[iSection], sizeof(g_subSectionValues[])); 
Example:

PHP Code:
// 1D array
char sString[32];
kv.GetString("Key"sStringsizeof(sString), "");

// 2D array
char sString[32][32];
kv.GetString("Key"sString[0], sizeof(sString[]), ""); 
__________________
AFK like JFK
Crasher_3637 is offline
r3D w0LF
Senior Member
Join Date: Dec 2013
Location: Albania
Old 09-16-2018 , 11:12   Re: Issue with multidimensional string arrays.
Reply With Quote #4

I don't think i got it. Here is a visualization of what i want/mean

Main section array:
Code:
array(2) {
  [0]=>
  string(8) "Section1"
  [1]=>
  string(8) "Section2"
}

Subsection array (which corresponds to main array):
Code:
array(2) {
  [0]=>
  array(3) {
    [0]=>
    string(5) "Name1_1"
    [1]=>
    string(5) "Name1_2"
    [2]=>
    string(5) "Name1_3"
  }
  [1]=>
  array(2) {
    [0]=>
    string(5) "Name2_1"
    [1]=>
    string(5) "Name2_2"
  }
}
By adding up, i think the issue is on the first main array, as I have declared it as g_SectionName[255]
Now that im having a cleaner look at the visualization i think it should be
g_SectionName[20][255] where 20 is max number of items in array and 255 max chars in each array item.
So the subarray ends up being g_subSectionName[20][10][255]

And, is there any difference between new String:str[x] or char str[x] ?

Last edited by r3D w0LF; 09-16-2018 at 11:12.
r3D w0LF 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 00:43.


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