数组中一元'*'(具有"int")最低元素的类型参数无效

invalid type argument of unary '*' (have 'int') lowest element in array

本文关键字:元素 类型参数 int 无效 一元 数组 具有      更新时间:2023-10-16

我想使用函数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(( 中返回地址似乎会破坏抽象级别,这与传入数组长度的事实有关。基本上,您可以混合两种样式。

  1. STL 样式为:int * lowest( int *begin, int * end )
  2. 复古风格将是:int lowestIndex( int *arr, int n)

第二个版本,虽然会有你不能表达"无结果"结果的问题。例如,如果数组大小为 0 或其他无效参数被传递给函数。因此,人们通常会这样做:

  1. bool lowestIndex( int * arr, int n, int *result )

。其中返回值指示成功,结果内容仅在返回值true时才有效。