In addition to Will Dean's version, the following are common for whole buffer initialization:
Copychar s[10] = {'\0'};
or
Copychar s[10];
memset(s, '\0', sizeof(s));
or
Copychar s[10];
strncpy(s, "", sizeof(s));
Answer from Matt Joiner on Stack OverflowIn addition to Will Dean's version, the following are common for whole buffer initialization:
Copychar s[10] = {'\0'};
or
Copychar s[10];
memset(s, '\0', sizeof(s));
or
Copychar s[10];
strncpy(s, "", sizeof(s));
You want to set the first character of the string to zero, like this:
Copychar myString[10];
myString[0] = '\0';
(Or myString[0] = 0;)
Or, actually, on initialisation, you can do:
Copychar myString[10] = "";
But that's not a general way to set a string to zero length once it's been defined.
Videos
I know that some compilers give error on following:
char* test2;test2 = "";
Is it valid or equivalent, to do the following?
char* test2;test2[0] = '\0';
Thank in advance for your response.
Well it's sort of possible, but it won't behave the way you expect. And in more recent versions of C, it's undefined behaviour.
What you did was allocate memory and then throw that pointer away, thus leaking that memory. You replaced the pointer with a non-constant pointer to a string literal (which should make your compiler emit a warning or an error).
This happened to work in your case. Fortunately you didn't try to write to that memory. If you did, chances are that bad stuff will happen.
Uh, oh! You allocate memory for a string and store the handle to the allocated memory in w:
char* w = malloc(100*sizeof(char));
In the next line, you overwrite that handle with an immutable string literal:
w = "";
That means that (1) you can no longer free w as you should after using it and (2) that w now points to a string in read-only memory whose modification will lead to undefined behaviour, most likely a crash.
The dynamically allocated memory behaves like an array. C strings are character arrays that contain the valid characters of the string up to a null terminator, '\0'. There fore, setting the first character to the null character will give you an empty string:
*w = '\0';
or
w[0] = '\0';
In the dead branch, you want to fill the character array with the contents of a string, but you assign a read-only literal, too. You can use the function strcpy from <string.h> to fill a character array with a string:
strcpy(w, "Not empty anymore");
You must make sure, however, that the array is big enough to hold the string plus the terminating null character.
How do I initialize it to anything other than zero or NULL? I just want an empty string array, which I can use to store text with the help of a for loop. I dont want to assign it the function of GetString. If I declare the array in between an operation like.
string c_txt[i] = (p_txt[i] + key) % 26;
where c_txt is the array containing enciphered text, p_txt is array containing plain text, the console pops me an error saying "variable-sized object may not be initialized".
Also, string c_txt = NULL; desent work for obvious reasons. So how do I declare this string array?
P.S : I have a really bad feeling that this is a really dumb question and I`m making a fool of myself in front of the staff. What am I missing?
It depends on what you mean by "empty". If you just want a zero-length string, then your example will work.
This will also work:
buffer[0] = '\0';
If you want to zero the entire contents of the string, you can do it this way:
memset(buffer,0,strlen(buffer));
but this will only work for zeroing up to the first NULL character.
If the string is a static array, you can use:
memset(buffer,0,sizeof(buffer));
Two other ways are strcpy(str, ""); and string[0] = 0
To really delete the Variable contents (in case you have dirty code which is not working properly with the snippets above :P ) use a loop like in the example below.
#include <string.h>
...
int i=0;
for(i=0;i<strlen(string);i++)
{
string[i] = 0;
}
In case you want to clear a dynamic allocated array of chars from the beginning, you may either use a combination of malloc() and memset() or - and this is way faster - calloc() which does the same thing as malloc but initializing the whole array with Null.
At last i want you to have your runtime in mind. All the way more, if you're handling huge arrays (6 digits and above) you should try to set the first value to Null instead of running memset() through the whole String.
It may look dirtier at first, but is way faster. You just need to pay more attention on your code ;)
I hope this was useful for anybody ;)
A couple of ways come to mind. Given that strings in C are usually terminated by an ASCII zero, the easiest would be to set the first byte to zero.
a[0] = '\0';
Now this doesn't erase all the characters in the string, but since the first character is now a termination character the string looks empty to your program.
If you want to erase all the characters in the string then you need to use a loop.
OR
Another way might be to use memset() to set the whole string to zeros.
memset(a, 0, strlen(a));
but this will only work for zeroing up to the first NULL character.
In your case, it should suffice to do:
a[0] = '\0';
This sets the first char in the string to be the null terminating character, such that when you print the string, it prints an empty string.
You should also assign the null terminating character to after the effective last character:
a[0] = 'a';
a[1] = 'b';
a[2] = '\0';
printf("%s", a); //print "ab"