PDA

View Full Version : Error in macro


Riwerry
12/08/2014, 05:19 PM
Hello, I'm making function that will create string with lowest possible calculated index for the string (may save LITTLE amount of memory :D). You can too format this string inside function, but I have problem getting error:
error 001: expected token: "-string end-"


DeclareOptimiziedString("string", "Hello %s!", "Jack"); //Example usage -> this string should have 14 cells which is lowest possible

//Macro
#define DeclareOptimiziedString(%0,%1,%2) \
new %0[IndexCount(%1, %2)] \
format(%0, sizeof(%0), %1, %2);

//Func that calculates index of string (with format)
IndexCount(const src[], const fmat[])
{
new
count,
i;

while(i++ < strlen(fmat))
{

if(fmat[i] == '"' || fmat[i] == ',' || fmat[i] == 32)
{
continue;
}
count++;
}
return strlen(src) + count;
}


Thanks!

Vince
12/08/2014, 06:13 PM
Pawn does not support dynamic arrays. Other than that, don't treat a macro like a function. If you need to create new variables within a macro then a function is usually more appropriate.

Riwerry
12/08/2014, 06:33 PM
I want to create formatted string, with lowest possible memory. That means I won't use bigger numbers than it needs like new string[128];. Yeah it's not macro, I think it's inline function, becouse I want just to replace it in piece of code, where am I. And if I create something like:
#define Test(%0, %1) new %0[%1];
Its working, but in code up is something bad. :\

[XST]O_x
12/08/2014, 06:37 PM
You can't do that.
That's like doing
new something = 5;
new array[something];


Not in pawn man, not in pawn.

Riwerry
12/08/2014, 06:49 PM
Yeap, but then whole thing have no sense, okay thanks. :D