array sizes do not match, or destination array is too small
#1

Код:
new savedPasswordForFinalReg[MAX_PLAYERS][255];
Код:
OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
	// ...

			// ...
			savedPasswordForFinalReg[playerid] = inputtext;   // ERROR LINE

	// ...

	return 1;
}
ERROR: array sizes do not match, or destination array is too small

This is not possible! I know that inputtext can have a maximum of 128, so my 255 string can't be smaller -.-. If I make my string 128 long, comes the same error.

Where's the problem ?!?!
Reply
#2

You might make the *string* 128 characters long, but you probably aren't considering the actual size of the inputtext array. Just because you shortened the string, doesn't mean that you've got rid of those empty cells.

To verify this, try putting something like
Код:
OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
	printf("OnDialogResponse: sizeof inputtext: %d", sizeof inputtext);    // DEBUG
	// ...
into your function, and see what gets output to the console.

You should be able to mitigate this with something like:
Код:
OnDialogResponse(playerid, dialogid, response, listitem, inputtext[])
{
	// ...

			// ...
			format(savedPasswordForFinalReg[playerid], sizeof savedPasswordForFinalReg[playerid], "%s", inputtext);   // should work now

	// ...

	return 1;
}
Reply
#3

PHP код:
new savedPasswordForFinalReg[MAX_PLAYERS][255];
savedPasswordForFinalReg[playerid] = inputtext
That's not right.

If you want to copy the content from some string to another one, you could use strcpy.
Reply
#4

Just format the variable.

pawn Код:
format(SavedPasswordForFinalReg[playerid], 255, inputtext);
Reply
#5

Quote:
Originally Posted by SickAttack
Посмотреть сообщение
Just format the string.

pawn Код:
format(SavedPasswordForFinalReg[playerid], 255, inputtext);
It's bad practice to use arbitrary strings as format strings in format(), rather than using the format string "%s" and passing the string as an extra argument.

The latter will never fail, whereas the former gets squiffy if any % characters make it into the string you pass.
Reply
#6

@renegade334 - The funny thing here is that you claim PAWN to be a language equivalent to a ten-year-old pedal cycle, which is a peace of cake, and you still get it wrong.
Reply
#7

Direct array to array assignments only work if the both arrays have the same number of dimensions and if the size of the array on the left is equal or greater than the size of the array on the right.

Don't use format to copy strings, though.
pawn Код:
stock strcpy(dest[], const source[], maxlength=sizeof dest)
    strcat((dest[0] = EOS, dest), source, maxlength);
Reply
#8

And format() is slower than strcat(), it's worth pointing out.

Quote:
Originally Posted by SickAttack
Посмотреть сообщение
@renegade334 - The funny thing here is that you claim PAWN to be a language equivalent to a ten-year-old pedal cycle, which is a peace of cake, and you still get it wrong.
Not when you ride it down the highway, it isn't.

Anyway, what specifically are you referring to? If it's how to pass strings to format():

Код:
new string[255];
new inputstring[] = "A percent sign looks like this: %. This input string contains some percent signs (%s) and a double-percent sign (%%).";

// Using inputstring directly as the format string
format(string, sizeof string, inputstring);
print(string);

// Using "%s" as the format string, and providing inputstring as an input argument
format(string, sizeof string, "%s", inputstring);
print(string);

// Output:
// A percent sign looks like this:  This input string contains some percent signs () and a double-percent sign (%).
// A percent sign looks like this: %. This input string contains some percent signs (%s) and a double-percent sign (%%).
Reply
#9

Thanks for the help guys!


Should I use strcpy or strins ?

strcpy:
Код:
stock strcpy(dest[], const source[], maxlength=sizeof dest)
{
    strcat((dest[0] = EOS, dest), source, maxlength);
}
strins:
Код:
strins(string[], const substr[], pos, maxlength=sizeof string);
Reply
#10

Quote:
Originally Posted by klaus1n3
Посмотреть сообщение
Should I use strcpy or strins ?
It looks like strcat, strins and memcpy are all pretty much equivalent in terms of speed. format is approximately 2.5x slower.

The strcat method is the one used in y_utils so it seems like a sensible bet.

Код:
Format string iterations: 7947ms
Format %s iterations: 7764ms
strcat iterations: 3475ms
strins iterations: 3476ms
memcpy iterations: 3530ms

code:
	new string[255] = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef";
	new target[240];
	new tick_count;
	
	tick_count = tickcount();
	for (new i = 0; i < 10000000; i++) {
		format(target, sizeof target, string);
	}
	tick_count = tickcount() - tick_count;
	printf("Format string iterations: %dms", tick_count);
	
	tick_count = tickcount();
	for (new i = 0; i < 10000000; i++) {
		format(target, sizeof target, "%s", string);
	}
	tick_count = tickcount() - tick_count;
	printf("Format %%s iterations: %dms", tick_count);
	
	tick_count = tickcount();
	for (new i = 0; i < 10000000; i++) {
		strcat((target[0] = EOS, target), string);
	}
	tick_count = tickcount() - tick_count;
	printf("strcat iterations: %dms", tick_count);
	
	tick_count = tickcount();
	for (new i = 0; i < 10000000; i++) {
		strins((target[0] = EOS, target), string, 0);
	}
	tick_count = tickcount() - tick_count;
	printf("strins iterations: %dms", tick_count);
	
	tick_count = tickcount();
	for (new i = 0; i < 10000000; i++) {
		memcpy(target, string, 0, strlen(string) * 4 + 4);
	}
	tick_count = tickcount() - tick_count;
	printf("memcpy iterations: %dms", tick_count);
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)