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 Overflow
Top answer
1 of 2
10

Short Answer:

const char *get_string() { return ""; }

or

char *get_string() { return const_cast<char *>(""); }

or

char *get_string() { return NULL; }

or

std::string get_string() { return std::string(); }


Detailed Answer:

Implicit conversion from string literal to char * is supported in C and C++98/C++03, but apparently not in C++11. The deprecation warning is just there to let you know that this should be addressed, particularly if you want to be able to migrate your code C++11.

The empty string literal ("") is not actually empty, it is a string containing a single null character (\0). When you use return "";, you are actually returning a pointer to the memory location of the const string literal so the function return type should be const char *.

If you really must return a non-const pointer to a string literal, you can use the const_cast operator to cast away the const.

A better practice would be to return NULL (or nullptr) for functions that are returning empty, non-const, C-style strings, but only if the calling code is checking for NULL pointers.

Note that C++ has its own string type (std::string), and an even better practice would be to use this rather than a C-style string when possible.

2 of 2
2
char* foo()
{
    static char empty[1];
    return empty;
}

Just be aware that this function is absolutely stupid, and whatever your actual problem is, this is not likely the correct solution. But, since you refuse to expound upon your actual problem, here you go.

Find elsewhere
Top answer
1 of 3
2

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.

2 of 3
0

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
🌐
Northern Illinois University
faculty.cs.niu.edu › ~winans › CS501 › Notes › cstrings.html
C Strings
The destination array must be large enough to hold the combined strings (including the null character). If it is not, the array will overflow.
🌐
Stack Overflow
stackoverflow.com › questions › 36230169 › string-function-returning-empty-string-c
String function returning empty string C++ - Stack Overflow
I'm trying to make a function which converts a number to binary in C++ but whenever I call the function it returns an empty string. Here is my code: #include #include
Top answer
1 of 16
907

Use whatever you and your team find the most readable.

Other answers have suggested that a new string is created every time you use "". This is not true - due to string interning, it will be created either once per assembly or once per AppDomain (or possibly once for the whole process - not sure on that front). This difference is negligible - massively, massively insignificant.

Which you find more readable is a different matter, however. It's subjective and will vary from person to person - so I suggest you find out what most people on your team like, and all go with that for consistency. Personally I find "" easier to read.

The argument that "" and " " are easily mistaken for each other doesn't really wash with me. Unless you're using a proportional font (and I haven't worked with any developers who do) it's pretty easy to tell the difference.

2 of 16
405

There really is no difference from a performance and code generated standpoint. In performance testing, they went back and forth between which one was faster vs the other, and only by milliseconds.

In looking at the behind the scenes code, you really don't see any difference either. The only difference is in the IL, which string.Empty use the opcode ldsfld and "" uses the opcode ldstr, but that is only because string.Empty is static, and both instructions do the same thing. If you look at the assembly that is produced, it is exactly the same.

C# Code

private void Test1()
{
    string test1 = string.Empty;    
    string test11 = test1;
}

private void Test2()
{
    string test2 = "";    
    string test22 = test2;
}

IL Code

.method private hidebysig instance void 
          Test1() cil managed
{
  // Code size       10 (0xa)
  .maxstack  1
  .locals init ([0] string test1,
                [1] string test11)
  IL_0000:  nop
  IL_0001:  ldsfld     string [mscorlib]System.String::Empty
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  stloc.1
  IL_0009:  ret
} // end of method Form1::Test1
.method private hidebysig instance void 
        Test2() cil managed
{
  // Code size       10 (0xa)
  .maxstack  1
  .locals init ([0] string test2,
                [1] string test22)
  IL_0000:  nop
  IL_0001:  ldstr      ""
  IL_0006:  stloc.0
  IL_0007:  ldloc.0
  IL_0008:  stloc.1
  IL_0009:  ret
} // end of method Form1::Test2

Assembly code

        string test1 = string.Empty;
0000003a  mov         eax,dword ptr ds:[022A102Ch] 
0000003f  mov         dword ptr [ebp-40h],eax 

        string test11 = test1;
00000042  mov         eax,dword ptr [ebp-40h] 
00000045  mov         dword ptr [ebp-44h],eax 
        string test2 = "";
0000003a  mov         eax,dword ptr ds:[022A202Ch] 
00000040  mov         dword ptr [ebp-40h],eax 

        string test22 = test2;
00000043  mov         eax,dword ptr [ebp-40h] 
00000046  mov         dword ptr [ebp-44h],eax 
Top answer
1 of 4
9

It's implementation defined. §5.1.2.2.1 abridged:

  • If the value of argc is greater than zero, the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings, which are given implementation-defined values by the host environment prior to program startup. The intent is to supply to the program information determined prior to program startup from elsewhere in the hosted environment. [...]

  • If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment. [...]

So if argc is greater than zero, it's quite the intention that argv[0] never be an empty string, but it could happen. (Note that with argc equal to n, argv[0] through argv[n - 1] are never null and always point to a string. The string itself may be empty, though. If n is zero, argv[0] is null.)

In practice, of course, you just need to make sure the platforms your targetting behave as needed.

2 of 4
7

Yes.

The C language standard explicitly allows for the possibility that argv[0] can be a null pointer, or that it can point to an empty string (""). N1256 5.1.2.2.1p2:

The value of argc shall be nonnegative.

argv[argc] shall be a null pointer.

[...]

If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment. If the value of argc is greater than one, the strings pointed to by argv[1] through argv[argc-1] represent the program parameters.

On Unix-like systems, programs are invoked by one of the exec() family of functions (execl(), execlp(), etc.), which allow the caller to specify exactly what arguments are passed to the main() function. (It's even possible to invoke a program in ways that violate the requirements imposed by the C standard.)

Note that the standard says that argv[0] (assuming it's neither null nor empty) "represents the program name". The standard is deliberately vague about how it represents the program name. In particular, it needn't provide a name by which the program can be invoked (since the standard doesn't even require that programs can be invoked by name).