【发布时间】:2017-09-11 03:29:03
【问题描述】:
结构是
struct student
{
char name[10];
int roll;
int percent;
};
struct student s[5];
struct student *p;
上面的声明是全局的。这是我的冒泡排序函数。
void sort(int n)
{
int i,j,k;
struct student temp;
for(i=0;i<=(n-2);i++)
{
for(j=0;j<(n-i-1);j++){
if((p+j)->percent>(p+j+1)->percent){
//Define the temp variable of Structure type
temp=*(p+j);//Interchange s[j] and s[j+1]
*(p+j)=*(p+j+1);
*(p+j)=temp;
}
}
}
我想避免使用点运算符来访问结构的元素 用于交换的临时变量已声明为结构类型。 此冒泡排序功能不起作用。这些是我认为我搞砸的地方。请指出错误。
if((p+j)->percent>(p+j+1)->percent){
temp=*(p+j);//Interchange s[j] and s[j+1]
*(p+j)=*(p+j+1);
*(p+j)=temp;
【问题讨论】:
-
有什么问题吗?您似乎避免使用点运算符。代码不行吗?
-
如果有错误请指出错误,冒泡排序功能不起作用。我使用点运算符编写了一个相同的函数,并且那个函数正在工作。我在某处弄乱了语法吗?
-
它不能像无法编译或不能像未排序那样工作?
-
它不会对结构数组进行排序,非常感谢您的帮助。我发现了错误。代码的最后一行应该是 *(p+j+1)=temp;
-
每当你想为一些基指针
p和索引i写*(p + i)时,请对自己仁慈一点,写p[i]。更少的令牌,更清晰,更少的混乱,双赢。请问?
标签: c pointers structure bubble-sort