[英]Invalid conversion from int to int* error for a function with void return type
So, I need to get the maximum value from an array.所以,我需要从数组中获取最大值。 So the first input line contains the number of array elements, then the values for the array.
所以第一个输入行包含数组元素的数量,然后是数组的值。 But I am really confused as to why this error keeps popping up when I pass an integer type array to the function.
但是我真的很困惑为什么当我将整数类型数组传递给函数时会不断弹出这个错误。 I am finding it a little difficult to deal with pointers and functions at the moment.
我发现目前处理指针和函数有点困难。
#include<iostream>
using namespace std;
void maxim(long long int nums[],long long int n){
int max_val=0;
for(int i=0;i<n;i++){
if(nums[i]>max_val){
max_val=nums[i];
}
}
cout<<max_val<<endl;
}
int main() {
long long int n;
cin>>n;
long long int arr[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
maxim(arr[n],n);
return 0;
}
#include<iostream>
using namespace std;
void maxim(long long int *nums,long long int n){
long long int max_val=nums[0];
for(int i=1;i<n;i++){
if(nums[i]>max_val){
max_val=nums[i];
}
}
cout<<max_val<<endl;
}
int main() {
long long int n;
long long int *arr;
cin>>n;
arr = new long long int[n];
for(int i=0;i<n;i++){
cin>>arr[i];
}
maxim(arr,n);
delete []arr;
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.