English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

say I got a variable "char[] something = "Hello World" "
Now I want something to be empty, null. How do I do this ?

2007-01-30 14:08:59 · 4 answers · asked by ? 3 in Computers & Internet Programming & Design

4 answers

something.empty();

2007-01-30 14:11:39 · answer #1 · answered by KO 3 · 0 2

Actually, if you are using 'C', and you just want the string to be 'empty' (i.e. still be accessable by address, but contain no characters, then you need to set the first character to '\0' (ASCII value 0) since all C strings end with a 'null' character.

Note, this is different from "NULL", which is used to initialize pointers. Now, assuming this is valid ANSI C and you used:

char something[] = "Hello World";

Then you could basically empty the string by using:

something[0] = '\0';

However, be very careful. Because you did not specify a size of the array, you will only be able to copy no more than 12 'char' values into the string (the length of "Hello World" plus its 'null' character). If you were to empty the string, then use strcpy() or other direct means to fill this string array, you would cause stack or heap corruption if you overstep that twelve char boundary.

You can do something like this:

char something[1024] = { "Hello World" };

This will ensure that you have up to 1024 characters of space (minus one for the ending null character) that you can use later to fill up with larger strings.

2007-01-30 22:43:28 · answer #2 · answered by JFalcon 5 · 2 0

string is an array ends with the character \0

2007-01-30 22:18:05 · answer #3 · answered by iyiogrenci 6 · 1 0

char[] something="" or char[] something=NULL. or char[] something=0.

2007-01-30 22:13:15 · answer #4 · answered by iammisc 5 · 1 0

fedest.com, questions and answers