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));
Answer from Mysticial on Stack OverflowIt 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 ;)
Videos
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"
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));
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.
In C a string is an array of char and the end of the string is marked with a NUL character (aka '\0') which is nothing else than a byte of value 0.
If you want to have an empty string it is sufficient to do
temp[0] = '\0';
or
*temp = '\0';
which is the same.
If you defined something like
char temp[100];
you could also do
memset(temp, '\0', sizeof temp);
This will overwrite all characters, allowing you to fill in new data character by character without having to care about the terminating '\0'.
With dynamic allocation the answer will be different.
It depends a bit on how you want to assign a new value to temp, but in general it is not necessary to clear the old value before assigning a new value.
In C, a string is a sequence of consecutive char variables that is terminated by the representation of zero as a character, '\0'.
This value acts as a sentinel for the end of the string and allows the idiomatic "string processing" loop below:
char *p = <some string>;
while(*p)
{
dosomething(*p);
p++;
}
Library functions that process strings (i.e. strlen, strcpy, strcat, etc.) use a construct similar to the code above, and when you write your own code that processes arbitrary strings, you will find the null character check useful; even if a string is stored in a char [] array of known length, it decays to a pointer to its first element when passed to a function, losing information about its length.
So, if you want to blank the string, all that is needed is to set the first element of the string to '\0'. If the first value in the string is the null terminator, the condition *p is false and the loop is never entered:
char *p = "\0someotherstuff";
printf("%zu\n", strlen(p));
// Output: 0
Since choice hasn't been initialized to anything, strlen(choice) can be returning any value between 0 and SIZE_MAX, which is probably causing memset to overwrite stuff it shouldn't.
Similarly, it may be causing problems with the first strcmp call against "logout".
You do want to initialize choice to something before the first strcmp call; I'd recommend zeroing out the whole buffer like so:
char choice[255] = {0};
You do not need to clear your input buffer before reading input again - scanf will overwrite the existing contents with the new input. You should probably lose the memset call completely.
However, if you feel you must zero out the buffer between inputs (and there are valid use cases for doing so, although this isn't one of them), use sizeof choice as the third argument to memset:
memset( choice, 0, sizeof choice );
This will zero out the entire choice array. That way it doesn't matter if choice contains a properly terminated string or not.
If you are only using C strings and related functions like strlen, strcat, strcpy etc then choice[0] = 0; will do it. There is no need to set every character to 0.
Setting first char to nul is perfectly acceptable. But if that string was sensitive in terms of security, then you should zero it out with memset.
Edit:
Answer from Matteo Italia made me dig a bit deeper on this subject. According to this document (and Matteos answer) memset could be optimized away, and so is not the best option to remove sensitive information from memory. The document has several options, but none of them is portable and reliable, so it proposes new function standard memset_s just for such purposes. This function does not exist yet, so we're currently stuck with non-portable (SecureZeroMemory), non-reliable (volatile trick), or non-optimal options (secure_memset example).
Use memset instead. This would just nullify the buffer but the memory allocated would any how gets deallocated from stack when the variable goes out of scope.
memset (name,'\0',sizeof(name));
For some reason, the name variable becomes empty even though I gave it an input.
Code:
#include <stdio.h>
int main(){
char name[16];
short unsigned int age;
printf("What is your name? ");
scanf("%s", &name);
printf("How old are you? ");
scanf("%u", &age);
printf("Hello, %s.\n", name);
printf("You are %u years old\n", age);
return 0;
}Terminal:
What is your name? Momus How old are you? 99 Hello, . You are 99 years old
I seems that the value for name was changed in the part somewhere in the part that prints "How old are you? " and the scanf() for the value of age because it works when I do this.
Code:
#include <stdio.h>
int main(){
char name[25];
short unsigned int age;
printf("What is your name? ");
scanf("%s", &name);
printf("Hello, %s.\n", name);
printf("How old are you? ");
scanf("%u", &age);
printf("You are %u years old\n", age);
return 0;
}Terminal:
What is your name? Momus Hello, Momus. How old are you? 99 You are 99 years old
Does anyone know what happened? How do I make it so that the first one will show the input? Thanks!