我的完整程序如下:
#include<iostream>
using namespace std;
int max(int *,int);
int main()
{
int n, a[10], b;
cout << "Please enter the no. of integers you wish to enter ";
cin >> n;
for(int i = 0; i < n; i++)
{
cout << endl << "please enter the " << i+1 << " no. ";
cin>>a[i];
}
b = max(&a[0], n);
cout << endl << "The greates no. is " << a[b] << " and its index position is " << b;
return 0;
}
int max(int * ptr,int n)
{
int b[10], i, max, k;
&b[0] = ptr;
max = b[0];
for(i = 0; i < n; i++)
{
if (max < b[i]);
max = b[i];
k = i;
}
return k;
}
我想将指针传递给函数并找到最大的数字。 我不确定传递数组是否算作传递指针。
最佳答案
你不需要为b[10]分配内存,这里只需要一个指针,而不是
int b[10];
只需声明一个指针并将其地址设置为函数传递的数组的起始元素。
即
int* b= ptr;
关于c++ - 我从 g++ 编译器收到此错误 - 从 ‘int*’ 到 ‘int’ 的无效转换 [-fpermissive],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22440373/