10

I'm trying to find out the proper way to return an integer from a void * function call within C.

ie ..

#include <stdio.h>

void *myfunction() {
 int x = 5;
 return x;
}

int main() {
  printf("%d\n", myfunction());
  return 0;
}

But I keep getting:

warning: return makes pointer from integer without a cast

Is there a cast I need to do to make this work? It seems to return x without problem, the real myfunction returns pointers to structs and character strings as well which all work as expected.

3
  • 8
    Why are you doing this? Commented Aug 8, 2013 at 3:26
  • 5
    myfunction return type is void* but the type of x is int. Why do you wish to make it work ? What is the real function you are dealing with ? Commented Aug 8, 2013 at 3:27
  • 1
    A good enough reason: maintaining code that someone else did. Commented Jun 5, 2015 at 15:16

4 Answers 4

9

It's not obvious what you're trying to accomplish here, but I'll assume you're trying to do some pointer arithmetic with x, and would like x to be an integer for this arithmetic but a void pointer on return. Without getting into why this does or doesn't make sense, you can eliminate the warning by explicitly casting x to a void pointer.

void *myfunction() {
 int x = 5;
 return (void *)x;
}

This will most likely raise another warning, depending on how your system implements pointers. You may need to use a long instead of an int.

void *myfunction() {
 long x = 5;
 return (void *)x;
}
Sign up to request clarification or add additional context in comments.

5 Comments

This does seem to make the warning disappear, and I'd love to know why this works or if this is actually undefined behavior. Everyones answers made sense to me, but this does exactly what I was trying to do.
The other answers assume you wanted to return a pointer to the integer, which is very different than the integer as a pointer. The warning you were getting was to make sure you realized you were turning an int into a void pointer. Casting a pointer to a long is perfectly well defined, although it often is easier to work only with pointers to avoid confusion. As void pointer arithmetic is illegal, consider using a char* pointer, which will behave as an int during arithmetic, since its size is one byte.
It might be worth mentioning that there might be a platform where an int is wider then a void *, so converting the former to the latter would lose bits. So if storing an integer in a pointer variable is really necessary one might consider declaring the integer as intptr_t x; or uintptr_t x;, as intptr_t and uintptr_t are guaranteed by the C standard to fit into a pointer variable.
@user2662982: This is well defined behaviour. However, it might lose bits. Please see my comment a bove on this.
Note that if this is what you want to do, you can also write it as return (void *) 5;.
2

A void * is a pointer to anything, you need to return an address.

void * myfunction() {
  int * x = malloc(sizeof(int));
  *x=5;
  return x;
}

That being said you shouldn't need to return a void * for an int, you should return int * or even better just int

6 Comments

I understand your intent but there is no new operator in C.
@Mahesh whoops, wasn't paying attention to the tags
Whatever, he can always change it to malloc(sizeof(int)).
@JosuéMolina fixed it, he should be using c++ anyway
|
0

Although you'd think the easiest way to do this would be:

void *myfunction() {
  int x = 5;
  return &x; // wrong
}

this is actually undefined behaviour, since x is allocated on the stack, and the stack frame is "rolled up" when the function returns. The silly but correct way is:

void *myfunction() {
  int *x = malloc(sizeof(int));
  *x = 5;
  return x;
}

Please never, ever write code like this, though.

7 Comments

You should find a way to put the whole first part in bright red with an x through it
I'm confused, why is it that I can cast a integer as a void pointer when sending it to another function, but I can't cast a integer as a void pointer when getting it as a return value?
@user2662982 The integer is stored on the stack. When you call a function, the original function's "stack frame" (storage for variables) is preserved, and the new function gets space "on top" of the old stack frame. However, once the function returns, there's no guarantee that the variables will be preserved, since the space "on top" could be used for other variables. You might want to read about the stack and the heap for more detail.
in first code you are returning address of local variable causes Undefined behaviour at runtime.
That isn't "silly" that is the intention of using a void* return. What might be silly is returning just a single integer instead of a structure. This kind of function is often used as part of a callback so that the user's callback can return a data pointer.
|
0

I compiled this source with gcc -pedantic:

 #include <stdio.h>

 void *myfunction() {
   size_t x = 5;  
   return (void*)x;
 }

 int main() {
   printf("%d\n", *(int*)myfunction());
   return 0;
 }

There is no warnings

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.