qsort结构和指针 [英] qsort wiht struct and pointers

查看:84
本文介绍了qsort结构和指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我的代码出了什么问题?我无法编译和排序我的结构数据...


这是我的代码的安宁......

typedef struct student

{

int num_stu;

char name [40];

char team [5];

struct student * next;

}学生;


typedef学生* PtrStudent;


void Insert_Student( PtrStudent * lista);

PtrStudent apStudent = NULL;

****************

int sortbynumber(const Student * c1,const Student * c2)

{

return(c1-> num_stu - c2-> num_stu);
< br $>
}

qsort(aux,PtrStudent,sizeof(学生),(void *)sortbynumber);

printf(" \ nn \\号码:\ n");

for(i = 0; i< PtrStudent; i ++){

printf("%d",aux [i ] num_stu);

}


Kisses

解决方案

Maria Mela < h4 *** @ netvisao.ptwrites:


typedef struct student

{

int num_stu;

char name [40];

char team [5];

struct student * next;

}学生;


typedef学生* PtrStudent;



一般来说,我不建议创建typedef,但

指针类型的typedef特别令人困惑。
< blockquote class =post_quotes>
int sortbynumber(const Student * c1,const Student * c2)

{

return(c1-> num_stu - c2 - > num_stu);


}



这个比较函数容易溢出,它没有

正确的参数类型。这是一个更好的版本:


int

sortbynumber(const void * a_,const void * b_)

{

const学生* a = a_;

const学生* b = b_;

if(c1-> num_stu c2-> num_stu)

返回1;

else if(c1-> num_stu< c2-> num_stu)

返回-1;

else

返回0;

}


qsort(aux,PtrStudent,sizeof(学生),(void *)sortbynumber);



这有很多问题。第一个参数应该是
是一个指向数组开头的指针,但你没有提到

是什么aux是。第二个参数应该是数组中

元素的数量,但是你试图传递一个类型。

第三个参数似乎是正确的。第四个参数没有

一旦你修复了sortbynumber函数就需要演员。


printf(" \ nn \\号码:\ n");

for(i = 0; i< PtrStudent; i ++){



再次,你将PtrStudent声明为typedef。你明白

什么是typedef?你不能将变量与类型进行比较。


printf("%d",aux [i] num_stu);

}



这是缺少一个。。

-

"表达方式一点也不清楚*并且只有专家可以实际上

对此有疑问

--Dan Pop

< br>

在文章< ne ******************** @ newsfront4.netvisao.pt>,

Maria Mela< ; h4 *** @ netvisao.ptwrote:


>我的代码有什么问题?我无法编译和排序我的结构数据...


> int sortbynumber(const Student * c1,const Student * c2)
{

return(c1-> num_stu - c2-> num_stu);
}
qsort(aux,PtrStudent,sizeof(学生),(void *) sortbynumber);



当你调用qsort时,你正在使用-function-指针sortbynumber

并试图强制它成为一个通用指针-object-,

将其转换为(void *)。向对象转换函数指针

不是由标准C定义的 - 如果需要,实现可以允许它b / b
,但实现不需要允许它。


qsort()的第三个参数不应该是(void *):它

应该是int(* compar)(const void *,const void * )
这是一个指向函数的指针,它接受两个const void *参数和

返回一个int。这几乎与您对

sortbynumber的定义相同。如果你要将sortbynumber定义为


int sortbynumber(const void * c1,const void * c2)

{

return (const学生*)c1-> num_stu -

(const学生*)c2-> num_stu;

}


然后sortbynumber已经有适合qsort

的类型你可以打电话


qsort((void *)aux,(size_t)number_of_entries ,sizeof(学生),

sortbynumber);


请注意,您有PtrStudent作为qsort()的第二个参数,

但是PtrStudent只是一个类型名称,你需要在第二个参数中传递

是表中的条目数。

-

好​​的,只有流行语。两个音节,上衣。 - Laurie Anderson


