我是C语言中的新手,但对C语言中的Pointers不太熟悉
我正在尝试实现一个对Struct数组进行排序的函数,如下所示:
typedef struct Album {
char titel[MAX_STRING_LENGTH];
char interpret[MAX_STRING_LENGTH];
unsigned short releaseYear;
enum conditionEnum condition; //1 = sehr gut, 2 = gut, 3 = mittel, 4 = schlecht, 5 = sehr schlecht
};
我写了这个排序函数:
void bubblesort(struct Album yourArray[], int arraysize)
{
struct Album tmp;
for (int i = arraysize; i > 1; i--) //loop that makes the bubblesort smaller --> defines endcriteria
{
for (int j = 0; j < i - 1; j++) //loop for the not sorted data
{
if (yourArray->releaseYear[j] > yourArray->releaseYear[j + 1]) //comparing first value with the second value
{
tmp = yourArray[j]; //the biggest value is stored in a tmp value
yourArray[j] = yourArray[j + 1]; //swapping process (7 > 5) --> the smaller value moves forward (j - Stelle) --> 7 goes to 5
yourArray[j + 1] = tmp; //swapping process (7 > 5) --> the biggest value moves backwards (j + 1 - Stelle) --> 5 goes to 7
}
}
}
}
但是我的IDE说
“表达式必须具有指向对象类型的指针”
有人可以帮助我,向我解释如何处理吗?在我的代码中,我将数组初始化为:struct Album Alben[5];
感谢亚历克斯
答案 0 :(得分:0)
您有一个结构体数组。您可以获取数组中的一项,然后该项是一个结构。
您应使用yourArray[j].releaseYear
,这意味着:
j
中第yourArray
项-这是一个结构。releaseYear
字段。您拥有的是yourArray->releaseYear[j]
,这意味着:
yourArray
指向的项目-这是一个结构。 (出于以后会发现的原因,它与yourArray[0]
相同。)releaseYear
字段。j
字段中的第releaseYear
条,但这是无效的,因为releaseYear
不是数组。