简体   繁体   English

调用函数后指针未指向正确的数组元素

[英]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. 我的主要功能中有两个指针变量:largest_ptr和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. 任务是调用find_max_min()函数,将两个数组(均为全局数组)中的最大和最小元素分配给各自的指针。

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. 我尝试调试find_min_max函数,结果是正确的,即正确的值已分配给最大和最小的指针。 However, when I call the function in main(), the respective addresses are not assigned to largest_ptr and smallest_ptr. 但是,当我在main()中调用该函数时,相应的地址未分配给largest_ptr和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. smallest_ptr是类型指针的变量uint_8,并且它包含一个值是一个地址。 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 . 表达式minimum_ptr求值到某个地址,该地址通过值传递给函数,即-分配给minimum

Although I can't see the inner workings of your function, I assume You do not read from smallest , but write to it. 尽管我看不到函数的内部工作原理,但我假设您不是从small读取,而是向其写入。 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. 其中&是地址运算符,因此我们提供y的地址以使func()函数能够写入。

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); 由于在您的情况下,需要操纵的变量是指向uint8_t的指针,因此您需要传递指针的地址,因此局部变量必须是指向指针的指针-func(int ** x);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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