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!

5
  • 1
    Read about passing by value, passing arrays to a function, and modifying pointers. This'll solve your problems. Commented Jan 14, 2016 at 12:50
  • 1
    Possible duplicate of Returning a pointer to an automatic variable Commented Jan 14, 2016 at 12:53
  • This is a duplicate of a duplicate of a duplicate ..... Commented Jan 14, 2016 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}; Commented Jan 14, 2016 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 ); Commented Jan 14, 2016 at 13:09

3 Answers 3

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();
}
Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

There is no need to involve malloc here. Check @machine_1's Answer.
@Michi, It cannot be done just by changing the readAddress function. He also has mentioned that. ... Try this after fixing readArray().
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);

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.