为你的衣服挑战...

aux在我的代码中...


void Insert_Student(PtrStudent * lista)

{

PtrStudent aux = NULL,start = NULL ;

aux =(PtrAluno)malloc(sizeof(Aluno));


在你看来,我怎么能把这个表达?

qsort(aux,PtrStudent,sizeof(学生),(void *)sortbynumber);


Kisses


" Ben普法夫" < bl*@cs.stanford.eduescreveu na mensagem

news:87 ************ @ blp.benpfaff.org ...


" Maria Mela" < h4 *** @ netvisao.ptwrites:


> typedef struct student
{int / num_stu;
char name [40];
char团队[5];
struct student * next;
}学生;

typedef学生* PtrStudent;



我不建议一般创建typedef,但是

指针类型的typedef特别令人困惑。
< blockquote class =post_quotes>
> int sortbynumber(const Student * c1,const Student * c2)
{
return(c1-> num_stu - c2-> num_stu);

}



这个比较函数很容易溢出,它没有

正确的参数类型。这是一个更好的版本:


int

sortbynumber(const void * a_,const void * b_)

{

const学生* a = a_;

const学生* b = b_;

if(c1-> num_stu c2-> num_stu)

返回1;

else if(c1-> num_stu< c2-> num_stu)

返回-1;

else

返回0;

}


> qsort(aux,PtrStudent, sizeof(学生),(void *)sortbynumber);



这有很多问题。第一个参数应该是
是一个指向数组开头的指针,但你没有提到

是什么aux是。第二个参数应该是数组中

元素的数量,但是你试图传递一个类型。

第三个参数似乎是正确的。第四个参数没有

一旦你修复了sortbynumber函数就需要演员。


> printf(" \ n \ NumberSum:\ n");
for(i = 0; i< PtrStudent; i ++){



再次,你声明了PtrStudent作为typedef。你明白

什么是typedef?您无法将变量与类型进行比较。


> printf("%d",aux [i] num_stu);
}



这是缺少一个。。

-

"表达式isn根本不清楚*并且只有专家可以实际上

对此有疑问

--Dan Pop


Hello Everyone,

What′s wrong in my code?? I can′t compile and sort my struct data...

Here′s peace of my code...
typedef struct student
{
int num_stu;
char name[40];
char team[5];
struct student *next;
}Student;

typedef Student *PtrStudent;

void Insert_Student (PtrStudent *lista);
PtrStudent apStudent =NULL;
****************
int sortbynumber(const Student *c1, const Student *c2)
{
return (c1->num_stu - c2->num_stu);

}
qsort (aux, PtrStudent, sizeof (Student), (void *) sortbynumber);
printf("\n\Sort Numbers: \n");
for(i=0; i<PtrStudent; i++){
printf("%d", aux[i]num_stu);
}

Kisses

解决方案

"Maria Mela" <h4***@netvisao.ptwrites:

typedef struct student
{
int num_stu;
char name[40];
char team[5];
struct student *next;
}Student;

typedef Student *PtrStudent;

I don''t recommend creating typedefs, in general, but typedefs for
pointer types are particularly confusing.

int sortbynumber(const Student *c1, const Student *c2)
{
return (c1->num_stu - c2->num_stu);

}

This comparison function is prone to overflow and it doesn''t have
the right parameter types. Here''s a better version:

int
sortbynumber(const void *a_, const void *b_)
{
const Student *a = a_;
const Student *b = b_;
if (c1->num_stu c2->num_stu)
return 1;
else if (c1->num_stu < c2->num_stu)
return -1;
else
return 0;
}

qsort (aux, PtrStudent, sizeof (Student), (void *) sortbynumber);

There are many things wrong with this. The first argument should
be a pointer to the beginning of an array, but you didn''t mention
what "aux" is. The second argument should be the number of
elements in the array, but you''re trying to pass a type. The
third argument seems to be correct. The fourth argument does not
need the cast once you''ve fixed the sortbynumber function.

printf("\n\Sort Numbers: \n");
for(i=0; i<PtrStudent; i++){

Again, you declared PtrStudent as a typedef. Do you understand
what a typedef is? You can''t compare a variable to a type.

printf("%d", aux[i]num_stu);
}

And this is missing a ".".
--
"The expression isn''t unclear *at all* and only an expert could actually
have doubts about it"
--Dan Pop


In article <ne********************@newsfront4.netvisao.pt>,
Maria Mela <h4***@netvisao.ptwrote:

>What′s wrong in my code?? I can′t compile and sort my struct data...

>int sortbynumber(const Student *c1, const Student *c2)
{
return (c1->num_stu - c2->num_stu);
}
qsort (aux, PtrStudent, sizeof (Student), (void *) sortbynumber);

When you call qsort, you are taking the -function- pointer sortbynumber
and trying to force it to be a generic pointer to an -object-,
by casting it to (void *) . Casting function pointers to objects
is not defined by standard C -- implementations can allow it
if they want, but implementations are not required to allow it at all.

The third argument to qsort() should not be a (void *) : it
should be int (*compar) (const void *, const void *)
which is a pointer to a function that takes two const void* arguments and
returns an int. That is -almost- the same as your definition of
sortbynumber . If you were to define sortbynumber as

int sortbynumber(const void *c1, const void *c2)
{
return (const Student *)c1->num_stu -
(const Student *)c2->num_stu;
}

then sortbynumber would already have the proper type for qsort
and you would be able to call

qsort( (void *) aux, (size_t) number_of_entries, sizeof (Student),
sortbynumber );

Note that you had PtrStudent as your second argument to qsort(),
but PtrStudent is just a type name, and what you need to pass
in the second argument is the number of entries in the table.
--
Okay, buzzwords only. Two syllables, tops. -- Laurie Anderson


Thks for your answear...
the "aux" in my code...

void Insert_Student (PtrStudent *lista)
{
PtrStudent aux=NULL, start=NULL;
aux=(PtrAluno) malloc (sizeof(Aluno));

In you r opinion, how can i put this expression?
qsort (aux, PtrStudent, sizeof (Student), (void *) sortbynumber);

Kisses

"Ben Pfaff" <bl*@cs.stanford.eduescreveu na mensagem
news:87************@blp.benpfaff.org...

"Maria Mela" <h4***@netvisao.ptwrites:

>typedef struct student
{
int num_stu;
char name[40];
char team[5];
struct student *next;
}Student;

typedef Student *PtrStudent;


I don''t recommend creating typedefs, in general, but typedefs for
pointer types are particularly confusing.

>int sortbynumber(const Student *c1, const Student *c2)
{
return (c1->num_stu - c2->num_stu);

}


This comparison function is prone to overflow and it doesn''t have
the right parameter types. Here''s a better version:

int
sortbynumber(const void *a_, const void *b_)
{
const Student *a = a_;
const Student *b = b_;
if (c1->num_stu c2->num_stu)
return 1;
else if (c1->num_stu < c2->num_stu)
return -1;
else
return 0;
}

>qsort (aux, PtrStudent, sizeof (Student), (void *) sortbynumber);


There are many things wrong with this. The first argument should
be a pointer to the beginning of an array, but you didn''t mention
what "aux" is. The second argument should be the number of
elements in the array, but you''re trying to pass a type. The
third argument seems to be correct. The fourth argument does not
need the cast once you''ve fixed the sortbynumber function.

>printf("\n\Sort Numbers: \n");
for(i=0; i<PtrStudent; i++){


Again, you declared PtrStudent as a typedef. Do you understand
what a typedef is? You can''t compare a variable to a type.

>printf("%d", aux[i]num_stu);
}


And this is missing a ".".
--
"The expression isn''t unclear *at all* and only an expert could actually
have doubts about it"
--Dan Pop



这篇关于qsort结构和指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