[英]lvalue required as left operand of assignment - What causes this error and how to fix it?
Here is the piece of code.这是一段代码。 Its in C, compiler is CodeBlocks I made a function Replacethings which is supposed to replace all characters that are spaces, commas, and exclamation points with *.
它在 C 中,编译器是 CodeBlocks 我做了一个 function Replacethings,它应该用 * 替换所有字符,即空格、逗号和感叹号。 Seemingly an easy task, and yet so hard.
看似轻而易举的任务,实则艰辛。 The input has to be 25 characters or less.
输入必须是 25 个字符或更少。
void Replacethings( char *StrongOfChars[25]){
for(int i =0; i<25; i++){
if(*StrongOfChars[i]=' ' || *StrongOfChars[i] = ',' || *StrongOfChars[i] = '!'){
*StrongOfChars[i]= '*';
printf("%c", StrongOfChars[i]);
}
}
}
printf("Enter your favorite quote (Has to be less than 25 characters)!");
char StronKK[25];
scanf("%s", &StronKK);
Replacethings(&StronKK);
printf("\n Your favorite squote is now: %s +", StronKK);
*StrongOfChars[i]=' '
means you assign the value ' ' to the first character of the i'th char pointer: *StrongOfChars[i]=' '
表示将值 ' ' 分配给第 i 个 char 指针的第一个字符:
if(*StrongOfChars[i]=' ' || *StrongOfChars[i] = ',' || *StrongOfChars[i] = '!'){
You want to use ==
instead.您想改用
==
。 The first expression of the body of the if
statement is: if
语句主体的第一个表达式是:
*StrongOfChars[i]= '*';
which probably should be part of your if expression instead, ie:这可能应该是您的 if 表达式的一部分,即:
if(
*StrongOfChars[i] == ' ' ||
*StrongOfChars[i] == ',' ||
*StrongOfChars[i] == '!' ||
*StrongOfChars[i] == '*'
) {
...
}
or you can use strchr(",,*", *StrongOfChars[i])
instead of those 4 conditions.或者您可以使用
strchr(",,*", *StrongOfChars[i])
代替这 4 个条件。
The code is poorly formatted, so you cannot see that the }
before the 2nd print statement is in the wrong place so the print statement is not in a function.代码格式不正确,因此您看不到第二条打印语句之前的
}
位置错误,因此打印语句不在 function 中。 Maybe remove it or you forgot a main()
function?也许删除它或者你忘记了
main()
function?
As you declared the array StronKK
like正如您声明的数组
StronKK
一样
char StronKK[25];
then the expression &StronKK
has the type char ( * )[25]
.那么表达式
&StronKK
的类型为char ( * )[25]
。
So you have to write in the call of scanf
所以你必须在
scanf
的调用中写
scanf( "%24s", StronKK );
On the other hand, the function parameter has the type char *StrongOfChars[25]
另一方面,function 参数的类型为
char *StrongOfChars[25]
void Replacethings( char *StrongOfChars[25])
that is adjusted by the compiler to the type char **
由编译器调整为类型
char **
void Replacethings( char **StrongOfChars )
The types char ( * )[25]
and char **
are not compatible types and the compiler should issue a diagnostic message. char ( * )[25]
和char **
类型是不兼容的类型,编译器应该发出诊断消息。
You need to declare the function like您需要像这样声明 function
void Replacethings( char *StrongOfChars )
or或者
void Replacethings( char StrongOfChars[25])
or或者
void Replacethings( char StrongOfChars[])
and call it like并称它为
Replacethings( StronKK );
Pay attention to that the user can enter a string with the length less than 25
.注意用户可以输入长度小于
25
的字符串。 So using the magic number 25
within the function is a bad idea and can result in undefined behavior.因此,在 function 中使用幻数
25
是一个坏主意,并且可能导致未定义的行为。
Also instead of the equality operator ==
you are using the assignment operator =
within the for loop of the function.此外,您在 function 的 for 循环中使用赋值运算符
=
而不是相等运算符==
。
The function can be defined the following way function 可以通过以下方式定义
void Replacethings( char StrongOfChars[])
{
for ( ; *StrongOfChars != '\0'; ++StrongOfChars )
{
if ( *StrongOfChars == ' ' ||
*StrongOfChars == ',' ||
*StrongOfChars == '!' )
{
*StrongOfChars = '*';
}
}
}
It would be also reasonable to check whether a character is a tab character.检查一个字符是否是制表符也是合理的。 That is the if statement can look
那就是if语句可以看
if ( *StrongOfChars == ' ' ||
*StrongOfChars == '\t' ||
*StrongOfChars == ',' ||
*StrongOfChars == '!' )
{
*StrongOfChars = '*';
}
Or you can write it lime或者你可以写成石灰
if ( strchr( " \t,!", *StrongOfChars ) )
{
*StrongOfChars = '*';
}
To use the function strchr
you need to include header <string.h>
.要使用 function
strchr
,您需要包含 header <string.h>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.