Storing null value in a char array
-
There is no reason why this should not be possible. But of course you can not use strcpy() and friends, which interpret the data as C string, which means the first NULL char indicates the end of the string. Use memcpy() with the appropriate length instead. If you still have issues, please show some code...
-
-
[quote author="anmol2701" date="1399032936"]but since i am putting arr[1]=0x00 it is giving buffer overflow exception since compiler is taking it as null value.
[/quote]No, that's perfectly valid! So your problem must be elsewhere. For the compiler the char array is just an array of char's and the value 0x00 has no special meaning. It does have a special meaning only when interpreting the array as a C string, like strcpy(), strlen(), strcmp() and friends do...
Where exactly in the code do you get the buffer overflow exception?
__
For instance, this code works fine:
@int _tmain(int argc, _TCHAR* argv[])
{
char test[50];for(int i = 0; i < 50; i++)
{
test[i] = i % 10;
}for(int i = 0; i < 50; i++)
{
printf("Value[d] is %d\n", i, (int)test[i]);
}return 0;
}@
@Value[00] is 0 <--
Value[01] is 1
Value[02] is 2
Value[03] is 3
Value[04] is 4
Value[05] is 5
Value[06] is 6
Value[07] is 7
Value[08] is 8
Value[09] is 9
Value[10] is 0 <--
Value[11] is 1
Value[12] is 2
Value[13] is 3
Value[14] is 4
Value[15] is 5
Value[16] is 6
Value[17] is 7
Value[18] is 8
Value[19] is 9
Value[20] is 0 <--
Value[21] is 1
Value[22] is 2
Value[23] is 3
Value[24] is 4
Value[25] is 5
Value[26] is 6
Value[27] is 7
Value[28] is 8
Value[29] is 9
Value[30] is 0 <--
Value[31] is 1
Value[32] is 2
Value[33] is 3
Value[34] is 4
Value[35] is 5
Value[36] is 6
Value[37] is 7
Value[38] is 8
Value[39] is 9
Value[40] is 0 <--
Value[41] is 1
Value[42] is 2
Value[43] is 3
Value[44] is 4
Value[45] is 5
Value[46] is 6
Value[47] is 7
Value[48] is 8
Value[49] is 9@ -
[quote author="anmol2701" date="1399092247"]arr2=0×00;
here i get exception.[/quote]
Then you obviously have some other issue in your code. But at this point it's impossible to say without seeing your complete code...
[quote author="anmol2701" date="1399092247"]
char array takes 0x00 as a null value and terminates the array. all the values entered after null cause buffer overflow.[/quote]No, it certainly doesn't! As said before, the value 0x00 has no special meaning at all, except when interpreting the content of the array as a C string. And even then, 0x00 only identifies the end of the string, not the end of the array. You can perfectly write to array positions after the NULL-terminator - as long as you are writing to positions that still are inside the array bounds.
You can try "the above sample code":http://qt-project.org/forums/viewreply/174569/, if you don't believe ;-)
__
Or just try this:
@int main()
{
char test[42];
test[0] = 'T';
test[1] = 'E';
test[2] = 'S';
test[3] = 'T';
test[4] = '\n';
test[5] = 0x00; //<-- First terminator
test[6] = 'E';
test[7] = 'V';
test[8] = 'I';
test[9] = 'L';
test[10] = '!';
test[11] = '\n';
test[12] = 0x00; //<-- Second terminatorprintf("Result #1: %s", test);
printf("Result #2: %s", &test[6]);return 0;
}@Expected Output:
@Result #1: TEST
Result #2: EVIL!@