If I have a string like this:
char myString [5];
But I didn't put any characters into the array
does it mean:
myString [0] = '\0'
myString [1] = '\0'
myString [2] = '\0'
myString [3] = '\0'
myString [4] = '\0'
????
This is a discussion on A question about string within the C Programming forums, part of the General Programming Boards category; If I have a string like this: char myString [5]; But I didn't put any characters into the array does ...
If I have a string like this:
char myString [5];
But I didn't put any characters into the array
does it mean:
myString [0] = '\0'
myString [1] = '\0'
myString [2] = '\0'
myString [3] = '\0'
myString [4] = '\0'
????
No. It means you can't know for sure what it does contain until you initialize it to something.
If I have eight hours for cutting wood, I spend six sharpening my axe.
yep, vart said it right. but just a note:
inside of doing that, you could simple use the first line of code. sinceCode:myString [0] = '\0' myString [1] = '\0' myString [2] = '\0' myString [3] = '\0' myString [4] = '\0'
C stops processing a string after reaching the \0 sign.
How about following code?
Regards,Code:static char myString [5];
Siddu
myString would have static storage duration, thus it would be zero initialised.Originally Posted by Siddu_Kyocera
C + C++ Compiler: MinGW port of GCC
Version Control System: Bazaar
Look up a C++ Reference and learn How To Ask Questions The Smart Way
As long as the string is defined globally, it's same as static and is initialized to zeroes. If it's defined inside a function then its memory is the stack. It is not zeroed out when the function is executed.
Last edited by nonoob; 03-23-2010 at 04:19 PM.