error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive] [duplicate] - c++

This question already has answers here:
invalid conversion from ‘const char*’ to ‘char’
(2 answers)
Closed 6 years ago.
#include<stdio.h>
#include<iostream>
#include<fstream>
#include<string.h>
using namespace std;
class base {
public:
int lookup(char c);
}; // class base
int base::lookup(char c)
{
cout << c << endl;
} // base::lookup
int main() {
base b;
char c = "i";
b.lookup(c);
} // main
On Compiling above code I am getting below error :
g++ -c test.cpp test.cpp: In function ‘int main()’: test.cpp:20:10:
error: invalid conversion from ‘const char*’ to ‘char’ [-fpermissive]

Try replacing
char c = "i";
with
char c = 'i';

"i" is not a character, it's a character array that basically decays to a pointer to the first element.
You almost certainly want 'i'.
Alternatively, you may actually want a lookup based on more than a single character, in which case you should be using "i" but the type in that case is const char * rather than just char, both when defining c and in the base::lookup() method.
However, if that were the case, I'd give serious thought to using the C++ std::string type rather than const char *. It may not be necessary, but using C++ strings may make your life a lot easier, depending on how much you want to manipulate the actual values.

"i" is a string literal, you probably wanted a char literal: 'i'.
String literals are null terminated arrays of const char (which are implicitly converted to char const* when used in that expression, hence the error).
char literals are just chars

Related

Invalid conversion from ‘const char*’ to ‘char*’ with ```rindex``` function

I want to do something with string using the index and rindex function under c++17, but when I compile the program, this error poped up:
debug.cpp: In function ‘int main()’:
debug.cpp:7:27: error: invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive]
7 | char* index_first = index(str,'c');
| ~~~~~^~~~~~~~~
| |
| const char*
debug.cpp:9:27: error: invalid conversion from ‘const char*’ to ‘char*’ [-fpermissive]
9 | char* index_last = rindex(str,'c');
| ~~~~~~^~~~~~~~~
| |
| const char*
Then I checked this program online, every function defines of index and rindex I saw are the same:
char* index(const char* s,int c);
char* rindex(const char* s,int c);
And heres my debug code:
#include <stdio.h>
#include <string.h>
int main()
{
const char* str = "abcdefgabcdefg";
char* index_first = index(str,'c');
printf("the first index is %ld\n",index_first - str + 1);
char* index_last = rindex(str,'c');
printf("the last index is %ld\n",index_last - str + 1);
return 0;
}
I compile it using:
g++ -o debug debug.cpp -std=c++17
I want to know why can't I do that and the right way to use index and rindex functions and (or) the right function defines please.
Heres my environment:
Ubuntu LTS 20.04 (x64)
g++ (Ubuntu 9.4.0-1ubuntu1~20.04.1) 9.4.0
Thank you for all the help.
You are trying to assign returned pointers of the type const char * that are used within the functions to pointers of the type char *
Actually the functions you are calling are declared like
const char* index(const char* s,int c);
const char* rindex(const char* s,int c);
In C++ the functions can be overloaded like
const char* index(const char* s,int c);
const char* rindex(const char* s,int c);
and
char* index(char* s,int c);
char* rindex(char* s,int c);
the same way as some other standard C functions as for example the standard C function strchr.
So you should write
const char* index_first = index(str,'c');
printf("the first index is %td\n",index_first - str + 1);
const char* index_last = rindex(str,'c');
printf("the last index is %td\n",index_last - str + 1);
The result of subtracting two pointers has the signed integer type ptrdiff_t. So you need to use the conversion specifier %td instead of %ld.
From the C Standard (7.21.6.1 The fprintf function)
7 The length modifiers and their meanings are:
t Specifies that a following d, i, o, u, x, or X conversion specifier applies to a ptrdiff_t or the corresponding unsigned integer
type argument; or that a following n conversion specifier applies to a
pointer to a ptrdiff_t argument.

Why using string instead of char shows error in the following code? [duplicate]

