I'm just starting c++ and am having difficulty understanding const char*
. I'm trying to convert the input in the method to string
, and then change the strings to add hyphens where I want and ultimately take that string and convert it back to char*
to return. So far when I try this it gives me a bus error 10.
char* getHyphen(const char* input){
string vowels [12] = {"A","E","I","O","U","Y","a","e","i","o","u","y"};
//convert char* to string
string a;
int i = 0;
while(input != '\0'){
a += input[i];
input++;
i++;
}
//convert a string to char*
return NULL;
}
A: The std::string
class has a constructor that takes a char const*
, so you simply create an instance to do your conversion.
B: Instances of std::string
have a c_str()
member function that returns a char const*
that you can use to convert back to char const*
.
auto my_cstr = "Hello"; // A
std::string s(my_cstr); // A
// ... modify 's' ...
auto back_to_cstr = s.c_str(); // B
First of all, you don't need all of that code to construct a std::string
from the input. You can just use:
string a(input);
As far as returning a new char*
, you can use:
return strdup(a.c_str()); // strdup is a non-standard function but it
// can be easily implemented if necessary.
Make sure to deallocate the returned value.
It will be better to just return a std::string
so the users of your function don't have to worry about memory allocation/deallocation.
std::string getHyphen(const char* input){
Don't use char*
. Use std::string
, like all other here are telling you. This will eliminate all such problems.
However, for the sake of completeness and because you want to understand the background, let's analyse what is going on.
while(input != '\\0'){
You probably mean:
while(*input != '\0') {
Your code compares the input
pointer itself to \\0
, ie it checks for a null-pointer, which is due to the unfortunate automatic conversion from a \\0
char
. If you tried to compare with, say, 'x'
or 'a'
, then you would get a compilation error instead of runtime crashes.
You want to dereference the pointer via *input
to get to the char
pointed to.
a += input[i]; input++; i++;
This will also not work. You increment the input
pointer, yet with [i]
you advance even further . For example, if input
has been incremented three times, then input[3]
will be the 7th character of the original array passed into the function, not the 4th one. This eventually results in undefined behaviour when you leave the bounds of the array. Undefined behaviour can also be the "bus error 10" you mention.
Replace with:
a += *input;
input++;
i++;
(Actually, now that i
is not used any longer, you can remove it altogether.)
And let me repeat it once again: Do not use char*
. Use std::string
.
Change your function declaration from
char* getHyphen(const char* input)
to
auto hyphenated( string const& input )
-> string
and avoid all the problems of conversion to char const*
and back.
That said, you can construct a std::string
from a char_const*
as follows:
string( "Blah" )
and you get back a temporary char const*
by using the c_str
method.
Do note that the result of c_str
is only valid as long as the original string
instance exists and is not modified. For example, applying c_str
to a local string
and returning that result, yields Undefined Behavior and is not a good idea. If you absolutely must return a char*
or char const*
, allocate an array with new
and copy the string data over with strcpy
, like this: return strcpy( new char[s.length()+1], s.c_str() )
, where the +1
is to accomodate a terminating zero-byte.
Exchange functions of data types in C/C++ convenience. In this repo there are convenience functions of all c++ data types and I wrote them all as in python.
Use This Repo:
https://github.com/azizovrafael/Simple-CPlusPlus
Example:
...
int main(){
int n = INT(INPUT("Write Some Text For Console Ex. (Enter: )"));
string str = STR(n);
return 0;
}
...
#simplecplusplus
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.