【发布时间】:2016-01-30 08:01:59
【问题描述】:
我想使用函数lowest() 查找数组中的最低元素。但是这个程序不起作用。它显示错误
一元'*'的无效类型参数(有'int')
代码如下:
#include <stdio.h>
int lowest(int *j, int n) { //For finding the lowest element
int i, temp, tempAdd;
for (i = 0; i < n; i++) {
if (temp > *(j + i))
temp = *(j + i);
tempAdd = j + i;
}
return tempAdd; //Sends the address of the lowest element
}
int main() {
int n;
printf("Enter the number of inputs: ");
scanf("%d", &n);
int arr[n], i;
for (i = 0; i < n; i++) {
printf("\nEnter element no. %d: ", i + 1);
scanf("%d", &arr[i]);
}
for (i = 0; i < n; i++) {
printf("Element no. %d is %d with the address %d.\n", i + 1, *(arr + i), arr + i);
}
int low = lowest(arr, n); //Saves the address of the lowest element.
printf("\nThe Lowest element in the list is %d with address %d.", *low, low); //Error occurs
return 0;
}
【问题讨论】:
-
如果你想返回最小元素的地址,你的“lowest”函数应该有返回类型
int *。但它有int。 -
1)
tempAdd = j + i;您正在尝试为整数分配地址。 2)您没有为 temp 分配任何值。因此,如果if(temp > *(j + i)),我们无法预测第一次会有什么值 -
尚不清楚这是 C 还是 C++ 问题。不要用两个标签来标记您的问题,因为它们是不同的语言。
-
@Archimaredes 在这些情况下,我切换到“c-ish c++”模式;)
-
问题:为什么有 2 票反对。同意这个问题对 SO 的未来用户没有太大的价值。但这个问题在任何其他方面都不缺乏恕我直言。那么为什么要投反对票呢?
标签: c++ c unary-operator invalid-argument