Inserting a string variable into an array (ERROR 006)
#1

Hi, I am having a little bit of trouble (much trouble) and I need some help.

Basically, I need to add a string variable to an array and I can't, it always gives me errors. I use the default Pawno BTW.

Here is an example of a script that gives me errors, for example:


main()
{
new array[5];
array[0] = "I AM HAPPY TO SEE YOU";
print(array[0]);
}


I am sorry if I posted in the wrong section, it is the first time I post on this forum and I wish you, the reader, all the best from me.
Reply
#2

That is simply not possible. Strings are stored in arrays; each character is an index.

pawn Код:
new string[6] = "Hello";

string[0] = 'H';
string[1] = 'e';
string[2] = 'l';
string[3] = 'l';
string[4] = 'o';
string[5] = 0; // Null terminator (can be represented with '\0')
If you want an array of strings, you have to do something like this:

pawn Код:
new strings[5][16] = { // 5 strings with UP TO 15 characters each (+1 for null terminator)
"example",
"example",
"example",
"example",
"example"
};
Then you could display the first string as 'strings[0]. But even then you can't use the assignment operator (x = y) to modify an array unless it is the same size. You'd have to use format().
Reply
#3

@MP2: Don't lie to the kid, he just needs to enumerate it!

enum _Enum_
{
string[64],
int1,
int2,
Float:float1,
Float:float2
}
main()
{
new array[_Enum_];
format(array[string], sizeof array[string], "I AM HAPPY TO SEE YOU %s", "BITCH");
array[int1] = 666;
array[int2] = 777;
array[float1] = 676.0;
array[float2] = 767.0;
printf("string: %s\nint 1: %i\nint 2: %i\nfloat 1: %0.2f\nfloat 2: %0.2f\n", array[string], array[int1], array[int2], array[float1], array[float2]);
}


Output:
I AM HAPPY TO SEE YOU BITCH
666
777
676.00
767.00
Reply
#4

It worked. I can't belive it actually worked. Thank you! It should be included in the Wikipedia Pawn scripting page, but it isn't. Thank you again, you changed my life! xD
Reply
#5

It is in the wiki. It's all in the enumerator section.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)