[英]C pointer argument assigned to a parameter with a different value
I was trying to write a seemingly trivial function in C to get the maximum value in an array of integers.我试图用 C 编写一个看似微不足道的函数来获取整数数组中的最大值。 I'm trying to do so using pointers to get a better understanding.我正在尝试使用指针来获得更好的理解。 (I know how to do this passing the size of the array, the question is not how to do this another way) (我知道如何传递数组的大小,问题不是如何以另一种方式执行此操作)
#include <stdio.h>
int gimme_largest(int *, int *);
int main(void)
{
int arr[] = {1, 22, 3, 44, 5};
int largest;
printf("In main, arr:\t\t\t %p \n", arr);
printf("In main, arr + sizeof(arr):\t %p\n", arr + 5);
putchar('\n');
largest = gimme_largest(arr, arr + sizeof(arr));
// printf("\n\nThe largest is %d", largest);
return 0;
}
int gimme_largest(int *a, int *lastPlusOne)
{
int largest = *a; // let's assume the first element is the largest one
printf("In gimme_largest, a:\t\t %p\n", a);
printf("In gimme_largest, a + 5:\t %p\n", a + 5);
printf("In gimme_largest, lastPlusOne:\t %p\n", lastPlusOne);
while (a < lastPlusOne) {
if (*a > largest)
largest = *a;
a++;
}
return largest;
}
The problem is that lastPlusOne
does not equal a + 5
in the invoked function, but a higher address;问题是lastPlusOne
在调用的函数中不等于a + 5
,而是一个更高的地址; can't see why that's happening.不明白为什么会这样。
This call这个电话
largest = gimme_largest(arr, arr + sizeof(arr));
is invalid.是无效的。 sizeof( arr )
in the case of your program is equal to 5 * sizeof( int )
while for the correct pointer arithmetic you need to use only the number of elements in the array. sizeof( arr )
在您的程序中等于5 * sizeof( int )
而对于正确的指针算法,您只需要使用数组中的元素数。
That is there shall be那就是应该有
largest = gimme_largest(arr, arr + sizeof(arr) / sizeof( *arr ) );
Thus the expression sizeof(arr) / sizeof( *arr )
is equal to the value 5 that you are using in this call of printf因此,表达式sizeof(arr) / sizeof( *arr )
等于您在此 printf 调用中使用的值 5
printf("In main, arr + sizeof(arr):\t %p\n", arr + 5);
Pay attention to that the user can call the function with an empty range when a
is equal to lastPlusOne
.请注意,当a
等于lastPlusOne
时,用户可以调用具有空范围的函数。 In this case the function will have undefined behavior because for an empty range there is no largest elemenet.在这种情况下,函数将具有未定义的行为,因为对于空范围,没有最大的元素。
The function should return a pointer to the largest element.该函数应该返回一个指向最大元素的指针。
Also as in C there is no function overloading then the function should be called for constant and non-constant arrays.同样在 C 中没有函数重载,那么应该为常量和非常量数组调用该函数。
Here is a demonstrative program that shows how the functipon can be defined.这是一个演示程序,显示了如何定义函数。
#include <stdio.h>
int * gimme_largest( const int *first, const int *last )
{
const int *largest = first;
if ( first != last )
{
while ( ++first != last )
{
if ( *largest < *first ) largest = first;
}
}
return ( int * )largest;
}
int main(void)
{
int arr[] = {1, 22, 3, 44, 5};
const size_t N = sizeof( arr ) / sizeof( *arr );
int *largest = gimme_largest( arr, arr + N );
printf( "The largest number is %d\n", *largest );
return 0;
}
The program output is程序输出是
The largest number is 44
To get the end address of the array, you can do this by arr + sizeof(arr) / sizeof(arr[0])
:要获取数组的结束地址,您可以通过arr + sizeof(arr) / sizeof(arr[0])
:
#include <stdio.h>
int gimme_largest(int *, int *);
int main(void)
{
int arr[] = {1, 22, 3, 44, 5};
int largest;
printf("In main, arr:\t\t\t %p \n", arr);
printf("In main, arr + sizeof(arr):\t %p\n", arr + 5);
putchar('\n');
largest = gimme_largest(arr, arr + sizeof(arr) / sizeof(arr[0]));
printf("\n\nThe largest is %d", largest);
return 0;
}
int gimme_largest(int *a, int *lastPlusOne)
{
int largest = *a; // let's assume the first element is the largest one
printf("In gimme_largest, a:\t\t %p\n", a);
printf("In gimme_largest, a + 5:\t %p\n", a + 5);
printf("In gimme_largest, lastPlusOne:\t %p\n", lastPlusOne);
while (a < lastPlusOne) {
if (*a > largest)
largest = *a;
a++;
}
return largest;
}
output:输出:
In main, arr: 000000000062FE00
In main, arr + sizeof(arr): 000000000062FE14
In gimme_largest, a: 000000000062FE00
In gimme_largest, a + 5: 000000000062FE14
In gimme_largest, lastPlusOne: 000000000062FE14
The largest is 44
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.