我使用的是 source.c ,我使用与 c++ 相同的控制台,这之前没有给我带来问题。它给我的问题是指向 bubbleSort 函数,但我正在按照视频一步步让我的函数正常工作,我怀疑我有一个指针错误或一个变量。 我对 C 编程相当陌生,经常会犯我找不到的错误。任何帮助将不胜感激。
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct SalesRep //creating sturcture
{
char name[20];
int Sales;
};
void bubbleSort(SalesRep *a,int n);
int main()
{
int *a; //dynamic array pointer
int i,n;
struct SalesRep *s;
printf("How many sales to be entered for dynamic array?"); //getting dynamic array info
scanf_s("%d",&n);
a=(int*)calloc(n,sizeof(int));
s=(SalesRep*)calloc(n,sizeof(SalesRep));
printf("Enter Sales rep name and sales:");
for(i=0;i<n;i++)
{
printf("Enter name:\n");
scanf("%s",s[i].name);
printf("enter sales:\n");
scanf("d%",&s[i].Sales);
}
printf("This is the information entered:\n"); //displaying info
{
printf("%s: ",s[i].name);
printf("Sales %d:\n",s[i].Sales);
}
bubbleSort(s,n); //function call of bubblesort
printf("This is the information entered:\n"); //see new information after bubblesort
{
printf("%s: ",s[i].name);
printf("Sales %d:\n",s[i].Sales);
}
free( s );
return 0;
}
void bubbleSort(SalesRep *a,int n)
{
int i,j,temp;
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1;j++)
{
if((*(a+j)).Sales>(*(a+j+1)).Sales)
{
temp=(*(a+j)).Sales;
(*(a+j)).Sales=(*(a+j+1)).Sales;
(*(a+j+1)).Sales=temp;
}
}
}
}
错误:
(11)error C2143: syntax error : missing ')' before '*'
(11) error C2143: syntax error : missing '{' before '*'
(11)error C2059: syntax error : 'type'
(11)error C2059: syntax error : ')'
(28)error C2065: 'SalesRep' : undeclared identifier
(28)error C2059: syntax error : ')'
(46)warning C4013: 'bubbleSort' undefined; assuming extern returning int
(61)error C2143: syntax error : missing ')' before '*'
(61)error C2143: syntax error : missing '{' before '*'
(61)error C2059: syntax error : 'type'
(61)error C2059: syntax error : ')'
最佳答案
使用 C 编译器编译此文件时,您可能缺少的唯一内容如下:
typedef struct SalesRep SalesRep;
与 C++ 不同,在 C 中,通过 struct
引入的标记(如 struct SalesRep
中的 SalesRep
)仅在引用时才被识别为类型struct SalesRep
(而不是单独通过 SalesRep
)。 typedef
允许您直接使用 SalesRep
作为类型(无需 struct
前缀。
一旦解决此问题,所有其他问题都会消失。 bubbleSort
原型(prototype)中存在错误,因此调用 bubbleSort
时缺少声明,...
关于c - 冒泡排序无法编译c,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47582393/