1

A function returns a void double pointer containing a pointer to a float array, how can i access the output data?

    void **pointer;

    // function(void **ptr)
    function(pointer);

This pointer points has to point to a float type pointer.

   float *coords; 
   coords = (float*)malloc(3*500*sizeof(float)); //I know the amount of memory to be allocated

How can I read the data from this void double pointer? I'm pretty confused.

1
  • In C, do not cast the result of a call to malloc(), realloc(), or calloc() - it is unnecessary and potentially masks the serious error of a missing prototype. Commented Jul 15, 2019 at 16:31

3 Answers 3

2

Your function prototype is not with respect to what you want to achieve. If you want your function to allocate memory and send its reference back to the main then your function will look like this (considering you want to pass double pointer) :

void function(void ***ptr)
{
    float *coords; 
    coords = (float*)malloc(3*500*sizeof(float)); 
    *ptr = (void **) &coords;
    //do something
    return;
}

main()
{
    void **pointer;
    function(&pointer);
    /* Accessing first member of array allocated in function */
    printf("%f", (*((float **)pointer))[0]);

}

If this is the objective, there is simpler way :

void function(void **p)
{
    float *coords;
    coords = (float*)malloc(3*500*sizeof(float));
    *p = coords;
     return;
}

main()
{
    void *pointer;
    function(&pointer);
    printf("%f", ((float*)pointer)[0]);
}

Hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

0

Please check your code. What you are posting is nonsense - you have an uninitialised variable and pass it to a function. That's undefined behaviour and can crash or worse before the function even starts executing.

I think you are confused by what you call a "double pointer". There is no such thing as a "double pointer". A pointer points to something, it doesn't double-point. A pointer might point to an int (an int*), or to a struct T (struct T*) or to a float* (a float**). In the float** there are two *'s, but that doesn't make it a "double pointer". It is an ordinary pointer that happens to be pointing to something that is itself a pointer.

Pointers as function parameters are most often used so that the function can return more than one value. Say a function get_width_and_height returning to ints:

void get_width_and_height (int* width, int* height) {
    *width = 10;
    *height = 20; 
}

int x, y;
get_width_and_height (&x, &y); 

Now with that example in mind, how would you write a function that returns two int and one float* ?

1 Comment

Ok, sorry for the confusion, i have a function(void *point) what do i have to do in order to gain access to the output data. I know that if it was a function(float *pointer), i would declare a float pointer: float *pointer //and then allocate memory by pointer = (float)malloc(3 * 18139 * sizeof(float)); //and then to print out data i would do: printf("%f " pointer[0]); What is the case with a void **pointer? And how to initialize it? as you mentioned.
-1

I am going to have to make a few assumptions here as the question is unclear

I am going to assume that you call the function thus:

 void **pointer;
 function(pointer);

Then want to access the output, so do

 flow *coord = (float *) *pointer;

Then you are home free

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.