【问题标题】:Swapping an array of structs' element with another element将结构元素数组与另一个元素交换
【发布时间】:2012-08-23 19:50:06
【问题描述】:

我正在尝试交换一个结构数组,我认为按照类似的方式存储在 temp 中会像这样工作:

int temp ,a, b;
temp = a;
a = b;
b = temp;

我的结构定义数组是这样的:

struct storage data[10];

我尝试交换一个结构数组,我试过这个:

struct storage temp[1];
temp = data[1];
data[1] = data[2];
data[2] = temp;

不幸的是,它没有编译

我的错误如下:

错误 #2168:“=”的操作数具有不兼容的类型“struct storage[1]”和“struct storage”。

错误 #2088:需要左值。

错误 #2168:“=”的操作数具有不兼容的类型“struct storage”和“struct storage*”。

【问题讨论】:

    标签: c arrays struct swap


    【解决方案1】:

    在 C 中,数组不是可修改的左值。删除 [1] 即可:

    struct storage temp;
    

    【讨论】:

      【解决方案2】:

      您正在尝试持有一个结构存储,当您说时已取消引用该结构存储

      温度 = 数据[1];

      您需要声明您的临时变量以保存数组中取消引用的值

      结构存储温度;

      【讨论】:

        猜你喜欢
        • 2021-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-11-27
        • 1970-01-01
        • 2011-11-29
        • 1970-01-01
        相关资源
        最近更新 更多