By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,234 Members | 1,938 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,234 IT Pros & Developers. It's quick & easy.

problem about function pointer

P: n/a
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[i]);
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[i]);
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[i]<list->elem[i-1])
{ temp=list->elem[i];
list->elem[i]=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[s];
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[s]=list->elem[j];
s=j;
}
list->elem[s]=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[i];
list->elem[i]=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[i]>list->elem[i+1])
{ temp=list->elem[i];
list->elem[i]=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[i];
list->elem[i]=list->elem[min];
list->elem[min]=temp;
}
}
}

Jun 19 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a

ne********@gmail.com 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.

<snip>
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);

Jun 19 '06 #2

P: n/a
"ne********@gmail.com" wrote:

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.

--
"I don't know where bin Laden is. I have no idea and really
don't care. It's not that important." - G.W. Bush, 2002-03-13
"No, we've had no evidence that Saddam Hussein was involved
with September the 11th." - George Walker Bush 2003-09-17
Jun 19 '06 #3

P: n/a

ne********@gmail.com wrote:
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.

Jun 20 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.