user3595344
user3595344

Reputation: 1

Compare with string in if statement doesn't work

I try to compare my two strings that obtained from my scanf and fscanf. I already figured out what is the content inside the each variable. It both show the same strings, but after I compare with this two string in if statement, it doesn't work and execute else statement instead. What's wrong with my codes ?

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;
}

Upvotes: 0

Views: 214

Answers (3)

Jim Amidei
Jim Amidei

Reputation: 116

As others have said, you need to use strcmp to compare strings (really character arrays). Also, you should not pass the address of name (i.e. &name) to the scanf() function.

You have this:

char input[256];
printf("Enter your name: ");
scanf("%s",&input);
....
if(input == stuff[i].name)
...

More correct code would include the following changes:

char input[256];
printf("Enter your name: ");
scanf("%s", input);
....
if (!strcmp(input, stuff[i].name))
....

You should check the definition and use of stuff[i].name as well. scanf() with a %s format character requires a simple char* parameter. The argument to strcmp() is const char* but using char* is fine and will be automatically promoted.

C is more flexible than other languages in that it allows you to obtain the address of variables. You create pointers to variables in this way. However, a variable declared as an array, such as input is already a pointer in a way. Only by providing an index to you dereference the pointer. Specifically:

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.

&input is not good C form. You can kind of think of this as the address of the array but, in reality, input is already the address of the array. There is a use for such types of double-ly addressed variables but your case is not really one of them. Until you've had some practice with arrays and pointers (and their relationship) the following example might be a bit confusing but it does demonstrate where a char** variable might be used.

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;
}

Upvotes: 2

Mestica
Mestica

Reputation: 1529

use the function strcmp in string.h library to compare your strings

Upvotes: 2

Manlio
Manlio

Reputation: 10864

== just checks for pointer equality. Use strcmp instead

Upvotes: 8

Related Questions