【发布时间】:2019-11-20 06:35:52
【问题描述】:
我是编码新手。我试图制作一个具有多种功能的程序,该程序具有不同的线性搜索和冒泡排序功能。我正在使用带有 TDM-GCC 4.9.2 64 位编译器的 DevC++ IDE。我不知道我在哪里犯了错误。 在第 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]);
}
}
【问题讨论】:
-
scanf("%d",&a);-->scanf("%d",&a[i]); -
&a[i]→a + i;) -
@greybeard,是的,这两个表达式是等价的。但是,我更喜欢
&a[i],因为它更好地描述了我们想要做的事情:增加数组的索引并指向对应的变量。这与将 X Bytes 添加到 X 是sizeof(data_type)的数组的指针相同。我只是觉得不太直观。有点难以记住:)