0

I do my best to expalin what I'm trying to develop:

  1. I have developed a function2 into a library which returns the address of an array.

    uint16_t * readArray(void)
    {
      uint16_t array[5] = {1, 2, 3, 4, 5};
      return array;
    }
    
  2. Another function1 from the same library has to store this address into a pointer that is passed as argument. This function does not return nothing, but the address need to be save in the pointer.

    void readAddress(uint16_t *pointer_)
    {
      pointer_ = readArray();
    }
    
  3. From 'main' function, values from the array need to be printed.

    uint16_t *values;
    
    int main(void)
    {
      ...
      readAddres(values);
      print(values);      // This function prints the complete array
      ...
    }
    
    • Function declarations are omitted conciously (library.h).

What I have found is doing so, the value stored in 'pointer_' is not the array's address.

This is a pointer issue, and I would be pleased if someone could help me to understand how to develop a situation like this.

KR!

Yolco
  • 29
  • 3
  • 1
    Read about passing by value, passing arrays to a function, and modifying pointers. This'll solve your problems. – cadaniluk Jan 14 '16 at 12:50
  • 1
    Possible duplicate of [Returning a pointer to an automatic variable](http://stackoverflow.com/questions/1224042/returning-a-pointer-to-an-automatic-variable) – 2501 Jan 14 '16 at 12:53
  • This is a duplicate of a duplicate of a duplicate ..... – machine_1 Jan 14 '16 at 12:55
  • regarding this line: `uint16_t array[5] = {1, 2, 3, 4, 5};`. this array is on the stack of the enclosing function and disappears when the function exits. one easy way to correct that is: `static uint16_t array[5] = {1, 2, 3, 4, 5};` – user3629249 Jan 14 '16 at 13:04
  • regarding this line: `readAddres(values);`. In C, items are always passed by value, to change where a pointer points in a calling function, the address of that pointer must be passed. So the line should be: `readAddres( &values );` – user3629249 Jan 14 '16 at 13:09

3 Answers3

3

Since array[5] is a local variable,it gets wiped out when the function terminates.

As for your function void readAddress(uint16_t *pointer_),it cannot change the value of the supplied pointer since it is passed by Value.you need to change it to accept uint16_t** as an argument.Try this after fixing readArray():

void readAddress(uint16_t **pointer_)
{
    *pointer_ = readArray();
}
machine_1
  • 4,266
  • 2
  • 21
  • 42
2

What I have found is doing so, the value stored in 'pointer_' is not the array's address.

This is because uint16_t array[5] is local to the function readArray, and it gets destroyed when the function returns.


To make this work you can allocate memory in heap. Memory allocated in heap is retained in between function calls.

Use the malloc() system call to allocate memory in heap.

Haris
  • 12,120
  • 6
  • 43
  • 70
  • There is no need to involve `malloc` here. Check @machine_1's Answer. – Michi Jan 14 '16 at 15:59
  • @Michi, It cannot be done just by changing the `readAddress` function. He also has mentioned that. *... Try this after fixing `readArray()`*. – Haris Jan 14 '16 at 16:02
1
uint16_t * readArray(void)
{
  uint16_t array[5] = {1, 2, 3, 4, 5};
  return array;
}

The array is initialized only when this function is called and its heap is destroyed as soon as the function returns.

  pointer_ = readArray();

So, will always have point to null. I had the same issue once and a colleague of mine suggested an elegant solution, which, for your case would be:

void readArray(uint16_t *ptr)
{
  uint16_t array[5] = {1, 2, 3, 4, 5};
  for(i=0;i<4;i++)
      ptr[i]=array[i];
}

void readAddress(uint16_t *pointer_)
{
  readArray(pointer_);
}

In this case you, if you desire, you may even get rid of readAddress method and from you main call

readArray(values);
codeahead
  • 133
  • 1
  • 9