-2

My code reads as below: if (array[i].name == name)

Array.name is assigned by an argc, then name is entered by the user. I've used a debugger to check the values, and prior to hitting this if command, array[i].name = Joe and name = Joe.

But the if function doesn't run... I'm lost. Everything I can see is showing that these two strings hold the same word, so why won't the if command recognize that?

Paul Rooney
  • 20,879
  • 9
  • 40
  • 61
  • 1
    If I understand your question correctly, you want to compare two strings, but that's not how you compare strings in C. You can use [strcmp()](https://man7.org/linux/man-pages/man3/strcmp.3.html) for example. – JASLP doesn't support the IES Aug 23 '21 at 04:58
  • What you are comparing is memory addresses. You need a function to iterate through the strings and compare them char by char. – Paul Rooney Aug 23 '21 at 05:04
  • 2
    Does this answer your question? [How do I properly compare strings in C?](https://stackoverflow.com/questions/8004237/how-do-i-properly-compare-strings-in-c) – Krishna Kanth Yenumula Aug 23 '21 at 05:22

1 Answers1

0

You can't compare strings in C like that. As Paul Rooney said in the comment,

You need a function to iterate through the strings and compare them char by char.

So you can either create your own function that iterates through the strings and compares them char by char or you can use strcmp() to compare the strings. Here's how to compare the strings using strcmp():

if(strcmp(array[i].name,name)==0){
//do something if the strings are equal
}

Note:

strcmp() returns an integer indicating the result of the comparison, as follows:

• 0, if the s1 and s2 are equal;

• a negative value if s1 is less than s2;

• a positive value if s1 is greater than s2.