A problem about strings
#10

The problem is that the string isn't cleared completely. If you use x = "" or x[0] = '\0', only the first cell will be cleared, but you think it is completely empty. If you put a character to the first cell again, the entire string with the original characters stored will show up again, which is my problem.

pawn Код:
new x[16];
x[0] = '1';
x[1] = '2';
x[2] = '3';
x[3] = '4';
x[4] = '5';
x = "";
printf("%c %c %c %c %c",x[0],x[1],x[2],x[3],x[4]);
x[0] = '2';
printf("%c %c %c %c %c",x[0],x[1],x[2],x[3],x[4]);
x[1] = '3';
printf("%c %c %c %c %c",x[0],x[1],x[2],x[3],x[4]);
x[2] = '4';
printf("%c %c %c %c %c",x[0],x[1],x[2],x[3],x[4]);
Output:
Код:
(yes it is empty here, making you to think that it is completely cleared.)
2 2 3 4 5 (The problem occurs here, the last 4 cells shouldn't appear, as they should have been removed already)
2 3 3 4 5
2 3 4 4 5
Since it act like this, I am not sure if there is any characters after the NULL character that can cause such problems, I have to loop through the whole string and clear it cell-by-cell.
Anyway I can get the range of the string and copy it by things like strmid instead of copying cell-by-cell, but I want to know what the problem is.

Quote:
Originally Posted by cessil
Посмотреть сообщение
kinda made up a scenario in my head about a dynamic string based on some variables, personally I'd just use another string but I came up with this script if I had to use the same string.

pawn Код:
new x[16];
    for(new i;i<10;i++)
    {
        switch(i)
        {
            case 1, 3, 5, 7, 9: x[i] = '1';
            default: x[i] = '0';
        }
    }
    print(x);
    x[0] = '\0';
    for(new i;i<5;i++)
    {
        switch(i)
        {
            case 1, 3, 5, 7, 9: strins(x,"3",i,sizeof(x));
            default: strins(x,"2",i,sizeof(x));
        }
    }
    print(x);
result:
0101010101
23232

not sure if it'd be faster than making all the cells null
Let's do the same thing with a nested loop (more suitable to my actual case):
pawn Код:
new x[16];
for(new j = 1;j < 3;j++)
{
    x[0] = '\0';
    for(new i;i<(10 / j);i++)
    {
        switch(i)
        {
            case 1, 3, 5, 7, 9: x[i] = 49 + (j - 1) * 2;//'1' if j = 1, '3' if j = 2
            default: x[i] = 48 + (j - 1) * 2;//'0' if j = 1, '2' if j = 2
        }
    }
    print(x);
}
Output:
Код:
0101010101
2323210101
The output explains it all.
Reply


Messages In This Thread
A problem about strings - by leong124 - 07.12.2011, 13:34
Re: A problem about strings - by TheArcher - 07.12.2011, 13:36
Re: A problem about strings - by leong124 - 07.12.2011, 13:39
Re: A problem about strings - by TheArcher - 07.12.2011, 13:41
Re: A problem about strings - by leong124 - 07.12.2011, 14:10
Re: A problem about strings - by JamesC - 07.12.2011, 14:19
Re: A problem about strings - by leong124 - 07.12.2011, 14:24
Re: A problem about strings - by cessil - 07.12.2011, 14:54
Re: A problem about strings - by cod4esle - 07.12.2011, 14:55
Re: A problem about strings - by leong124 - 07.12.2011, 15:49

Forum Jump:


Users browsing this thread: 4 Guest(s)