数组中一元'*'(具有"int")最低元素的类型参数无效
invalid type argument of unary '*' (have 'int') lowest element in array
我想使用函数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 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
}
- 您忘记了
if
块周围的牙套。 缩进不能确定 C 中的块结构。 - 语义不一致:将索引返回到最低元素,但将
tempAdd
索引设置为j + i
这是指向最低元素的指针。 - 您不初始化
temp
,也不tempAdd
。行为未定义。
将 - 指针命名为
j
是令人困惑的,j
通常指定一个整数索引。 使用p
.
这是一个更简单的版本:
int lowest(int *p, int n) { //For finding the lowest element
int i, tempAdd = 0;
for (i = 1; i < n; i++) {
if (p[i] < p[tempAdd]) {
tempAdd = i;
}
}
//Return the index of the lowest element
return tempAdd;
}
总的来说,你应该修改代码,因为low
不是一个指针:
printf("nThe Lowest element in the list is %d with address %d.",
arr[low], &arr[low]);
-
对于打印地址,您可以使用如下
%p
。printf("nThe Lowest element in the list is %d with address %p.", low, low);
#include<stdio.h>
int *lowest(int *j, int n) { //For finding the lowest element
int i, temp;
int *tempAdd;
temp=*j;
tempAdd=j;
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 low = lowest(arr, n);
到int *low = lowest(arr, n);
lowest
函数应该是:
int *lowest(int *j, int n) { //For finding the lowest element
int i, temp = *j;
int *tempAdd = NULL;
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
}
在main
函数中:使用 int *low
而不是 int low
并使用 %p
显示变量地址。
这里是固定函数的第一次迭代。它仍然不是我所写的 100%,而是限制自己解决问题。
由于您要返回地址,因此我调整了返回类型以及变量tempAdd
的类型
int* lowest(int *j, int n) { //For finding the lowest element
int i, temp;
int *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
}
例如,对于参数n = 0
如果不对函数进行进一步更改,则函数的返回值将未定义。
由于变量temp
最初也没有初始化,因此返回的地址也可能未定义,以防数组的任何成员都不小于变量temp
的(随机(值。
在这里,一个稍微健壮的版本:
int* lowest(int *j, int n) { //For finding the lowest element
if( 0 == n ) return NULL; // empty arrays have no smallest element!
int i;
int temp = j[0]; // instead of using pointer arithmetic you can also use this syntax.
int *tempAdd = j; // initially the first element is allegedly the smallest...
for(i = 1; i < n; i++) // loop starts at index 1 now!
{
if(temp > *(j + i)) {
temp = *(j + i);
tempAdd = j + i;
}
}
return tempAdd; //Sends the address of the lowest element
}
您的函数main()
也有其问题。您无法创建动态大小的自动(位于堆栈位置(数组,这是您尝试的。相反,如果要向用户查询数组的大小,则必须改用基于堆的数组。或者,您可以查询一个大小,该大小小于或等于基于堆栈的数组的任意选择的固定大小。
int main() {
int n = 0;
printf("Enter the number of inputs (1..500): ");
scanf("%d", &n);
if( n < 1 || n > 500 ) {
puts("Invalid input.");
return -1;
}
int arr[500]; // 500 was chosen because most likely no one is crazy enough to manually type in more values by hand ;)
int 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 %p.", *low, low); //Error occurs
return 0;
}
还将指针的格式更改为"%p"。还将low
类型从int
更改为int *
。
最后并非最不重要的一点是,如果要允许 0 数组大小,则必须进一步更改main()
。为什么?因为在您的 printf 中您写...,*low,...
.由于 lowest(( 在 n = 0
的情况下会返回 NULL,因此您将取消引用 NULL 指针,这会导致令人讨厌的运行时错误。
从设计的角度来看,最终,在 lowest(( 中返回地址似乎会破坏抽象级别,这与传入数组长度的事实有关。基本上,您可以混合两种样式。
- STL 样式为:
int * lowest( int *begin, int * end )
- 复古风格将是:
int lowestIndex( int *arr, int n)
第二个版本,虽然会有你不能表达"无结果"结果的问题。例如,如果数组大小为 0 或其他无效参数被传递给函数。因此,人们通常会这样做:
-
bool lowestIndex( int * arr, int n, int *result )
。其中返回值指示成功,结果内容仅在返回值true
时才有效。
- 在 c++ 中的模板实例化中使用带有构造函数的类作为类型参数
- 如何解决一元"*"(有"字符")错误的无效类型参数?
- "std::shared_ptr":不是参数"_Ty"的有效模板类型参数
- 具有可变参数非类型参数的模板专用化
- C++ 未初始化的本地(非全局)int 数组中的元素类型到底是什么?
- 函数类型参数的模板参数推导
- PowerShell 使用结构类型参数调用 C++ DLL 的导出函数
- 对于非常量指针类型的参数,未调用具有常量指针模板类型参数的功能
- 为模板传递非类型参数 agument
- 为什么带有类型参数的运算符 () 可以应用于 result_of 上下文中的类型?
- 使用其他模板类型参数作为要在函数签名中使用的类型别名声明
- 如何避免具有相同类型参数的函数中的错误
- 如何将 n 个连续元素插入到元素类型不可复制的 std::vector 中?
- 将内置类型变量传递给只有一个类类型参数的"+"运算符函数时自动类型转换的构造函数
- c++非类型参数包扩展
- 如何实现对参数顺序不可知的std::same_as的广义形式(即对于两个以上的类型参数)
- 在编译时间检查模板参数类型是否设置或多键,并且容器的元素类型是算术的
- 从函数模板参数中自动推论对容器的元素类型
- 如何将字符串类型的矢量元素作为参数传递给pthread_create()
- 数组中一元'*'(具有"int")最低元素的类型参数无效