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;
}
Lukas-T
  • 11,133
  • 3
  • 20
  • 30
  • 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()`](https://en.cppreference.com/w/cpp/string/basic_string/data) to get non-const `char *` to the char array that is managed by a string. – Lukas-T Sep 22 '20 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 – Raildex Sep 22 '20 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: https://stackoverflow.com/questions/6441218/can-a-local-variables-memory-be-accessed-outside-its-scope – Jabberwocky Sep 22 '20 at 11:35
  • 3
    `char char_array[n+1];` [is not even valid.](https://stackoverflow.com/questions/15013077/arrayn-vs-array10-initializing-array-with-variable-vs-real-number) – user1810087 Sep 22 '20 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. – JohnFilleau Sep 22 '20 at 11:41

1 Answers1

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.

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185
  • 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 – Ahmed Elgindy Sep 22 '20 at 12:26
  • 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< – Ahmed Elgindy Sep 22 '20 at 12:27
  • @AhmedElgindy "converting a `std::string` to a char array" doesn't really make much sense. A `std::string` *is* a char array (and more) – 463035818_is_not_an_ai Sep 22 '20 at 12:28
  • okay put how can i push the chars of that string to the stack – Ahmed Elgindy Sep 22 '20 at 12:29
  • 1
    @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 – 463035818_is_not_an_ai Sep 22 '20 at 12:29
  • 1
    @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](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) – 463035818_is_not_an_ai Sep 22 '20 at 12:30