I'm trying to take the sqrt of a variable int *msg in C.
I am trying to get the squareroot of message
int main(int argc, char **argv) {
int *msg;
...
while(N!=0) {
if (g_continue == 0) {
break;
}
if(mq_timedreceive(qdes, (char *)&msg, sizeof(int), 0, &ts) == -1){
printf("Error Message Missing");
}
if(sqrt((float *)msg)*sqrt((float *)msg) == (int)sqrt((float *)msg)*(int)sqrt((float *)msg))
{
printf("%i %n %f", cID, msg, sqrt((float *)msg));
}
N--;
}
}
any ideas how to make this work?
msgis a pointer, and so is(float *)msg. Use*(float *)msginstead.Howover, notice that you are not converting the
intnumber into afloat, you are readingmsgas a float. Theres an important difference here, because the number1for instance, read as a float, is not1.0f. If you want to convert the value ofmsgtofloatkeeping it'sintmeaning, use(float)(*msg)instead.I'm not sure what
mq_timedreceive()does so I don't know which one is correct for you, so you will have to figure that out yourself.In addition, make sure you initialized the pointer
int *msgbefore using it. I don't know if you did, sinse you omitted part of your code.