SA-MP Forums Archive
sizeof char-array - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: sizeof char-array (/showthread.php?tid=423433)



sizeof char-array - MP2 - 17.03.2013

There are 4 'chars' per array element, which means sizeof doesn't work as I need it to. If it's 1, 2, 3 or 4 chars, it will return 1. If it's 5, 6, 7 or 8 chars, it will return 2. It only works in 4's. Is there no way to do sizeof on a char-array?

pawn Код:
public OnFilterScriptInit()
{
    new bool:charArray1[10][1 char];
    printf("sizeof: %i", sizeof(charArray1[]));
   
    new bool:charArray2[10][4 char];
    printf("sizeof: %i", sizeof(charArray2[]));
   
    new bool:charArray3[10][8 char];
    printf("sizeof: %i", sizeof(charArray3[]));
   
    new bool:charArray4[10][9 char];
    printf("sizeof: %i", sizeof(charArray4[]));
   
    new bool:charArray5[10][20 char];
    printf("sizeof: %i", sizeof(charArray5[]));
    return 1;
}
Output: 1 1 2 3 5


Re: sizeof char-array - Misiur - 17.03.2013

Sizeof is correct - it returns number of cells. Using 17 char will use 5 cells anyway.


Re: sizeof char-array - MP2 - 17.03.2013

Yeah I know. Is there a way to return the number of chars?


Re: sizeof char-array - Misiur - 17.03.2013

Let me rephrase it. When you initialise the array
pawn Код:
new bool:charArray1[10][5 char];
In fact it's same as if you'd do
pawn Код:
new bool:charArray1[10][2];
Number of chars isn't saved anywhere.


Re: sizeof char-array - MP2 - 17.03.2013

I know that.

Are char-arrays null-terminated? I'll test it out.

EDIT: Well, they aren't exactly null-terminated, but any chars you don't set are automatically \0 (null-terminator) so I can just init the array with 1 more char than I need then go from there.

It's not exactly ideal, as I can't use bool:, I have to do this:

pawn Код:
new charArray[5][5 char];
   
charArray[0]{0} = '0';
charArray[0]{1} = '1';
charArray[0]{2} = '0';
charArray[0]{3} = '1';
I can't think of another way to do this.

Thanks for the help anyway.


Re: sizeof char-array - Misiur - 17.03.2013

strlen works with packed strings. If all your values are non 0, then it will do the trick


Re: sizeof char-array - MP2 - 17.03.2013

That's the problem - maybe they are all 0/false.


Re: sizeof char-array - mastermax7777 - 17.03.2013

i have simple solution that will save headache and time.. DONT USE CHAR


Re: sizeof char-array - MP2 - 17.03.2013

Quote:
Originally Posted by mastermax7777
Посмотреть сообщение
i have simple solution that will save headache and time.. DONT USE CHAR
To be honest I've been thinking the same thing. It's a lot of hassle to save a few KB (or less, I'm not sure) of memory.


Re: sizeof char-array - Misiur - 17.03.2013

If you array is shorter than 255 chars, you could sacrifice first char to hold the size of it. Of course not the handiest way