-
memcmp ?
Can someone tell me why the following is not working?
Code:
char temp[HOW_MANY_SENSORS] = { [0 ... (HOW_MANY_SENSORS - 1)] = 0xFF};
Code:
test = memcmp((const void *)temp, (const void *)0xFF, 1);
test is an int.
If I change temp to 0xFF I get the expected test = 0 but for the life of me I cannot get temp to work...I've tried &temp, &temp[0] ....in all instances test comes back as a non-zero integer
I've also tried strncmp and get the same type of results??
-
Why would you expect some random byte at memory location 0xFF would equal 0xFF?
I would expect a segfault, actually.
-
There is nothing random here...I have an actual use case where a segment in memory will initialize to 0xFF then over time the space can change....I need a conditional to check if the space is void (ie 0xFF)....how can I do that over HOW_MANY_SENSORS????
my example above is kept simple so someone can show me what I am doing wrong
-
Casting (in this case, with (const void *)) is a way to tell the compiler that you know what you're doing. You don't know what you're doing (otherwise you wouldn't be asking us for help), so remove the casts and report what errors you get from the compiler.
-
Why don't you start with
Code:
for ( i = 0 ; i < HOW_MANY_SENSORS ; i++ ) {
if ( temp[i] == 0xff ) {
// something
}
}
Instead of digging yourself a hole in your attempt at micro-optimisation.
-
Code:
char test[3] = { [0 ... 2] = 0xFF};
int flag = 0;
flag = strncmp(test, 0xFF, 3);
Yes I could do the for loop but was hoping to do it like above bcz I am placing this in a conditional &&
I get the following warnings from the compiler:
Description Resource Path Location Type
#154-D conversion of nonzero integer to pointer j.c /ThermalSensorNoLib/Source line 18 C/C++ Problem
#169-D argument of type "int" is incompatible with parameter of type "const char *" j.c /ThermalSensorNoLib/Source line 18 C/C++ Problem
but if I place (const char *) in front of 0xFF the warning disappears. 0xFF is NOT a memory address it is just a char that I fill the array with...
-
Write the loop and place it in a function that returns a value that you can then check in a conditional. Problem solved.