problem about function pointer

N

neilcancer

My English is poor...

There are some sort algorithms which sort a sequencial list. The
sequencial list was defined in "list-seq.h".

sort_and_time() accepts one of these sort functions. It uses the sort
function to sort the list and compute the time consumed by the sort
function.

I wrote the program as below. When sort_and_time() is called in main(),
there are some errors:(in VC++6)

D:\ds\funp.c(31) : error C2095: 'sort_and_time' : actual parameter has
type 'void' : parameter 1
D:\ds\funp.c(31) : error C2198: 'sort_and_time' : too few actual
parameters

I have no idea about the error. Can you tell me? Thank you

#include<time.h>
#include"list-seq.h"

void insertsort(liststruct *list);
void heapsort(liststruct *plist);
void heapadjust(liststruct *plist,int s,int m);
void bubblesort(liststruct *list);
void selsort(liststruct *list);
void sort_and_time(void (*sort)(liststruct *));

liststruct list,temp,*plist,*templist;
time_t first,second;
int i,x,way,j,k;

int main()
{ plist=&list;
templist=&temp;
initial(templist);
initial(plist);
i=1;
printf("输入数æ®ï¼Œä»¥9999结æŸï¼š\n");
scanf("%d",&x);
while(x!=9999)
{ insert(plist,i,x);
insert(templist,i,x);
i++;
scanf("%d",&x);
}
for(i=0;i<plist->length;i++)
printf("%d\n",plist->elem);
sort_and_time((*insertsort)(plist));
sort_and_time((*bubblesort)(plist));
sort_and_time((*heapsort)(plist));
sort_and_time((*selsort)(plist));

}

void sort_and_time(void (*sort)(liststruct *))
{ first=clock();
for(k=0;k<100000;k++)
(*sort)(plist);
second=clock();
printf("after sort:\n");
for(i=0;i<plist->length;i++)
printf("%d\t",plist->elem);
printf("\n所用时间:%lf\n",(double)(second-first)/CLOCKS_PER_SEC);
initial(plist);
for(i=1;i<=templist->length;i++)
insert(plist,i,templist->elem[i-1]);
}

void insertsort(liststruct *list)
{ int i,j,temp;
for(i=1;i<list->length;i++)
if(list->elem<list->elem[i-1])
{ temp=list->elem;
list->elem=list->elem[i-1];
for(j=i-1;temp<list->elem[j];--j)
list->elem[j+1]=list->elem[j];
list->elem[j+1]=temp;
}
}

void heapadjust(liststruct *list,int s,int m)
{ int j,rc;
rc=list->elem;
for(j=2*s;j<=m;j*=2)
{ if(j<m&&(list->elem[j]<list->elem[j+1]))
j++;
if(!(rc<list->elem[j]))
break;
list->elem=list->elem[j];
s=j;
}
list->elem=rc;
}

void heapsort(liststruct *list)
{ int i,temp;
for(i=(list->length-1)/2;i>=0;i--)
heapadjust(list,i,list->length-1);
for(i=list->length-1;i>=1;i--)
{ temp=list->elem;
list->elem=list->elem[0];
list->elem[0]=temp;
heapadjust(list,0,i-1);
}
}

void bubblesort(liststruct *list)
{ int i,j,temp;
for(j=0;j<list->length-1;j++)
for(i=1;i<list->length-1;i++)
if(list->elem>list->elem[i+1])
{ temp=list->elem;
list->elem=list->elem[i+1];
list->elem[i+1]=temp;
}
}

void selsort(liststruct *list)
{ int i,j,temp,min;
for(i=0;i<list->length-1;i++)
{ min=i;
for(j=i;j<list->length;j++)
if(list->elem[j]<list->elem[min])
min=j;
if(i!=min)
{ temp=list->elem;
list->elem=list->elem[min];
list->elem[min]=temp;
}
}
}
 
K

kleuske

(e-mail address removed) schreef:
My English is poor...

Never mind. It's good enough for me to understand.
D:\ds\funp.c(31) : error C2095: 'sort_and_time' : actual parameter has
type 'void' : parameter 1
D:\ds\funp.c(31) : error C2198: 'sort_and_time' : too few actual
parameters

Hmmm... something's wrong.
I have no idea about the error. Can you tell me? Thank you

Yes.

void sort_and_time(void (*sort)(liststruct *));

The only parameter you define here is a pointer to a function. Nothing
wron there.
sort_and_time((*insertsort)(plist));

But this is. You are not trying to pass a pointer to a function, here,
but the _result_ of a function call, which does not return anything
(it's of type 'void').
sort_and_time((*bubblesort)(plist));
sort_and_time((*heapsort)(plist));
sort_and_time((*selsort)(plist));

and you do it three times more.


Change the decl to
void sort_and_time(void (*sort)(liststruct *), liststruct *);

and the calls to

sort_and_time(insertsort, plist);
sort_and_time(bubblesort, plist);
sort_and_time(heapsort, plist);
sort_and_time(selsort, plist);
 
C

CBFalconer

My English is poor...
.... snip ...

I have no idea about the error. Can you tell me? Thank you

#include<time.h>
#include"list-seq.h"

void insertsort(liststruct *list);
void heapsort(liststruct *plist);
void heapadjust(liststruct *plist,int s,int m);
void bubblesort(liststruct *list);
void selsort(liststruct *list);
void sort_and_time(void (*sort)(liststruct *));

liststruct list,temp,*plist,*templist;
time_t first,second;
int i,x,way,j,k;

int main()
{ plist=&list;
templist=&temp;
initial(templist);
initial(plist);
i=1;
printf("输入数æ®ï¼Œä»¥9999结æŸï¼š\n");
scanf("%d",&x);
while(x!=9999)
{ insert(plist,i,x);
....

Regardless of your English, you could attempt to indent your code
properly and make it readable. As it is I will not even attempt to
read it. Others may. You also omitted to show us the "list-seq.h"
header file, nor the also presumed "list-seq.c" code file.

Reduce your code to a minimum compilable file that exhibits the
problem, publish that and describe the actual problem. Do not use
tabs, and do not let lines exceed about 65 chars.
 
B

Betaver

My English is poor...

May I have something else to say.
I find almost every Chinese have no confidence about their English,
though they have leart a lot at school. My English is poor but I've
never see one cannot understand what I have said, especially on
computer languages -- because the code you wrote is the best explain.
Don't worry about your English and ask in c.l.c often, here you can get
excellent answers easier.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,108
Latest member
AlbertEste
Top