在传递数组时从'int'到'int *'[-fpermissive]的无效转换 [英] invalid conversion from 'int' to 'int*' [-fpermissive] on passing array

查看:381
本文介绍了在传递数组时从'int'到'int *'[-fpermissive]的无效转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C ++语言的新手,我不知道指针及其用法.在行中编译时遇到错误"[Error] invalid conversion from 'int' to 'int*' [-fpermissive]":

I am new to C++ language and I have no idea of pointers and their usage. I'm facing the error "[Error] invalid conversion from 'int' to 'int*' [-fpermissive]" while compiling in line:

cout << midd (ax [10], asize) << endl;

这是代码:

#include <iostream>

using namespace std;
double midd(int arr[10], int size);

int main() {
    int ax[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
    int asize = 10;
    cout << midd(ax[10], asize) << endl;
}

double midd(int arr[10], int size) {
    int acount = 0;
    int mid1;
    int mid2;
    int amid = size / 2;

    double mid = 0.0;

    while (acount < 10) {
        if (acount == amid) {
            mid1 = arr[acount];
        }
        else if (acount == (mid + 1)) {
            mid2 = arr[acount];
        }
        ++acount;
    }    
    mid = (mid1 + mid2) / 2.0;
    return mid;
}

推荐答案

此处midd(int arr[10],int size);期望int *,并且您试图传递int值(ax [10]也是错误:ax仅具有10个元素,而您尝试使用11th),编译器无法将int转换为int*,因此它显示"[错误]从'int'到'int *'[-fpermissive]的无效转换".

Here midd(int arr[10],int size); is expecting int* and you are trying to pass int value ( ax[10] which is also ERROR : ax has only 10 elements and you try to use 11th ), and compiler can not convert int to int* so it shows "[Error] invalid conversion from 'int' to 'int*' [-fpermissive]".

要使此程序正确,您必须进行此更改.

To make this program correct you have to make this change.

  • cout<< midd (ax[10], asize );替换为cout<< midd (ax, asize );

-现在传递了斧头(int*)的指针,因此midd()会接受它.

-now pointer of ax (int*) is passed so midd() will accept it.

这篇关于在传递数组时从'int'到'int *'[-fpermissive]的无效转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