How to pass out an array from a function

Go To Last Post
3 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi all,

 

I've a newbie question about functions and arrays. I've a function where there is a struct composed in this way:

 

    typedef struct {
        uint8_t details;
        uint8_t info[20];
    } values_struct;
    
    values_struct pro_struct[10];

 

 

I need, when this function is called, to pass out values from the function. I had advice to use this way:

 

    void return_values_function(uint8_t* detail_pointer, uint8_t** array_pointer) {
      *detail_pointer = pro_struct[0].details;
      *array_pointer = pro_struct[0].info;
    
    }

 

But I never used something like double **.

 

I've written this:

 

uint8_t get_detail;

uint8_t **get_array = NULL;

void return_values_function(&get_detail, get_array);

 

But I don't know how to proceed. I'm used to pass an array address to a function where values are elaborated and then I can continue to use my array as array[i] = x; ecc..

But how I can request and pass values from a function? In this way seems I can't use get_array as get_array[i] as normal. Should I pass an array and copy values with a for? or there is a way with pointers?

 

Thank you!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Remember that, in 'C', an array name is just a pointer to the first element of the array.

 

So, if you supply the array name as a parameter to your function, it can write to the array.

 

void my_func( char *pBuf )
{
    pBuf[0] = 1; // write to the char pointed-to by pBuf
    pBuf[1] = 1; // write to the char after the one pointed-to by pBuf
    etc;
    etc;
    :
}

Is equivalent to:

void my_func( char *pBuf )
{
    *pBuf   = 1; // write to the char pointed-to by pBuf
    *pBuf+1 = 1; // write to the char after the one pointed-to by pBuf
    etc;
    etc;
    :
}

Also equivalent to:

void my_func( char pBuf[] )
{
    pBuf[0] = 1; // write to the char pointed-to by pBuf
    pBuf[1] = 1; // write to the char after the one pointed-to by pBuf
    etc;
    etc;
    :
}

 

Yaroooo wrote:
I never used something like double **

 

It just means a pointer to a pointer - literally * twice!

 

Note that this is all standard 'C' stuff; so you might find it easier to experiment using a PC compiler - where the debug environment is usually a lot easier ...

 

http://c-faq.com/aryptr/index.html

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

What would "uint8_t **" have to do with this anyway?

 

pro_struct is an array of elements of type values_struct. I would do something like either:

void do_something(uint8_t * output_addr, values_struct * p_s_array) {
    *output = p_s_array[3].info[7];
}

int main(void) {
    uint8_tr target;
    ...
    do_soumething(&target, pro_struct);
    ...
}

or perhaps (dealing with just 1 array element at a time):

void do_something(uint8_t * output_addr, values_struct p_s_array_elem) {
    *output = p_s_array_elem.info[7];
}

int main(void) {
    uint8_tr target;
    ...
    do_soumething(&target, &pro_struct[3]);
    ...
}

But this question (and my answer) don't seem to have anything to do with the thread title where you are asking how to return an array (your functions and mine are both "void" return). To return an array you would actually return a POINTER to the array data or, more likely, as you have done in your example, you pass a pointer in, not return one.