Setting string size via function parameter
#1

Why doesn't this work? Can I set the size of a string with a variable somehow? It can be useful in a function.

pawn Код:
someFunction(size) {
    new string[size];
}
Код:
error 008: must be a constant expression; assumed zero
error 009: invalid array size (negative, zero or out of bounds)
error 036: empty statement
fatal error 107: too many error messages on one line
Reply
#2

pawn Код:
int size;
function(size) {
    new string[size];
}
Reply
#3

Never tried creating a little function using a stock I usually use macro for one line function, the code below compiled.

pawn Код:
#define Create.%0(%1) \
    new %0[%1]
Compilation
pawn Код:
Pawn compiler 3.2.3664          Copyright (c) 1997-2006, ITB CompuPhase
Usage
pawn Код:
Create.string(128);
   
format(string, sizeof(string), "aa");
Parameter
pawn Код:
Parameter : Create_StringName(Size);
Reply
#4

Quote:
Originally Posted by pds2k12
Посмотреть сообщение
Never tried creating a little function using a stock I usually use macro for one line function, the code below compiled.

pawn Код:
#define Create.%0(%1) \
    new %0[%1]
Compilation
pawn Код:
Pawn compiler 3.2.3664          Copyright (c) 1997-2006, ITB CompuPhase
Usage
pawn Код:
Create.string(128);
   
format(string, sizeof(string), "aa");
Parameter
pawn Код:
Parameter : Create_StringName(Size);
that's NOT how you use macros!
Reply
#5

Quote:
Originally Posted by Ada32
Посмотреть сообщение
that's NOT how you use macros!
Zir can u sho me how 2 use macro? Zorry me beginner in scripting, please show me zir, i want to know hau also zorry me no speak english veri will.
Reply
#6

Quote:
Originally Posted by pds2k12
Посмотреть сообщение
Zir can u sho me how 2 use macro? Zorry me beginner in scripting, please show me zir, i want to know hau also zorry me no speak english veri will.
i'll do you better -> https://sampforum.blast.hk/showthread.php?tid=166680
Reply
#7

So there's no way I can use this without a macro?
Reply
#8

Quote:
Originally Posted by Ada32
Посмотреть сообщение
I won't argue with you, you don't even show me what I did wrong in there, there's alot of different style creating a macro, I apologize for being unprofessional *(Above, because your comment made me lol)

Pawn code
pawn Код:
#include <a_samp>

#define Create.%0(%1) \
    new %0[%1]
   
main()
{
    Create.string(128);
   
    new pds2k12 = 60 * 10 / 5;
    format(string, sizeof(string), "60 x 10 ч 5 = %i", pds2k12);
    print(string);
}
Result
pawn Код:
60 x 10 ч 5 = 120
Try the code here: http://slice-vps.nl:7070/

EDIT: lets go hard if you don't believe.

Pawn Code
pawn Код:
#include <a_samp>

#define Create.%0(%1) \
    new %0[%1]
   
main()
{
    new StartTick = GetTickCount();
    Create.string(128);
    printf("Took %i ms to create a string", GetTickCount() - StartTick);
   
    new StartTick1 = GetTickCount();
    new pds2k12 = 60 * 10 / 5;
    format(string, sizeof(string), "60 x 10 ч 5 = %i", pds2k12);
    print(string);
    printf("Took %i ms to execute the string & variable", GetTickCount() - StartTick1);
}
Result
Код:
Took 0 ms to create a string
60 x 10 ч 5 = 120
Took 1 ms to execute the string & variable
Reply
#9

Well, thanks, but is there a way to do it with a function as shown above?
Reply
#10

AFAIK, pawn requires strings with a fixed size.
You also cannot resize an existing string or array in pawn during runtime.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)