0

I want to return an array of chars and this is the code

char convert(string user){
    int n = user.length();
    char char_array[n+1];
    strcpy(char_array,user.c_str()) ;
    return char_array;
}
5
  • 4
    What is you question? There are some things wrong here, but in the end you probably don't need this function at all. You can use std::string::data() to get non-const char * to the char array that is managed by a string. Commented Sep 22, 2020 at 11:35
  • first, you want to return char* instead of char. Second you want to dynamically allocate the array with char* char_array = new char[n+1], otherwise returning the array will result in undefined behaviour. make sure to delete the allocated array when you need to Commented Sep 22, 2020 at 11:35
  • 4
    You're using C++, you should't work with char arrays in the first place, unless there is a very specifc good reason to do so. Also the function should return a char* instead of a char. and you're trying to return a pointer to a local variable which won't work either. For the latter point read this for more information: stackoverflow.com/questions/6441218/… Commented Sep 22, 2020 at 11:35
  • 3
    char char_array[n+1]; is not even valid. Commented Sep 22, 2020 at 11:40
  • 4
    What makes you think you need a char array in the first place? Legacy functions that expect a const char * can be passed your my_string.c_str() result. Any function that performs mutations on a non-const char * usually has an already existing analogue for std::string that bypasses c-stringiness. Commented Sep 22, 2020 at 11:41

1 Answer 1

1

You cannot return an array from a function. You can return a pointer to the first element of such array. Good news is: That function already exists. It is the std::string::c_str method.

void foo(const char*);     // <- legacy, cannot modify, must pass c-string

std::string x{"Hello World"};
foo( x.c_str() );

In your code, this char char_array[n+1]; is not valid standard C++. See here for details: Why aren't variable-length arrays part of the C++ standard?

Moreover you attempt to return a pointer to a local array. Once the function ends the arrays lifetime has passed and the pointer is of no use. More on that here: Can a local variable's memory be accessed outside its scope?

Last (and least) you declared the function to return a char when you wanted a char*. However, fixing that won't really help due to the last point.

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

6 Comments

look I traying to make stack and put chars after converting a string from the user I made some changes to the convert function but when I use the peek function it doesn't give me any return
void convert(string user){ int n = user.length(); char char_array[n+1]; strcpy(char_array,user.c_str()) ; for (int i = 0 ; i <= n ; i ++){ push(char_array[i]); cout<<char_array[i]<<endl; } }
@AhmedElgindy "converting a std::string to a char array" doesn't really make much sense. A std::string is a char array (and more)
@AhmedElgindy the code you posted may compile without compiler errors, yet is is completely wrong. If you have additional problems / question with code not included in the qeustion I cannot know. Code in comments is not readable
@AhmedElgindy what stack? Maybe you want to open a new quesiton where you ask about the actual problem you are trying to solve. The qeustion here seems to be a xy problem
|

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.