C-“从“ int *”到“ int”的无效转换

时间:2019-11-20 06:35:52

标签: c algorithm

我是编码新手。我试图制作一个具有多个功能的程序,这些功能具有用于线性搜索和气泡排序的不同功能。我将DevC ++ IDE与TDM-GCC 4.9.2 64位编译器一起使用。我不知道我在哪里弄错了。 在第17和19行,它显示错误“从“ int *”到“ int”的无效转换。

#include<stdio.h>
void LinearSearch(int,int);
void BubbleSort(int,int);
int main()
{
    int i,a[10],n;
    int size=10;
    for(i=0;i<10;i++)
    {
        printf("Enter a number\n");
        scanf("%d",&a);
    }
    printf("Press the button og=f your choice!\n");
    printf("1. Linear Seach    2. Bubble Sort\n");
    scanf("%d",&n);
    if(n==1)
    LinearSearch(a,size); //Invalid Conversion from "int*" to "int
    else if(n==2)
    BubbleSort(a,size);  //Invalid Conversion from "int*" to "int
    else
    printf("You have pressed the wrong button!\n");
}
void LinearSearch(int a[10],int sizA)
{
    int i,s,f=0;
    printf("Enter the search value\n");
    scanf("%d",&s);
    for(i=0;i<10;i++)
    {
        if(s==a[i])
        {
            f=1;
            printf("The Search Avlue is located at position no. %d in the array",i);
        }
    }
    if(f==0)
    printf("Search Value Not Found");
}
void BubbleSort(int a[10],int sizB)
{
    int i,j,k;
    for(i=0;i<10;i++)
    {
        for(j=0;j<10-i-1;j++)
        {
            if(a[j]>a[j+1])
            k=a[j];
            a[j]=a[j+1];
            a[j+1]=k;
        }
    }
    for(i=0;i<10;i++)
    {
        printf("%d  ",a[i]);
    }
}

2 个答案:

答案 0 :(得分:2)

scanf("%d",&a)应该是scanf("%d",&a[i])

并从

更正您的函数转发声明
void LinearSearch(int,int);
void BubbleSort(int,int);

void LinearSearch(int[],int);
void BubbleSort(int[],int);

答案 1 :(得分:1)

您的两个函数的声明与定义不匹配。需要明确的是,您已经声明:

void LinearSearch(int,int);  //<---the two arguments are int
void BubbleSort(int,int);    //<---the two arguments are int

但是,最后,您将函数定义为:

void LinearSearch(int a[10],int sizA); //<---the first argument is an int*
void BubbleSort(int a[10],int sizB);   //<---the first argument is an int*

如果要解决所报告的编译错误,请用这两行替换声明!

PS 。请注意,您可能还需要解决其他一些小问题(使用scanf("%d",&a);时)。正如其中一项注释中指出的那样,正确的指令应为scanf("%d",&a[i]);