c++ - 我从 g++ 编译器收到此错误 - 从 ‘int*’ 到 ‘int’ 的无效转换 [-fpermissive]

标签 c++ pointers casting

我的完整程序如下:

#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/

相关文章:

c++ - 隐式转换运算符优先级

c - 修改C中的时间函数

c++ - 如何检查模板中发送的参数类型?

C++ 从文件中读取

c++ - 取消对象创建的简洁方法

c - 还有其他方法可以将指针地址交换为交换值吗?

c++ - 什么是悬空指针?

c++ - 将以毫秒为单位的时间戳转换为boost中的时区

c - 移动指针并重新分配,C

c++ - 我可以在这里使用 reinterpret_cast 吗?