【发布时间】:2017-01-12 17:46:24
【问题描述】:
我的主函数中有两个指针变量:maximum_ptr 和 minimum_ptr。 任务是调用 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
}
我尝试调试我的 find_min_max 函数,结果是正确的,即正确的值被分配给最大和最小的指针。但是,当我在 main() 中调用该函数时,相应的地址并未分配给最大_ptr 和最小_ptr。有什么我做错了吗?
附言
我很抱歉没有发布代码。这是一个作业问题,这可能会在抄袭测试中被发现。我有信心这足以解释我的情况
【问题讨论】:
-
你是否直接在函数中更改
largest和smallest(例如:largest = array1[0];)?如果是,那么您可能应该将该函数声明为void find_min_max(uint8_t** a, uint8_t** b); -
使用双指针。例如
void find_min_max(uint8_t** largest, uint8_t** smallest){并致电find_min_max(&largest_ptr, &smallest_ptr); -
阅读“按价值传递”是什么意思!注意:C 是严格按值传递。绝对没有引用调用。
标签: c pointers memory-address