1

I get this error: assignment makes pointer from integer without a cast. I am using threads.

This is what I am doing:

void *calculate(void *arg) {
unsigned long *array;
.....
return array;
}

int main() {
unsigned long *result;
void *ptr;
...
p_thread_create(...);
p_thread_join(td, &ptr);
...
result = *((unsigned long *) ptr); /* This is the line where my error occurs */

return 0;
}

The calculate function returns an array of unsigned long type. The return value of the thread is stored in a void pointer and because it is void I cast it to * unsigned long.

But it's not working.

Any ideas?

user2817240
  • 195
  • 1
  • 5
  • 13
  • Figure out the type of result. Figure out the type of the expression that you're assigning to it. Note that they aren't the same. – Jim Balter Dec 09 '13 at 21:16
  • Try changing this: `result = *((unsigned long *) ptr);` to this: `result = ((unsigned long *) ptr);`. The first one is casting `ptr` to `unsinged long`, as the `*` operator dereferences the pointer. – Pacha Dec 09 '13 at 21:17

2 Answers2

3

If you want to return a single number (to which ptr points) from the thread function, use

unsigned long result = *((unsigned long *) ptr);

If you want to return a pointer to an array of unsigned long, use

unsigned long *result = ptr;
Martin R
  • 529,903
  • 94
  • 1,240
  • 1,382
  • Reading the question again I think this is wrong, because your thread function calculates and returns an array, not a single number. Will delete answer shortly. – Martin R Dec 09 '13 at 21:25
  • 2
    Why not update this instead with the other possibility as well (i. e. `result = ptr;`)? That would make this answer the most complete one. –  Dec 09 '13 at 21:26
0

it should be like this:

result = (unsigned long *) ptr;
HAL9000
  • 3,562
  • 3
  • 25
  • 47
  • 2
    No, it should be `result = ptr;` (assuming that he really wants the pointer and not the pointed value). –  Dec 09 '13 at 21:22
  • You are wrong. I respond now because I compiled it to be sure of what I thought: your instruction does an implicit cast, while mine instuction does it explicitally. The result is the exactly same. – HAL9000 Dec 09 '13 at 21:39
  • 1
    I am not wrong. There's no such thing as an implicit cast - there is implicit *conversion.* A cast, by definition, is explicit. And it **should** be `result = ptr;` because it's more readable -- including an explicit cast has no benefits, it only makes the code ugly. –  Dec 09 '13 at 21:40
  • Anyway the result is the same. Personally, as I both program in Java and C, I prefer to see casts everywhere two different types are assigned. In both cases there are no warnings. – HAL9000 Dec 09 '13 at 21:44
  • 1
    Your reasoning doesn't make sense - Java is not C. The explicit cast is bad practice in C. [Link.](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858) –  Dec 09 '13 at 21:45
  • Well it returns a void pointer so wouldn't it make sense to cast it? – user2817240 Dec 09 '13 at 21:51
  • Otherwise it could confuse the reader I think, who is expecting a cast. – user2817240 Dec 09 '13 at 21:52
  • @H2CO3 I already seen that post and I don't agree with it. I simply prefer to see explicit cast as in C++ and Java. I don't consider the four reasons there proposed as real dangers for the code, while I consider an explicit cast an hint for future developers (although not necessary). Maybe I'm wrong, obviously, but I don't consider that post as Bible. I respect your opinion but I also always cast malloc and I will do it until someone gives me a clear contrary reason (performance, real danger, etc). – HAL9000 Dec 09 '13 at 21:58
  • @HAL9000 Agree with it or not, the reasons enumerated there are hard facts. It's considered bad in C to include the cast. (It's not better in C++ either, but it is mandatory there.) So yes, you are wrong, and I hope I won't ever need to work with code you've written. That's all I've got. –  Dec 09 '13 at 22:00