C - [Error] invalid conversion from 'char' to 'const char*' [-fpermissive]
Solution 1
Try this:
int main( void ) {
char word[26], reverse[26];
int length, i;
printf("Enter word: ");
scanf("%s", &word);
length=strlen(word);
for (i=0; i<length; i++) {
reverse[i] = word[length - i - 1];
}
/* Put \0 to terminate the string */
reverse[length]='\0';
printf("%s\n", reverse);
}
Note that in the for loop you have to use only <
and not <=
because you'll get out of bounds of the array (negative index) and you have to put the \0
terminator to terminate your reverse string. You don't need letter
and btw strcpy
works not with single chars, since a single char is not a string (because of the missing string terminator (\0
)).
Edit:
int main( void ) {
char word[26], reverse[26];
int length, i;
printf("Enter word: ");
scanf("%s", &word);
length=strlen(word);
for (i=0; i<length; i++) {
reverse[i] = word[length - i - 1];
/* Put \0 to terminate the string */
reverse[i + 1]='\0';
printf("%s\n", reverse);
}
}
Solution 2
Problems:
- You pass the address of an array to scanf, rather than passing the array.
- You don't copy the null termination correctly.
- strcat must have null terminated strings as parameters, it cannot work with single characters. You don't need to use it here anyway.
Fixed program:
#include <stdio.h>
#include <string.h>
int main (void)
{
char word[26];
char reverse[26];
size_t length;
size_t i;
printf("Enter word: ");
scanf("%s", word);
length=strlen(word);
for (i=0; i<length; i++)
{
reverse[i] = word[length-i-1];
}
reverse[i] = '\0';
printf("Reverse: %s\n", reverse);
return 0;
}
Output:
Enter word: stackoverflow
Reverse: wolfrevokcats
Admin
Updated on July 18, 2022Comments
-
Admin almost 2 years
There's a bunch of threads about C++, but I'm stuck with regular C here and can't find an answer anywhere.
#include <stdio.h> #include <conio.h> #include <string.h> main() { char word[26], letter, reverse[26], length; printf("Enter word: "); scanf("%s", &word); length=strlen(word); for (int i=0; i<=length; i++) { letter = word[length-i]; strcpy(reverse, letter); printf("%c\n", reverse); } getch(); }
I'm getting errors as the title states.
13 25 ~ [Error] invalid conversion from 'char' to 'const char*' [-fpermissive]
51 18 C:\Program Files (x86)\Dev-Cpp\MinGW64\x86_64-w64-mingw32\include\string.h [Note]
initializing argument 2 of 'char* strcpy(char*, const char*)'
What the code should do is reverse the written word and type it out line by line, by each last letter. Etc. - Type in "food" and the program should output:
d
od
ood
food
I know I messed up somewhere in the 'letter', but I have no clue how to fix it. Also I'm trying to avoid using std::, personal prefrence.
-
kaylum over 7 yearsThe message seems clear.
letter
is not achar *
as required (ie, not a string). It is a singlechar
. -
user3386109 over 7 yearsYou seem to be confused about the difference between a
char
and a string. In C, a string is an array ofchar
. And thestrcpy
function expects two strings as arguments. -
qxz over 7 yearsMore specifically, in C a string is a null-terminated array of
char
s. -
qxz over 7 years@Tremmert: You want to push
letter
onto the beginning ofreverse
? -
David C. Rankin over 7 yearsIf it still isn't clear, when you declare
char word[26]
,word
is a pointer to the first character in a 26 character array. It is a pointer already. So when you need to provide a pointer toscanf
, it is simplyscanf("%s", word);
(no&
beforeword
). Also always check the return ofscanf
to validate a successful conversion. -
user3629249 over 7 yearsdo not
#include
header files those contents are not used: I.E.conio.h
-
user3629249 over 7 yearsthe function
main()
only has a very small number of valid return types (regardless of what Visual Studio allows) in the current scenario the signature formain()
should be:int main( void )
-
user3629249 over 7 yearsWhen calling any of the
scanf()
family of functions, 1) always check the returned value, not the parameter value, to assure the operation was successful. 2) when using the%s
format specifier, always include a MAX CHARACTERS modifier that is 1 less than the length of the input buffer so as to avoid any buffer overflow and the resulting undefined behavior, which can lead to a seg fault event -
user3629249 over 7 yearsthe posted code contains some 'magic' numbers. 'magic' numbers are numbers with no basis. I.E. 26. Strongly suggest using a
#define
orenum
statement to give those magic numbers meaningful names, then use those meaningful names through out the code. -
user3629249 over 7 yearsfor ease of readability and understanding: follow the axiom: only one statement per line and (at most) one variable declaration per statement.
-
user3629249 over 7 yearsusing the function:
getch()
is not portable. Suggest using:int ch; while( (ch = getchar()) != EOF && '\n' != ch ); getchar();
-
user3629249 over 7 yearsthis line:
strcpy(reverse, letter);
is nonsense. The contents ofletter
is a single character and the function:strcpy()
expects the second parameter to be a pointer. -
Kami Kaze over 7 years@user3629249 putting all this in an answer would have been easier and imho the better place (plus you get rewards for it)
-
user3629249 over 7 years@KamiKaze, other than the comment about
strcpy()
, none of the comments answer the OPs question. So they should not be used as an answer.
-
-
user3386109 over 7 yearsSorry, but replacing one problem with another is not an answer.
-
kaylum over 7 yearsGiving a wrong answer and then saying why it is wrong is still not an answer. Just don't go there. Go straight to a fully correct answer.
-
David C. Rankin over 7 yearsRead my comment to the question above.
-
Kami Kaze over 7 yearsthis is just plain wrong. reverese is an array so 'reverse' already is the address of the array, no need for '&'. And letter is still not a string just a pointer to a char.
-
Dhruv Khatri over 7 yearsthan why it will gives an error in code.? @KamiKaze
-
Dhruv Khatri over 7 yearsIncompatible integer to pointer conversion passing 'char' to parameter of type 'const char *'; take the address with & this what my IDE suggested. @KamiKaze First make sure you are right after that give suggestion
-
alk over 7 years
strcat()
expects address to the 1st element of a C-"string", that is a0
-terminated array ofchar
. -
Dhruv Khatri over 7 yearsSo why my IDE suggest me to do like this @alk. And it will gives me the right answer also.
-
Kami Kaze over 7 yearsyou just have to modify the second argument. The first one is okay as it is. The clue is here 'initializing argument 2 of
char* strcpy(char*, const char*)
. Don't know about your IDE, but I prefer reading the API and work with it than what some heuristic suggest might be right. As I saidreverse
already is an address, you could do&reverse[0]
if it satisfies you. Insert snarky remark about your remark here.... -
Lundin over 7 years
strcat
must get null terminated strings as parameters, you cannot pass single chars. If this program "seems to work" it is because of sheer luck. -
Lundin over 7 years(wolf revokes cats, wtf!?)
-
Lundin over 7 yearsOops, didn't see this one and therefore posted a nearly identical answer. Have an up-vote.
-
alk over 7 years@KamiKaze: "The first one is okay as it is" just to avoid misunderstandings:
char reverse[26]; ... strcat(reverse, ...):
is okay. Doingstrcat(&reverse, ...
is not. -
Kami Kaze over 7 years@alk I should have been clearer, the first argument is good in the original code. Thx for clarifying this.
-
Admin over 7 yearsIt works well, but what I require is the 'reverse' being outputted multiple times, +1 letter at a time. So "food" should get output as d od ood food Thank you for the code, I'll work with it. Still getting all the quirks of C.
-
gmug over 7 yearsIn that case just move the printf line at the end of the
for
loop and don't forget to add the terminating\0
before you call the printf:reverse[i + 1]='\0';