Change the value a pointer points to

Exactly as it says in the title :)

I have a pointer
 
    size_t* p_i = malloc(sizeof(size_t));


And a size_t variable whose content is changing constantly (as is on a for loop).
 
    size_t value;


I need to pass the value that the pointer points to to a function that takes void*
1
2
    *p_i = value;
    my_function = (stack, (void*)p_i);


Is this the right way to do it? If not, how should I do it?
Yes, that's the idea. It's not necessary to cast p_i to void * in the argument list.
A function that accepts void * argument will accept a pointer of any type.

BTW, a function call has no = sign in it.

 
   my_function (stack, p_i);


However, there's no need to even allocate a pointer for this. You can simply pass the address of value.

 
   my_function (stack, &value);  // Address of value is a pointer 


edit: Just saw your other thread and now understand why you're allocating an object of size_t, so disregard the last comment about not needing dynamic allocation.
Last edited on
Topic archived. No new replies allowed.