Array to string
#1

Hey,
I want to store an array's full content in another variable. I know I can make it by going through every slot but I'm pretty sure there's a much easier way I'm missing out. Please help me!
Reply
#2

Here's an example:
pawn Код:
new array[2][5]; // Declare the array
array[0] = "Test"; // Make array 0 contain "test"
array[1] = array[0]; // Make array 1 contain what array 0 contains
You cannot store several characters in a single variable if that's what you mean.
Reply
#3

That's what I meant when I said I could go through it slot by slot but I dont want to.
Here's what I want:
Quote:

new Input[5] = {a,b,c,d,e};
new Str = Input

But that gives error ofcourse. So basically I want Str to store "abcde".
Reply
#4

Use functions you can't assign arrays like that in PAWN.

memcpy or strcat
Reply
#5

If you want to insert characters into a string, use strins.

If you want to copy an array (array2 = array1), use memcpy.
Reply
#6

Код:
new Input[5] = {a,b,c,d,e};
new Str[sizeof(Input)] = Input;
But I don't understand what do you want. You can use index easily for that:

Input[0] = "a"
Input[1] = "b"
Input = "abcde"

Why to copy that you already have?
Reply
#7

Quote:
Originally Posted by alexus
Посмотреть сообщение
Код:
new Input[5] = {a,b,c,d,e};
new Str[sizeof(Input)] = Input;
But I don't understand what do you want. You can use index easily for that:

Input[0] = "a"
Input[1] = "b"
Input = "abcde"

Why to copy that you already have?
Why do you even need that?

new Input = {'a','b','c','d','\0'};
print(Input);

EDIT:Oops sorry, even you were criticizing that :P
Reply
#8

You can't store several pieces of data in a variable, that's why we use arrays for containing words.
Each variable can store 1 piece of data.
pawn Код:
Var1 = "H";
Var2 = "i";
You cannot do
pawn Код:
Var = "Hi";
It's the same with arrays, but they're like several variables.
You could say using Array[3] contains 3 "variables".
pawn Код:
Array[0] = "H";
Array[1] = "i";
Array[2] = "\0";
Which is the same as just doing
pawn Код:
Array = "Hi";
If the array is declared using
pawn Код:
new Array[3];
Because each "number" is like 1 variable, so here we have 3 variables, which and can store 1 piece of information each.
The reason we need 3 is because of the null constant, AKA "\0", it tells the compiler that the string ends where the null symbol is.
You can read more in this tutorial:
https://sampforum.blast.hk/showthread.php?tid=318212
Reply
#9

Doing this
pawn Код:
new Input = {'a','b','c','d','e'};
print(Input);
Isn't possible, because this , is supposed to hold an integer generally but doing this is possible:
pawn Код:
new Input[6] = {'a','b','c','d','e'};
print(Input);//would print 'abcde'
//tests:
for(new i = 0; i <6; i++) printf("%d:%s",i,Input);
/*
Would Print:
0:a
1:b
2:c
3:d
4:e
5: ' '//Means an empty message because the fifth cell is for the \0 , the null character
*/
Reply
#10

Quote:
Originally Posted by alexus
Посмотреть сообщение
Код:
new Input[5] = {a,b,c,d,e};
new Str[sizeof(Input)] = Input;
But I don't understand what do you want. You can use index easily for that:

Input[0] = "a"
Input[1] = "b"
Input = "abcde"

Why to copy that you already have?
It was just an example. The data of Input isn't fixed, it's what the user writes in it. To be more specific it's an input styled dialog and I want to use the given input outside of the callback 'OnDialogResponse' so I need a global variable for it. But you already helped me a lot, thanks guys.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)