You said the function returns just a single character yet you are trying to return a character array. You can't have it both ways. What you should have done is char* getEmpName()const -- notice there is a * after char to indicate it will return an array of characters.

Hi, sorry to calling back to life this old post, but I still get error even if I tried to do as you said. I have a declaration in a class which is

class Bla{
private:
   char date[9];
public:
   char* ReturnDate(const Bla& blabla);
};

char* Bla::ReturnDate(const Bla& blabla);{
   return blabla.date;
}

The error is this

error C2440: 'return' : cannot convert from 'const char [9]' to 'char*'

Dani AI

Generated

The compiler error is a const/correctness and lifetime issue, not a mystery of arrays. Because the function receives a const reference, the member array is treated as const when accessed through that reference, so it decays to a pointer-to-const; the compiler refuses to convert that to a plain char*. was right that the return type must match what you actually return, and ’s syntax warning about the stray semicolon in the definition is a good reminder to check function signatures for simple typos if the compiler output looks odd.

A couple of practical rules and safer alternatives:

  • Avoid using const_cast to drop constness unless you absolutely know the original object is non-const and you will not modify it; misusing const_cast can produce undefined behavior (so treat ’s suggestion as a last-resort workaround, not a best practice).
  • If you need a stable, owning string that outlives the object, return a copy (use std::string) so callers don’t depend on the source object’s lifetime.
  • If you want zero-copy access but can guarantee the Bla instance stays alive, return a non-owning view (C++17 std::string_view) or a const char* from a const accessor — but document the lifetime requirement clearly.

Example patterns (not the same code shown above):

std::string getDateCopy() const {
    return std::string(date); // safe copy, caller owns the data
}

std::string_view getDateView() const noexcept {
    return std::string_view(date, 8); // non-owning — object must outlive the view
}

Final checks: ensure char date[9] really holds the expected characters plus a null terminator, prefer std::array<char,9> or std::string for clarity, and prefer a const member accessor over a free function that takes a const reference when you’re exposing an internal buffer.

Recommended Answers

All 3 Replies

mmm, if the whole point is avoiding accidental changes to Bla's date field then you could use const_cast.

http://www.cppreference.com/wiki/keywords/const_cast

char* ReturnDate(Bla const& blabla)
    { 
        //strcpy_s(blabla.date,8,"assssde");  <- This would give you a compiler error.
        return const_cast<char*> (blabla.date);
        
    }

Don't just trust me. Do some research. It's been some time since I fiddled with this and my memory is rusty. I'll look into this later, If I can

First, that semicolon on line 8 (between the end of your method implementation and the curly brace) is going to be a problem....

"blabla.date" is "const char *". So, one of 2 things: either cast the return value as (char *), so it will match the method prototype:

return (char *)blabla.date;

or, change the method prototype to return a type of "const char *":

class Bla{
private:
   char date[9];
public:
   const char* ReturnDate(const Bla& blabla);
};

const char* Bla::ReturnDate(const Bla& blabla){
   return blabla.date;
}

Second one Worked and I understood the reason: Thank you!
Just to explain to future beginner readers:

I declared a const char[] array; To return an array you must declare the function, whatever type it is, as

*type function(parameters){
   return parameter;
}

The only problem here was: that was a constant char, so you must declare your function as

const *type function(etc....)
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.