【发布时间】:2019-03-14 20:34:43
【问题描述】:
我是 C 的新手,但对 C 中的指针不太熟悉 ...
我正在尝试实现一个对结构数组进行排序的函数,如下所示:
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];
谢谢亚历克斯
【问题讨论】:
-
yourArray->releaseYear[j]应该是yourArray[j].releaseYear -
上帝,谢谢...你能解释一下我做错了什么吗?
-
你需要对数组进行索引,而每个被索引的成员都是
struct Album类型,它不是一个指针,所以要访问它的成员你使用.操作符。