sizeof char-array
#1

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
Reply
#2

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

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

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.
Reply
#5

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.
Reply
#6

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

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

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

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.
Reply
#10

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
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)