Reputation: 6170
I am trying to compare two char arrays. The DoSomething() function, however, never get's called. I can't figure out why.
What am I doing wrong?
//The value the user types in will be stored here:
char TempStorage[]={0,0,0,0,0,0};
//User types in a sequence here:
GetInput();
//One of the sequences that TempStorage will be compared to.
char Sequence[]={1,2,3,4,0,0};
//If the user typed in "123400" then DoSomething().
if(memcmp(TempStorage, Sequence, sizeof(TempStorage) == 0))
{
DoSomething();
}
Upvotes: 1
Views: 80
Reputation: 8830
your characters will resolve to ASCII values so you should specify it as:
char Sequence[]={'1','2','3','4',0,0};
Or compare against an actual string like:
char Sequence[]="1234";
Upvotes: 3
Reputation: 385506
Misplaced paren.
if(memcmp(TempStorage, Sequence, sizeof(TempStorage) == 0))
should be
if(memcmp(TempStorage, Sequence, sizeof(TempStorage)) == 0)
Are those really six inputs that you parsed and placed in an array? Or is TempStorage
a string of ASCII characters that was input? If it's the latter, you should be using a string comparison, and you should be using the proper ASCII codes for the character you want to compare. (This is 0x30
or '1'' for
1.) A string comparison stops as soon as one of the strings is a NUL (
'\0'`), meaning you don't compare meaningless padding if any exists.
char *input = GetInput();
if (strcmp(input, "1234") == 0) { ... }
Upvotes: 7