This question already has answers here:
How to get length of a string using strlen function
(7 answers)
Closed 1 year ago.
#include<bits/stdc++.h>
using namespace std;
int main()
{
string str="ABC";
int n=strlen(str);
cout<<n;
}
This shows error which is:
error: cannot convert ‘std::string {aka std::basic_string}’ to ‘const char*’ for argument ‘1’ to ‘size_t strlen(const char*)’
int n=strlen(str);
But this works fine:
#include<bits/stdc++.h>
using namespace std;
int main()
{
char str[]="ABC";
int n=strlen(str);
cout<<n;
}
What is the reason behind this?
strlen is a function that counts the nuber of characters between the memory adress specified by the const char* argument and the first \0. So you cannot pass a string as argument, since it does not represent any memory address. Instead use
str.size()
or (not efficient also not expected result if the string contains \0)
strlen(str.c_str()

Why scanf is not taking any input and giving Error in hackerearth editor and codeblocks

I want to take string input with white spaces.
This is my code:
#include<iostream>
#include <stdio.h>
#include<string>
using namespace std;
int main()
{
string name;
printf("\nEnter the name : ");
scanf ("%[^\n]%*c", name); //it does not work
//getline(cin, name); //it works fine
cout<<name<<endl;
return 0;
}
Error Massage on hackerearth editor and codeblocks:
In function ‘int main()’:
16:29: error: cannot pass objects of non-trivially-copyable type ‘std::string {aka class std::basic_string}’ through ‘...’
16:29: warning: format ‘%[^
:string {aka std::basic_string}’ [-Wformat=]
16:30: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
string, or in fact std::string, is a C++ type from the 1990s (ish).
scanf is a C function from 1066. It has no idea what to do with a C++ type.
Don't mix and match; either use C++ features or use C features.

strcmp cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘const char* [duplicate]

This question already has answers here:
strcmp or string::compare?
(6 answers)
Closed 8 years ago.
Apologies in advance for the elementary nature of the question.
I am trying to use the strcmp function to test two strings for matching characters.
I reduced the issue to the simple code below:
#include <iostream>
#include <cstring>
using namespace std;
void compareStrings(string, string);
int main()
{
string string1 = "testString", string2 = "testString";
compareStrings(string1, string2);
return 0;
}
void compareStrings(string stringOne, string stringTwo)
{
if (strcmp(stringOne,stringTwo) == 0)
cout << "The strings match." << endl;
else
cout << "The strings don't match." << endl;
}
Could someone explain the following compiler error message?
./newProgram.cpp: In function ‘void compareStrings(std::string, std::string)’:
./newProgram.cpp:17:32: error: cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘const char*’ for argument ‘1’ to ‘int strcmp(const char*, const char*)’
if (strcmp(stringOne,stringTwo) == 0)
^
Thanks!
Xz.
strcmp is for C strings (null-terminated char *). string::compare is for C++ strings.
If you really want to use strcmp with your std::string, you can use string::c_str() to get a pointer to the underlying C-string:
if (strcmp(stringOne.c_str(), stringTwo.c_str()) == 0)
But of course, if you're using C++, you should actually use C++, and make use of std::string's == overload.
if (stringOne == stringTwo)

strcmp[c++] error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]

Where am doing wrong in this code? I need only in char types, please don't suggest to use std::string.
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char *mystring="C:/windows";
char last_char;
last_char = mystring[strlen(mystring)-1];
cout<<"Input: " <<mystring<<endl;
if(strcmp(last_char,";")!=0)
{
strcat(mystring,";");
}
cout<<"Output: "<<mystring<<endl;
return 0;
}
Output:
Compilation error time: 0 memory: 3340 signal:0
prog.cpp: In function ‘int main()’:
prog.cpp:7:17: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
char *mystring="C:/windows";
^
prog.cpp:11:25: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
if(strcmp(last_char,";")!=0)
^
In file included from prog.cpp:2:0:
/usr/include/string.h:140:12: error: initializing argument 1 of ‘int strcmp(const char*, const char*)’ [-fpermissive]
extern int strcmp (const char *__s1, const char *__s2)
Don't use strcmp, it expects a null terminated characters sequence. Instead, use direct comparison:
if (last_char == ';') ...
Also, your code invokes undefined behavior in the strcat() call. my_string was initialized with a string literal, thus, you are not allowed to modify it, since the implementation is free to place it in read-only memory (and typically will do so).
You can declare it like this instead:
char mystring[12] = "C:/windows"; // space for one more char
last_char is not a string. It is a character. You can't compare a char with string.
Try this instead
if (last_char == ';') {...}
Statement
strcat(mystring,";");
invokes undefined behavior. You can't modify a string literal as it resides in read only section of the memory.