C++: invalid conversion from char to char*? - c++

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);

Related

void * assignement from C to CPP

I'm wondering why this code works in C and not in C++
void* dum;
dum = "dum";
I have the C++ error
In function 'int main()':
8:10: error: invalid conversion from 'const void*' to 'void*' [-fpermissive]
Any C++ equivalent?
I'm wondering why this code works in C and not in C++
It doesn't work in C++ because string literal is an array of const char.
Any C++ equivalent?
const char* dum = "dum";
void* dum;
dum = (void *)"dum";
const void* dum = "dum";
const char* dum = "dum";
const char* dum;
dum = "dum";
const void* dum;
dum = "dum";
You would have to cast it back to get the result you want.
String literals have type const char* so your pointer would need to be constalso. You cannot assign something that is const to a pointer to non const.
const void* dum = "dum";
cout << static_cast<const char*>(dum);
Try this coutwithout the cast to see what you get. It would be interpreted as a void pointer...

error: cannot convert 'const char**' to 'const char (*)[64]'

I try to implement source code as below:
bool getParam(char* cmd, char** prm_arr, int num)
{
}
void main()
{
char strC[] = "btOK,btCancel";
char foo[10][10];
bool res = getParam(strC,foo,2);
}
It shows error:
error: cannot convert ‘char (*)[10]’ to ‘char**’ for argument ‘2’ to ‘bool getParam(char*, char**, int)’
bool res = getParam(strC,foo,2);
I think char** and char (*)[10] is similar in this case, isn't it?
The array declared like
char foo[10][10];
is converted to the type char( * )[10] when is passed to the function. And there is no implicit conversion from the type char ( * )[10] to the type char *.
So the function declaration should be
bool getParam(char* cmd, char ( *prm_arr )[10], int num);
That is in expressions with rare exceptions arrays are converted to pointers to array elements type.
If you have an array of the type T as for example
T a[N];
when the array is converted to the type T *.
In the declaration of the array foo the type of its elements is char[10]. So the array is converted to pointer to the element type char ( * )[10]
Pay attention to that the function main shall be declared like
int main()
instead of
void main()

Error when calling method with string: invalid initialization of non-const reference of type ‘size_t&

I'm trying to call the method
bool someMethod(char const * begin, char const * end, size_t & count);
having only a string.
std::string sString = "test";
if (someMethod(....)) {
std::cout << "working!";
}
But I dont know how to convert the string propertly.
I tried the following:
if (someMethod(&sString[0], &sString[sString.length() - 1], sString.length()) {
std::cout << "working!";
}
But I get the following message:
error: invalid initialization of non-const reference of type ‘size_t& {aka long unsigned int&}’ from an rvalue of type ‘std::basic_string<char>::size_type {aka long unsigned int}’
Would be awesome if someone has a tipp.
Thank you!
This is a "belt and suspenders" function signature, because it asks both for the end pointer and a count of characters.
In case you are doing it as an exercise, here is what you need to know to complete it:
you get the pointer to C string inside std::string by calling c_str()
you get the count by calling size()
with the initial pointer and size in hand, you get the end pointer by adding the two together.
Note that since size reference is non-const, you need to get it into a variable before making the call:
size_t count = s.size();
const char *start = s.c_str();
someFunction(start, start+count, count);

C++ invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]

WARNING: Extremely limited knowledge of C++ and coding in general. Please refrain from advanced terminology.
for ( i = 0; i < answer.size(); ++i) {
if (guess == answer.at(i)) { //to display correct letters in answerDisplay
answerDisplay.replace( (2 * i), 1, answer.at(i) );
correctGuesses += 1;
}
Given: answerDisplay and answer are strings.
When I run my program there is a compile-time error at the third line of what I've posted saying:
invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
What's the problem? How can I fix it? All other posts with this error talked about pointer characters but I don't know what those are.
Pointer characters are they way plain strings are implemented in C and C++. In C++ you have the nice class std::string, but string literals are still array of characters. And arrays in C and C++ can be seen as pointers.
For example, "hello" is of type const char[6] (5 characters plus the ending NUL), but it can be trivially converted to const char *, and that in turn can be converted to std::string.
In line 3, the only relevant code is a call to the member function std::string::replace(). There are a lot of overrides of this function (different sets of parameters to be used), but the one the compiler is trying to use is this one:
string& replace (size_t pos, size_t len, const char* s);
As you can see, it takes two numbers and a const char * (an old-string/char-array). But you are passing as third parameter answer.at(i) that is of type char. Hence the error:
invalid conversion from ‘char’ to ‘const char*’
Solution? You can build a string from that char:
answerDisplay.replace( (2 * i), 1, std::string(1, answer.at(i))
Or you can get a substring of the original string instead of a plain character.
answerDisplay.replace( (2 * i), 1, answer.substr(i, 1))

Reversing a char array in C++ causing an error

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.