【问题标题】:Cannnot Convert int to int. Arrays C无法将 int 转换为 int。数组 C
【发布时间】:2014-05-18 04:05:45
【问题描述】:

您好,我很难让数组在函数中工作。我不断收到这个错误,称为 C2664 "void ranNumPerm_10(int)' : cannot convert argument 1 from 'int [10]' to 'int'" 我不明白如何解决它...

函数ranNumPerm_10(int):

void ranNumPerm_10(int bubble_1[])
{
    int oneRandno;
    int haveRand[ARRAY_SIZE_1] = { 0 };

    for (int i = 0; i < ARRAY_SIZE_1; i++)
    {
        do
        {
            oneRandno = rand() % ARRAY_SIZE_1;
        } while (haveRand[oneRandno] == 1);
        haveRand[oneRandno] = 1;
        bubble_1[i] = oneRandno;
    }
    return;
}

这个程序是一个未完成的程序,将使用冒泡、选择、插入排序算法。我似乎还无法填充数组。我正在尝试通过成为“随机数排列生成器”来创建一个函数,以便每个数字都是随机的,并且没有数字重复它自己。我可以使用一些帮助来使此代码正常工作并解决错误 C2664。

完整代码:

#define _CRT_SECURE_NO_WARNINGS
#define ARRAY_SIZE_1 10
#include <stdio.h>
#include <stdlib.h>


void ranNumPerm_10(int bubble_1);

int main(void)
{
    //Declarations
    int bubble_1[ARRAY_SIZE_1];

    //Bubble population @ 10 
    ranNumPerm_10(bubble_1);
    for (int i = 0; i < ARRAY_SIZE_1; i++)
    {
        printf("%d\n", bubble_1[i]);
    }

    printf("Array population test...\n");
    return 0;
} 
    void ranNumPerm_10(int bubble_1[])
{
    int oneRandno;
    int haveRand[ARRAY_SIZE_1] = { 0 };

    for (int i = 0; i < ARRAY_SIZE_1; i++)
    {
        do
        {
            oneRandno = rand() % ARRAY_SIZE_1;
        } while (haveRand[oneRandno] == 1);
        haveRand[oneRandno] = 1;
        bubble_1[i] = oneRandno;
    }
    return;
}

【问题讨论】:

    标签: c arrays function random permutation


    【解决方案1】:

    ranNumPerm_10 的声明和定义有冲突的签名。在完整代码的顶部,您将其声明为void (int),但随后定义为void (int [])

    【讨论】:

    • 天啊,我太傻了!我花了几个小时思考原型的问题与指针有关。我一直在尝试诸如 void (int*) 和 void (int [*]) 之类的东西。我不知道在函数中使用数组的类型设置为“type []”。我在想没关系的原型。感谢您的帮助。
    • 你应该也得到了void ranNumPerm_10(int bubble_1[])行的错误
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-15
    相关资源
    最近更新 更多