C 与if语句中的字符串进行比较';行不通

C 与if语句中的字符串进行比较';行不通,c,string,if-statement,C,String,If Statement,我尝试比较从scanf和fscanf中获得的两个字符串。我已经弄清楚了每个变量中的内容。它都显示相同的字符串,但在我与if语句中的这两个字符串进行比较后,它不起作用,而是执行else语句。我的密码怎么了 int main(void) { ... char input[256]; printf("Enter your name: "); scanf("%s",&input); fp = fopen(_FILE,"r+"); for(i=0;i<textlines(_FILE);i+

我尝试比较从scanf和fscanf中获得的两个字符串。我已经弄清楚了每个变量中的内容。它都显示相同的字符串,但在我与if语句中的这两个字符串进行比较后,它不起作用,而是执行else语句。我的密码怎么了

int main(void)
{
...
char input[256];
printf("Enter your name: ");
scanf("%s",&input);

fp = fopen(_FILE,"r+");
for(i=0;i<textlines(_FILE);i++)
{
 fscanf(fp,"%s %d",stuff[i].name,&stuff[i].salary);
 if(input == stuff[i].name)
 {
 // Update name here
 }
 else
 {
 printf("Not Found");
 }
}
return 0;
}
int main(无效)
{
...
字符输入[256];
printf(“输入您的姓名:”);
scanf(“%s”,&input);
fp=fopen(_文件,“r+”);

对于(i=0;i
=
只需检查指针是否相等。使用
strcmp
而不是使用函数strcmp in string.h library来比较字符串

正如其他人所说,您需要使用strcmp来比较字符串(实际上是字符数组)。此外,您不应该将name(即&name)的地址传递给scanf()功能

你有这个:

char input[256];
printf("Enter your name: ");
scanf("%s",&input);
....
if(input == stuff[i].name)
...
更正确的代码将包括以下更改:

char input[256];
printf("Enter your name: ");
scanf("%s", input);
....
if (!strcmp(input, stuff[i].name))
....
您应该检查stuff[i].name的定义和使用情况。带有%s格式字符的scanf()需要一个简单的char*参数。strcmp()的参数是const char*,但使用char*可以,并且会自动升级

C语言比其他语言更灵活,因为它允许您获取变量的地址。您可以通过这种方式创建指向变量的指针。但是,声明为数组的变量(如input)在某种程度上已经是指针。仅通过向您提供索引来解引用指针。具体而言:

char input[256];
input is a pointer to the storage of 256 char's
input can be thought of as a char* variable
input[0] is the first char in the array
input[1] is the second char in the array
input+1 is a pointer to the second char in the array.
input+0 (or simply input) is a pointer to the first char in the array.
&输入不是很好的C形式。你可以把它看作数组的地址,但实际上,输入已经是数组的地址了。这种类型的双地址变量是有用的,但你的情况并不是真正的其中之一。直到你对数组和指针(以及它们的关系)有了一些实践下面的示例可能有点混乱,但它确实演示了在何处可以使用char**变量

int allow_access_to_private_data(char ** data)
{
  static char mystring[] = "This is a private string";
  if (!data) return -1;
  *data = mystring;
  return 0;
}

int main(int argc, char* argv[])
{
  char* string;
  if (!allow_access_to_private_data(&string))
    printf("Private data is: %s\n", string);
  else
    printf("Something went wrong!\n");
  return 0;
}

请把缩进的部分整理一下