c - 冒泡排序无法编译c

标签 c

我使用的是 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/

相关文章:

c++ - 删除二叉搜索树中的节点时出错

c - 这里的算法发生了什么,代码与 C 中的递归问题有关?

c - 在压缩文件中查找

c - 如何将 main 转换为函数

c - 别名分析和 _restrict 关键字 - C

python - C 连续矩阵上的 Fortran gemm 函数

c - 如何使守护进程的子进程在 Linux 中交互?

c++ - 是否为函数调用中作为实际参数给出的字符数组分配了内存?

c - 尝试反转数组中的多个整数 - 想知道为什么我的代码不起作用

c - 是否可以将一种类型的位域转换为具有相同总位数的另一种类型的位域?