-2

I am working on mobile communication Simulator based on C language. I have two different IPs which are stored as Strings in different format.

  1. For User it is XYZ[20]
  2. For Access Gateway, it is ABC[No. of Users][20]

Now, I need to compare both for one algorithm. but I am confused that how should I do them as both are different arrays. Is there any way to compare both 1D with 2D array ?

Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
Farhan
  • 3
  • 3
  • And I'm confused with your question. Could you be more specific and maybe show some relevant parts of your declarations? – Jabberwocky Apr 07 '14 at 14:40

1 Answers1

0
#include <string.h>
...
for ( int i = 0; i < num_users; i++ )
{
  if ( strcmp( ABC[i], XYZ ) == 0 )
  {
    // strings are equal
  }
  else
  {
    // strings are not equal
  }
}

ABC[i] is the i'th string stored in ABC.

John Bode
  • 119,563
  • 19
  • 122
  • 198
  • `strncmp(ABC[i], XYZ, 20)` should be safer. – Jarod42 Apr 07 '14 at 15:04
  • Thanks John Bode, It worked for me – Farhan Apr 07 '14 at 15:42
  • @Jarod: given that the array sizes are the same, the above is only safer if one or the other doesn't have a 0 terminator, meaning there was a coding error somewhere earlier in the code. I would rather spend those cycles verifying those arrays on assignment than safely comparing invalid data. – John Bode Apr 07 '14 at 17:08