I am using the following code in order to reverse a char array. My code as well as the error can be found below.
My code:
char * reverseStr(char* s) {
int i=0; //legnth of string
while(s[i]) i++;
char reversed[i];
for(int j=0; j<i; j++) {
reversed[j] = s[i-j - 1]; //look at this later
}
return *(reversed);
}
The error:
Compiling...
Compile error: your program did not compile correctly:
program.c: In function 'char* reverseStr(char*)':
program.c:18: error: invalid conversion from 'char' to 'char*'
--> 17: }
--> 18: return *(reversed);
Thank you in advance!
Your return value and type is wrong.
Furthermore, your declaration of reversed is invalid and would leak memory in any case.
Also, calculating the string length instead of using std::strlen isn’t recommended and the standard library has the std::reverse function to reverse strings.
Well, you are returning a char instead of a char*, so you are only returning the first letter in the reversed string instead of a string. Which causes your error messages, because you try to treat a char as a char*.
Check the error message:
program.c: In function 'int itoa2(int, char*, int)':
program.c:45: error: invalid conversion from 'char' to 'const char*'
It clearly tells you what the error is: invalid cast from const char* to char
In your code i is not const.
char reverseStr(char* s) {
int i=0; // --->> NOT CONST
while(s[i]) i++;
char reversed[i];
for(int j=0; j<i; j++) {
reversed[j] = s[i-j - 1]; //look at this later
}
return *(reversed);
}
char reversed[i]; ---> Variable Length Array in C++?? i is supposed to be known at Compile time.
strcpy receives (char*, const char*) as parameters.However, the return type of your function is char, thus the error appears.
And char reversed[] is allocated on the stack of the function, please don't use it as a return value.
Related
I am Comparing two strings but unfortunately this error occured
error: cannot convert 'std::__cxx11::string' {aka 'std::__cxx11::basic_string<char>'} to 'const char*' line 23
#include<iostream>
#include<string.h>
using namespace std;
class car{
public:
int feulcp,batterycp,length,height,widht,seater,torque,power,temp;
char ownername[20],noplate[12],statecheck[20];
string statename[28]={"andhra pradesh","arunachal pradesh","assam,bihar","chhattisgarh","goa","gujarat","haryana","himachal pradesh","jharkhand","karnataka","kerala","madhya pradesh","maharashtra","manipur","meghalaya","mizoram","nagaland","odisha","punjab","rajasthan","sikkim","tamil nadu","telangana","tripura","uttarakhand","uttar pradesh","west bengal"};
car(){
cout<<"Please Enter Your State Name :";
y:
cin>>statecheck;
for(int i=0;i<=27;i++){
temp=strcmp(statename[i],statecheck);//here is the Error!!!
if(temp==0){
goto x;
}
else
cout<<"INVALID STATE NAME \n PLEASE RE-ENTER ";
goto y;
}
x:
cout<<"successfull";
}
};
int main(){
car car1;
return 0;
}
strcmp() takes const char* parameters, but you are passing it a std::string object instead, hence the error. Use the std::string::c_str() method to get a const char*:
temp = strcmp(statename[i].c_str(), statecheck);
That being said, there is no need to use strcmp() at all, as std::string has its own operator== and compare() methods for performing string comparisons, eg:
if (statename[i] == statecheck){
if (statename[i].compare(statecheck) == 0){
SYNOPSIS: strcmp()
`int strcmp(const char *s1, const char *s2);`
Both arguments for strcmp() should be interpreted as type unsigned char, and their difference is calculated.
`temp=strcmp(statename[i],statecheck);`
In your code "statename" is "std::string" and compiler fails the implicit conversion from std::string to const char*.
SOLUTION:
temp = strcmp(statename[i].c_str(), statecheck);
std::string::c_str : will represent current value of the string in C-string.
NOTE
Replacing :
char statecheck[20]; with std::string statecheck;
temp=strcmp(statename[i].c_str(),statecheck); and if(temp==0) with if(statename[i] == statecheck)
will be better and as Adrian Mole explained the code will fail to provide the expected result.
Pls free to correct incase of any mistakes.
I am trying to print the elements of a set containing strings on graphics.h console using outtext() function,but i get this error:
cannot convert 'std::string {aka std::basic_string}' to 'char*' for argument '1' to 'void outtext(char*)'|
this the piece of code that gives error:
for(i=0;i<20;i++){
for(j=0;j<20;j++){
outtext(str[i][j]);
}
}
the template for the outtext function in the graphics.h header is like this:
void outtext(char *textstring);
i have used c_str() like this:
for(i=0;i<20;i++){
for(j=0;j<20;j++){
outtext(str[i][j].c_str());
}
}
but this time it gives this error:
error: invalid conversion from 'const char*' to 'char*' [-fpermissive]|
You can try this one as well:
char *cstr = new char[21]; // just in case string length is maxed at 20, leave 1 character for '\0'
for (int i = 0; i<20; i++) {
for (int j = 0; j<20; j++) {
strcpy_s(cstr, str[i][j].length() + 1, str[i][j].c_str());
outtext(cstr);
}
}
delete[] cstr;
Just added a char* string to temporarily hold the converted std::string value. The tricky part is that char* strings normally have the terminating character \0 which std::string don't have, so you have to add 1 more character to the size of each "row" of str.
I take it this question is about the 30 years old BGI graphics library and Borland C++. The root of the problem is that this library was poorly written, as it didn't implement const correctness.
The Turbo C++ compiler did not follow anything remotely close to any C++ standard, so you are mostly out of luck. If you had a proper C++ compiler you could use const_cast, but I very much doubt this is available to you.
The only solution left is the dirty, bad way:
outtext((char*)str[i][j].c_str()); // bad practice
You should never cast away const like this in neither C nor C++.
If you can change the prototype of the output function then it is better to change void outtext(char *textstring); to void outtext(const char *textstring); because there is no need for the output function to modifiy the string. Otherwise you could use const_cast before passing to the function like outtext(const_cast<char*>(str[i][j].c_str())) or copy the string to another char* and passed the copied value.
Good morning everyone! I am trying to create a fork/exec call from a parent program via a passed '-e' parameter (e.g. parent -e child key1=val1 ...). As a result I want to copy all the values after the first two in the argv array into a new array child_argv. Something like:
const char *child_argv[10]; // this is actually a global variable
static const char *sExecute;
int I = 0;
const char *Value = argv[1];
sExecute = Value;
for (i=2; i<argc; i++) {
child_argv[I] = argv[i];
I++;
}
child_argv[I] = NULL; // terminate the last array index with NULL
This way I can call the exec side via something like:
execl(sExecute, child_argv);
However I get the error message "error: cannot convert 'const char**' to 'const char*' for argument '2' to 'execl(const char*, const char*, ...)'". I have even tried to use an intermediate step:
const char *child_argv[10]; // this is actually a global variable
static const char *sExecute;
int I = 0;
const char *Value = argv[1];
sExecute = Value;
for (i=2; i<argc; i++) {
const char *Value = argv[i+1];
child_argv[I] = Value;
I++;
}
child_argv[I] = NULL; // terminate the last array index with NULL
But I can't figure this out. Any help would be greatly appreciated!
UPDATE
As was pointed out, I should be using 'execv' instead of 'execl' in this situation. Still getting errors though...
UPDATE 2
I ended up copying the array without the desired parameters of argv. See the post here to see the result How to copy portions of an array into another array
From here: http://linux.die.net/man/3/exec
I think you mean to call "execv" not "execl". Execl seems to take a variable number of arguments, expecting each const char * to be another argument, while execv takes an array of arguments.
You should be using execv. When you convert to execv, for some reason execv expects a array of non-const pointers instead of const pointers, so you either have to just cast the array to (char**) or copy the strings into (char*) pointers.
I receive this error:
array.cpp: In function ‘void strcat2(char*, const char*)’:
array.cpp:74: error: invalid conversion from ‘char’ to ‘char*’
array.cpp:74: error: initializing argument 1 of ‘void strcpy2(char*, const char*)’
array.cpp:74: error: invalid conversion from ‘char’ to ‘const char*’
array.cpp:74: error: initializing argument 2 of ‘void strcpy2(char*, const char*)’
When trying to run this code:
//Concatenates two arrays of characters using pointers
void strcat2(char* t, const char* s)
{
unsigned int i;
for (i = 0; *t; i++);
strcpy2(*(t + i), *s);
}
Here is the strcpy2 function it calls:
//Copies the information from one array of characters to another using pointers
void strcpy2(char* t, const char* s)
{
for ( ; *t++ = *s++;);
}
It says invalid conversion from char to char*, but where am I trying to convert from char to char*? It seems to me that everything in the code is char*. What am I missing?
I've looked over this code many times and can't seem to find what is wrong. I'm relatively new to pointers, go easy! Thank you very much for any help!
*(t + i)
t is of type char *.
so t + i means char * + i which means "add the value of i to the pointer to make a new pointer". *(t + i) then dereferences that new pointer, the type of that dereferenced expression will be char. So yes, the compiler is correct. You're trying to pass a char into a function that expects a pointer-to-char.
You simply want
strcpy2(t + i, s);
note: You were also dereferencing s, which would cause the same compile error.
The expression *(t + i) in strcpy2(*(t + i), *s); is a char type because the * dereferences the pointer.
Change these statements
for (i = 0; *t; i++);
strcpy2(*(t + i), *s);
to
for (i = 0; *(t + i ); i++);
strcpy2( t + i, s);
Also it would be better to declare the functions as having return type char *. For example
char * strcat2(char* t, const char* s);
I want to make function that can return two new string that is the composition of the old one but I got an above error.
string constru(string num, int pos_be, int pos_end)
{
string f_num="";
string s_num="";
f_num.append(num.at(pos_be));
f_num.append(num.at(pos_end));
num.erase(pos_be);
num.erase(pos_end);
for(int i=0; i<num.size();i++)
{
s_num.append(num.at(i));
}
return f_num,s_num;
}
The Error is at the line f_num.append(num.at(pos_be)) as well as the other lines that I used append with string. Does anyone want know what went wrong here?
The problem here is, the at function returns a char and not a string. But the append function supports a string. So you get this error. Convert the char to string before you append.
f_num.append(std::string(num.at(pos_be)));
f_num.append(std::string(num.at(pos_end)));
f_num.append(std::string(num.at(pos_be)));
f_num.append(std::string(num.at(pos_end)));