【发布时间】:2014-06-03 09:17:13
【问题描述】:
尝试对结构数组进行排序。 struct是下面定义的TextArt
typedef struct //struct that holds ASCII art
{
char artistName[80]; //name of artist
char asciiArt[20][80]; //actual ascii art line by line
int rating; //rating of art
}TextArt;
我认为结构与此无关。我得到编译器错误
错误:尝试将一个结构分配给另一个结构时需要左值作为分配的左操作数(见下文)
temp = asciiArt+pos_min;
asciiArt+pos_min = asciiArt+i; //error here
asciiArt+i = *temp; //error also here
调用函数
selectionSort(artPtr, artArrSize);
和全选排序功能。关于使用 = 在 C 中分配结构有什么我不理解的地方吗?我认为要么是这个,要么我传递的 TextArt 数组在某种程度上是错误的。请赐教,谢谢。
void selectionSort(TextArt *asciiArt, int size)
{
//pos_min is short for position of min
int pos_min;
TextArt *temp;
int i=0;
int j=0;
for (i=0; i < size-1; i++)
{
pos_min = i;//set pos_min to the current index of array
for (j = i + 1; j < size; j++)
{
if ((strncmp((asciiArt+i)->artistName, (asciiArt+j)->artistName)) < 0)
{
pos_min = j; //pos_min will keep track of the index that min is in, this is needed when a swap happens
}
}
//if pos_min no longer equals i than a smaller value must have been found, so a swap must occur
if (pos_min != i)
{
printf("copying...\n");
temp = asciiArt+pos_min;
asciiArt+pos_min = asciiArt+i;
asciiArt+i = *temp;
}
}
【问题讨论】:
标签: c arrays sorting struct variable-assignment