简体   繁体   中英

Pointer not pointing to the correct array element after function call

I have two pointer variables in my main function: largest_ptr and smallest_ptr. The task is to call the find_max_min() function to assign the largest and smallest elements within two arrays (both global) to the respective pointers.

void find_min_max(uint8_t* a, uint8_t* b); //function declaration

uint8_t array1[] = { <some values> };  
uint8_t array2[] = { <some values> };  

int main(void)
{
  uint8_t* largest_ptr;
  uint8_t* smallest_ptr;
  find_min_max(largest_ptr, smallest_ptr); //this does not assign any addresses

}

void find_min_max(uint8_t* largest, uint8_t* smallest){
   //correct code to find the max/min in array1 and array2, and assign the addresses of the elements to largest and smallest
}

I tried debugging my find_min_max function, and the result was correct ie the correct values were assigned to the largest and smallest pointers. However, when I call the function in main(), the respective addresses are not assigned to largest_ptr and smallest_ptr. Is there anything I am doing wrong?

PS
My apologies for not posting the code. This is an assignment question, and this may get detected in the plagiarism test. I am confident this is enough to explain my situation

Pass your pointer parameters through their adress in order to achieve your goal.

void find_min_max(uint8_t** a, uint8_t** b);

find_min_max(&largest_ptr, &smallest_ptr);

instead of

void find_min_max(uint8_t* a, uint8_t* b);

smallest_ptr is a variable of type pointer to uint_8, and a value that it holds is an address. Function definition you've created declares smallest as another such pointer. When You perform a function call, You initialize smallest with result of evaluation of expression. Often such expression is literal or single variable. Latter is the case here. Expression smallest_ptr evaluates to certain address, which is passed by value to function, that is - is assigned to smallest .

Although I can't see the inner workings of your function, I assume You do not read from smallest , but write to it. However smallest is a local variable with scope and lifetime tied to the function. You write to this variable and then the result is lost.

What You're trying to achieve is manipulate some data outside of your function scope. One of the possibilities is to provide address of that data to the function via argument, declared like this:

func( TYPE* x)
{
    //...
    *x = VALUE; // we don't want to set local variable, 
    // but the variable being pointed to by it, so we dereference the local pointer
}

Invoked like this:

TYPE y;
func(&y);

Where & is address operator, so we provide address of y for the func() function to be able to write into.

Since in your case variable that needs to be manipulated is pointer to uint8_t, You need to pass address of a pointer, so a local variable needs to be a pointer to a pointer - func(int** x);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM