Can someone tell me why the following isn't working?
Code:#include <string.h>
int main(void)
{
int sW, mVk;
char permanent_Info[4] = { [0 ... 3] = 0xFF};
sW = memcmp(permanent_Info, "FF", 4);
if (!sW)
mVk = 1;
else
mVk = 0;
}
Printable View
Can someone tell me why the following isn't working?
Code:#include <string.h>
int main(void)
{
int sW, mVk;
char permanent_Info[4] = { [0 ... 3] = 0xFF};
sW = memcmp(permanent_Info, "FF", 4);
if (!sW)
mVk = 1;
else
mVk = 0;
}
What do you mean by "isn't working"?
What do you expect to happen?
What exactly are you trying to do on line 6?
Do you know that for most current systems a char is a signed value, meaning that the largest value it can hold is 127?
Other than using a for loop I was looking for a way to verify that permanent_Info was loaded with 0xFF. I would like the mVk =1 in the above situation and it doesn't. Actually had to move int sW, mVk; outside of scope of main as my compiler wasn't recognizing it inside the loop.
You seem to be making a few incorrect assumptions.
0xff is not the same as "FF".
"FF" are two 'F' character codes (70 or 0x46 in ascii or utf-8).
Also, memcmp doesn't compare a single thing a given number of times, unlike memset which writes a single character a given number of times.
memcmp compares two byte sequences, contiguous memory interpreted as unsigned char, of the given length.
You would need to compare to something like "\xff\xff\xff\xff", which is actually 5 bytes long due to the terminating '\0', but you are only comparing the first 4 bytes.
And remember that memcmp, like strcmp, will return a negative value if the first byte string comes before the second, 0 if they are equal, or a positive value if the first comes after the second. It will not necessarily return 1 for a positive value and -1 for a negative value.
Note also that [0...3] is non-standard and a char may be (probably is) signed and so 0xff will be interpreted as -1, which may or may not make a difference depending on what you do with it. It may be best to make it unsigned.
Code:#include <stdio.h>
#include <string.h>
typedef unsigned char uchar;
int main(void)
{
uchar data[4];
memset(data, 0xff, sizeof data);
int result = memcmp(data, "\xff\xff\xff\xff", sizeof data);
printf("%d\n", result);
if (result == 0) printf("equal\n");
else if (result < 0) printf("less\n");
else printf("greater\n");
}
Thank you....That works. I understand your comment on 0xFF vs "FF".