Proper way to empty a C-String
Asked Answered
S

7

59

I've been working on a project in C that requires me to mess around with strings a lot. Normally, I do program in C++, so this is a bit different than just saying string.empty().

I'm wondering what would be the proper way to empty a string in C. Would this be it?

buffer[80] = "Hello World!\n";

// ...

strcpy(buffer, "");
Sigismondo answered 12/11, 2011 at 21:52 Comment(2)
Your example is confusing. Apparently buffer has been declared somewhere else and you try to initialize it from a literal. There is no need to clear anything here.Eremite
string.empty() doesn't modify the stringTopee
N
127

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));
Nudicaul answered 12/11, 2011 at 21:55 Comment(2)
To clarify, the last method will work if buffer is is an array, but not if it is a pointer. The compiler must be able to see the size of the array. If buffer is a pointer it will only zero out the size of a pointer, probably 4 or 8 bytes.Absence
This answer - https://mcmap.net/q/330959/-why-do-variables-declared-with-the-same-name-in-different-scopes-get-assigned-the-same-memory-addresses - makes a case for NEVER using strlen with memset - only use sizeofTitbit
C
5

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 ;)

Clockwise answered 3/4, 2014 at 5:31 Comment(0)
U
3

Depends on what you mean by emptying. If you just want an empty string, you could do

buffer[0] = 0;

If you want to set every element to zero, do

memset(buffer, 0, 80);
Unpaged answered 12/11, 2011 at 21:54 Comment(0)
E
1

If you are trying to clear out a receive buffer for something that receives strings I have found the best way is to use memset as described above. The reason is that no matter how big the next received string is (limited to sizeof buffer of course), it will automatically be an asciiz string if written into a buffer that has been pre-zeroed.

Err answered 12/1, 2020 at 1:30 Comment(0)
S
1

I'm a beginner but...Up to my knowledge,the best way is

strncpy(dest_string,"",strlen(dest_string));
Schoen answered 4/6, 2020 at 10:47 Comment(1)
Don't use strncpy geeksforgeeks.org/why-strcpy-and-strncpy-are-not-safe-to-useNewish
F
0

For those who clicked into this question to see some information about bzero and explicit_bzero:

  • bzero: this function is deprecated
  • explicit_bzero: this is not a standard c function and is absent on some of the BSDs

Further explanation:

Fideicommissary answered 20/5, 2022 at 5:4 Comment(0)
T
-1

needs name of string and its length will zero all characters other methods might stop at the first zero they encounter

    void strClear(char p[],u8 len){u8 i=0; 
        if(len){while(i<len){p[i]=0;i++;}}
    }
Thunderstorm answered 24/10, 2016 at 8:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.