How to dereference a void variable without cast


Please briefly explain why you feel this question should be reported .

Report Cancel
typedef void *STACK_ENTRY;

STACK_ENTRY *create_stack_entry(void *value) {
    STACK_ENTRY *entry = malloc(sizeof(*value));
    *entry = *value;
    return entry;

I tried to make a universal function to create a STACK_ENTRY that should be able to have any type. So I use a void pointer as a parameter. I allocate memory with the size of the type of the value and then I would like to give the value to the pointee of the STACK_ENTRY.

But I get the following error:

prog.c:34:9: error: void value not ignored as it ought to be
  *entry = *value;

How can I fix that?

solved 0
1 Answer 8 views 0

Answer ( 1 )

    December 4, 2016 at 5:07 pm

    Please briefly explain why you feel this answer should be reported .

    Report Cancel

    Code needs to cope with size. Pass in size when calling create_stack_entry(). No cast needed.

     // STACK_ENTRY *create_stack_entry(void *value)
     STACK_ENTRY *create_stack_entry(void *value, size_t size) {
       STACK_ENTRY *entry = malloc(size);
       if (entry) memcpy(entry, value, size);
       return entry;
    Best answer

Leave an answer


What is the capital of Egypt ? ( Cairo )

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>