【问题标题】:Two errors I'm getting in this C program. Deals with structures, arrays, and bubble sorting我在这个 C 程序中遇到了两个错误。处理结构、数组和冒泡排序
【发布时间】:2015-03-21 23:59:55
【问题描述】:

前两个错误:

cs1713p3.c:129: error: incompatible types in assignment
cs1713p3.c:131: error: conversion to non-scalar type requested

第 129 行和第 131 行是我编写的冒泡排序函数的一部分,该函数用于对名为“Stock”的结构的数组进行排序。这是 Stock 结构的代码:

typedef struct
{
    char        szStockNumber[7];           // Stock Number for a stock item
    long        lStockQty;                  // quantity in stock
    double      dUnitPrice;                 // price per unit of stock
    char        szStockName[31];            // name of the stock item
} Stock;

这是我为排序编写的代码:

void sortInventory(Stock stockM[], int iStockCnt)
 {
     Stock *temp;
     int i;
     int j;
     int bChange = 1;
     for(i = 0; i < (iStockCnt - 1) && bChange == 1; i++)
     {
         bChange = 0;
         for(j = 0; j < (iStockCnt - i - 1); j++)
         {
             if(strcmp(stockM[i+1].szStockNumber, stockM[i].szStockNumber) < 0)
             {
                 temp = stockM[i]; //line 129
                 stockM[i] = stockM[i+1];
                 stockM[i+1] = (Stock)temp; //line 131
                 bChange = 1;
             }
         }
    }
}

是的,我尝试在每行中的等号之后使用(Stock) 对每个作业进行类型转换,但这不起作用。

感谢任何提供帮助的人!不知道怎么回事,网上好像也找不到相关资料,我觉得这个案例太具体了,哈哈。

【问题讨论】:

    标签: c arrays sorting structure bubble-sort


    【解决方案1】:

    stockMStock 类型的数组。另一方面,tempStock* 类型的数组。因此,您基本上是在尝试将 Stock 类型的变量的值分配给 Stock* 类型的变量,这是不允许的。

    temp的类型改为Stock

    【讨论】:

    • 哦,我现在明白了!谢谢你。哇,这太简单了,我觉得自己很笨,哈哈哈。
    猜你喜欢
    • 2019-11-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多