3

Simple Question:

How do you return an empty C-String with as little code as possible?

I have code that needs to return an empty char*. I am looking for something along the lines of return "";. I know there are several ways to do this, but I am looking for the most efficient way possible.

Using return ""; gives warning: conversion from string literal to 'char *' is deprecated [-Wdeprecated-writable-strings]

Thanks!

Evorlor
  • 7,263
  • 17
  • 70
  • 141
  • What kind of efficiency are you looking for? CPU, memory, static segment of your application or less code? – Iuri Covalisin Oct 19 '13 at 03:19
  • 3
    What is the function prototype from which you want to return this? If it's `char *foo()`, I don't know of a shorter way than `return ""`, since `""` is a literal and the linker can give it an address. If you're returning an object, there are implicit conversions to consider. – cxw Oct 19 '13 at 03:19
  • @IuriCovalisin less code, but i would be interested to know the other ways of efficiency as well – Evorlor Oct 19 '13 at 03:20
  • @cxw So is warning: conversion from string literal to 'char *' is deprecated [-Wdeprecated-writable-strings] return ""; not something I need to worry about? – Evorlor Oct 19 '13 at 03:21
  • 1
    @Evorlor, It is if the caller does `*foo() = 'a'`. – chris Oct 19 '13 at 03:26
  • 3
    This would avoid deprecation: const char* s=""; return s; – Tarik Oct 19 '13 at 03:29
  • What are you doing with the result? If the caller should not alter it, declare the function `const char *foo()`. Otherwise, you should probably rethink your structure. – Kevin Oct 19 '13 at 03:29
  • @Tarik no good. thanks tho – Evorlor Oct 19 '13 at 03:40
  • @Kevin error: functions that differ only in their return type cannot be overloaded – Evorlor Oct 19 '13 at 03:47
  • @Evorlor: You'd have to declare the function as returning a `const char*`as well. Or cast away the constness, if -- and *only* if -- you know with 100% certainty that the caller isn't going to try and modify the string. But that's hideous; you shouldn't know anything about the caller. – cHao Oct 19 '13 at 03:48
  • So I am picking up that an empty char* does not exist. Is this true? – Evorlor Oct 19 '13 at 03:51
  • @Evorlor why are you trying to overload it? Change the original declaration to be `const`. – Kevin Oct 19 '13 at 03:53
  • I think the question may be getting a bit off track. I do not want a constant function. I just want to return an empty C-String for a particular case, if that is possible. – Evorlor Oct 19 '13 at 03:55
  • And yes, an empty `char *` can be allocated, but if you can't just change the return type to `const char *` you'll need to give us more information about the function, in particular why it can't be constant. – Kevin Oct 19 '13 at 03:57
  • I am trying, but my lack of C++ knowledge is making it tough. I have a C-String with 256 characters. I want to make each element empty in as little code as possible. – Evorlor Oct 19 '13 at 04:06
  • 1
    Whoa, that's a completely different question. Look at memset() to initialize a string. Ex. `char str[256]; memset(str,0,sizeof(str));`. – Chris Olsen Oct 19 '13 at 06:38

2 Answers2

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.

Chris Olsen
  • 3,243
  • 1
  • 27
  • 41
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.

Benjamin Lindley
  • 101,917
  • 9
  • 204
  • 274
  • It would be great to null-terminate the array to fix corruption by external code. – Basilevs Oct 19 '13 at 06:35
  • It is null terminated. Static variables are required to be automatically initialized to 0. – Chris Olsen Oct 19 '13 at 06:41
  • @ChrisOlsen I think Basilevs is suggesting to reassign the `char` on every call to the function. – aschepler Oct 19 '13 at 13:21
  • I'd argue that you don't really *want* that corruption fixed here; you want it fixed at the source. As it stands, someone modifying this "string" will almost certainly also overwrite whatever comes after it as well. If you magically fix the string each time, then the corruption appears to be caused by other things. No, if you're going to care about the value of `empty[0]`, i'd actually recommend *asserting* that it is a NUL, rather than setting it to one. – cHao Oct 21 '13 at 12:34