0

I've been struggling with a rather weird situation.

I had to implement some ADTs in C and use them. while using the ADT i have a typedef'ed void *.

As i was trying to work with int values, trying to return an int where that void * is expected, i got the warning "return makes pointer from integer without a cast". so i figured, ill just cast it.

but then i met this thread which guided me not to do such a thing, casting an int to void *. i had to try, though. so i got this warning - "cast to pointer from integer of different size".

anyway, i realised - maybe i could just directly return int, since this is just in the usage file of the ADT. so i changed the return type - and i got the same warnings, in other places, from pretty much the same reasons, from what i can tell.

Please, help me to get rid of those warnings. what should i do in this situation? i went through some threads regarding this issue (like the one i linked), yet there is no real solution to my problem in any of them.

My problematic function: (MapKeyElement is typedef'ed void *)

static MapKeyElement copyKeyID(MapKeyElement n) {
    if (!n) {
        return 0;
    }
    int *copy = malloc(sizeof(int));
    if (!copy) {
        return NULL;
    }
    *copy = *(int *)&n;
    return (void *)*copy;
}

Thank you for your time!

Community
  • 1
  • 1
Zephyer
  • 333
  • 6
  • 16

1 Answers1

0

Return a pointer to int:

void *return_int_data(int v) { 
   int *p = malloc(sizeof(int)); 
   p[0] = v; 
   return p; 
}

You can do this for all types.

perreal
  • 94,503
  • 21
  • 155
  • 181
  • Thanks for your reply. i added my problematic function to the main post, it contains this similar idea. – Zephyer Nov 29 '13 at 13:01