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

C++ 一元'的类型参数无效*';(具有数组中的最低元素(';int';),c++,c,unary-operator,invalid-argument,C++,C,Unary Operator,Invalid Argument,我想使用函数lovel()查找数组中的最低元素。但是这个程序不起作用。它显示了错误 一元“*”的类型参数无效(具有“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))

我想使用函数
lovel()
查找数组中的最低元素。但是这个程序不起作用。它显示了错误

一元“*”的类型参数无效(具有“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;
}
#包括
int lowest(int*j,int n){//用于查找最低元素
int i、temp、tempAdd;
对于(i=0;i*(j+i))
温度=*(j+i);
tempAdd=j+i;
}
return tempAdd;//发送最低元素的地址
}
int main(){
int n;
printf(“输入输入的数量:”);
scanf(“%d”和“&n”);
int-arr[n],i;
对于(i=0;i
  • 对于打印地址,您可以使用
    %p
    ,如下所示

    printf("\nThe Lowest element in the list is %d with address %p.", low, low);
    

    • 这里是固定函数的第一次迭代。它仍然不是我写的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
      }
      
      函数
      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;
      }
      
      intmain(){
      int n=0;
      printf(“输入输入数量(1..500):”;
      scanf(“%d”和“&n”);
      如果(n<1 | | n>500){
      puts(“无效输入”);
      返回-1;
      }
      int arr[500];//500被选中是因为很可能没有人疯狂到可以手动输入更多值;)
      int i;
      对于(i=0;i
      还将指针的格式更改为“%p”。 还将
      low
      的类型从
      int
      更改为
      int*

      最后,如果要允许0数组大小,则必须进一步更改
      main()
      。为什么?因为在你的printf中你写的是
      ,*low,
      。在
      n=0
      的情况下,lowest()将返回NULL,因此您将取消对NULL指针的引用,这将导致严重的运行时错误

      从设计角度来看,最终,在lower()中返回地址似乎打破了抽象级别,这与传入数组长度有关。你基本上混合了两种风格

    • STL-样式应该是:
      int*最低(int*开始,int*结束)
    • 复古风格是:
      int-lowestIndex(int*arr,int-n)
    • 但是,第二个版本会有一个问题,即您无法表达“无结果”的结果。例如,如果数组大小为0或其他无效参数正在传递给函数。因此,人们通常会这样做:

    • bool-lowestinex(int*arr,int-n,int*result)
    • 。。。其中,返回值表示成功,且结果内容仅在返回值为
      true

      #include时有效
      
      #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*lowest(int*j,int n){//用于查找最低元素 int i,温度; int*tempAdd; 温度=*j; tempAdd=j; 对于(i=0;i*(j+i)){ 温度=*(j+i); tempAdd=j+i; } } return tempAdd;//发送最低元素的地址 }
      除此之外,请更正以下行
      int low=最低(arr,n)
      int*low=最低(arr,n)

      最低的
      功能应该是:

      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
      }
      
      int*lowest(int*j,int n){//用于查找最低元素
      int i,temp=*j;
      int*tempAdd=NULL;
      对于(i=0;i*(j+i))
      温度=*(j+i);
      tempAdd=j+i;
      }
      return tempAdd;//发送最低元素的地址
      }
      

      main
      函数中:使用
      int*low
      而不是
      int low
      ,并使用
      %p
      显示变量地址。

      您的函数
      low
      有问题:

      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
      }
      
      总之,您应该修改代码,因为
      low
      不是指针:

      printf("\nThe Lowest element in the list is %d with address %d.",
             arr[low], &arr[low]);
      

      如果要返回最小元素的地址,则“最低”函数的返回类型应为
      int*
      。但是它有
      int
      .1)
      tempAdd=j+i您正在尝试为整数分配地址。2)你没有给临时变量分配任何值。所以我们不能预测在第一次如果<代码>如果(TEMP> *(J+I))< /C> >什么值将不清楚这是否是C或C++问题。不要用两个标记来标记你的问题,因为它们是不同的语言。@Archimaredes在这些情况下,我切换到“c-ish c++”模式;)问题:为什么有两张反对票。同意这个问题对SO的未来用户没有太大价值。但这个问题在imho的其他方面并不缺乏。那么为什么要进行向下投票呢?不初始化
      temp
      会产生未定义的行为。变量未默认初始化为0或其他值。哎呀,我的错误!我在g++下运行代码,它没有显示m
      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;
      }
      
      printf("\nThe Lowest element in the list is %d with address %d.",
             arr[low], &arr[low]);