Function error 'expected expression before char'?
I have created the following program which allows a user to guess a word 3 times before ending the program. I'm using a function to read the users input. When I compile the program I get the error 'expected expression before char'. Some feedback would be great thanks!
#include <stdio.h>
#include <string.h>
void get_user_input(char *guess[10]);
void get_user_input(char *guess[10])
{
printf("Please guess the word: n");
scanf("%s", guess);
}
int main(void)
{
const char secret[10] = "pink";
char guess[10];
int i;
for (i=0; i < 3; i++)
{
get_user_input(char *guess[10]);
if (strcmp(secret, guess)==0)
{
printf("Your guess was correct");
return 0;
}
else
{
printf("Your guess was incorrect. Please try againn");
}
}
return 0;
}
You have an extra char
here:
for (i=0; i < 3; i++)
{
get_user_input(char *guess[10]);
Just get rid of it. You just need to pass the variable in.
get_user_input(guess);
EDIT :
The other problem seems to be this function:
void get_user_input(char *guess[10]);
change it to this:
void get_user_input(char *guess)
{
printf("Please guess the word: n");
scanf("%s", guess);
}
and it should work. However, be aware that you run the risk of overrunning your guess
array.
Inside the loop, write
get_user_input(guess);
instead of
get_user_input(char *guess[10]);
.
In addition, you should delete the useless prototype
void get_user_input(char *guess[10]);
and change the following function's signature to
void get_user_input(char * guess)
to let a pointer to the first char of the array be passed instead of a pointer to a pointer to the first char which will not compile. A side issue is that char *guess[10]
means an array of 10 pointers to char.
PS: It helps to post the offending line number in addition to the error message.
PPS: You have a buffer overrun memory error if the use enters long answers. You can use fgets to avoid this.
链接地址: http://www.djcxy.com/p/84390.html上一篇: 功能参数作为switch语句中的情况
下一篇: 函数错误'char'之前的预期表达式?