#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <math.h>
#include <string.h>
struct protein
{
char proteinBrand[100];
int currentStock;
int unitsSold;
double averageReview;
int totalReviews;
};
void selection_sort(struct protein protein_powders[], int n)
{
int i, largest = 0, temp;
if (n == 1)
return;
for (i = 1; i < n; i++)
if (protein_powders[i].averageReview < protein_powders[largest].averageReview)
largest = i;
if (largest < n - 1) {
temp = protein_powders[n-1];
protein_powders[n-1] = protein_powders[largest];
protein_powders[largest] = temp;
}
}
有问题的行是
temp = protein_powders[n-1];
和
protein_powders[largest] = temp;
我遇到了这些编译器错误,我真的不知道要改变什么。我在C方面经验不足,所以任何帮助都会受到赞赏!:
incompatible types when assigning to type ‘int’ from type ‘struct protein’
incompatible types when assigning to type ‘struct protein’ from type ‘int’
答案 0 :(得分:2)
现在您正在尝试将类型protein
的元素分配给整数变量。
就像我在评论中指出的那样,temp
应该被声明为一个结构来使用你现在的方式:
// Declare temp as a struct.
struct protein temp;
// And then use it.
temp = protein_powers[n-1];
如果您打算只分配一个值,可以这样做:
int temp = protein_powers[n-1].currentStock;