user2817240
user2817240

Reputation: 195

Casting problems in C

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?

Upvotes: 1

Views: 122

Answers (2)

Martin R
Martin R

Reputation: 539795

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;

Upvotes: 3

HAL9000
HAL9000

Reputation: 3751

it should be like this:

result = (unsigned long *) ptr;

Upvotes: 0

Related Questions