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!

16
  • What kind of efficiency are you looking for? CPU, memory, static segment of your application or less code? Commented Oct 19, 2013 at 3: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. Commented Oct 19, 2013 at 3:19
  • 1
    @Evorlor, It is if the caller does *foo() = 'a'. Commented Oct 19, 2013 at 3:26
  • 3
    This would avoid deprecation: const char* s=""; return s; Commented Oct 19, 2013 at 3:29
  • 1
    Whoa, that's a completely different question. Look at memset() to initialize a string. Ex. char str[256]; memset(str,0,sizeof(str));. Commented Oct 19, 2013 at 6:38

2 Answers 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.

Sign up to request clarification or add additional context in comments.

Comments

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.

4 Comments

It would be great to null-terminate the array to fix corruption by external code.
It is null terminated. Static variables are required to be automatically initialized to 0.
@ChrisOlsen I think Basilevs is suggesting to reassign the char on every call to the function.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.