宁波市软件产业园:C++函数指针的调用

来源:百度文库 编辑:查人人中国名人网 时间:2024/05/03 01:16:10
// 5.cpp : Defines the entry point for the console application.

//注意其中int (*cmp)(const char *,const char *)指针函数调用形式
#include "stdafx.h"
#include <iostream.h>
#include <string.h>
#include <stdio.h>

void fi(char *p1,char *p2,int (*cmp)(const char *,const char *));
void cp(char *p1,char *p2,int (*pc)(const char *,const char *));
void main()
{
char p3[80],p4[80];
gets(p3);
gets(p4);
int (*p)(const char *,const char *);
int (*q)(const char *,const char *);
p=strcmp;
q=strcat;
fi(p3,p4,p);
cp(p3,p4,q);
}
void fi(char *p1,char *p2,int (*cmp)(const char *,const char *))
{
cout<<"Testing if the two sentences are eaqual: ";
if(!cmp(p1,p2))
cout<<"eaqual"<<endl;
else
cout<<"unequal"<<endl;
}
void cp(char *p1,char *p2,int (*pc)(const char *,const char *))
{
pc(p1,p2);
cout<<p1<<endl;
}
错误信息:
--------------------Configuration: 5 - Win32 Debug--------------------
Compiling...
5.cpp
D:\Work\5\5.cpp(19) : error C2440: '=' : cannot convert from 'char *(__cdecl *)(char *,const char *)' to 'int (__cdecl *)(const char *,const char *)'
This conversion requires a reinterpret_cast, a C-style cast or function-style cast
Error executing cl.exe.

5.exe - 1 error(s), 0 warning(s)
to:pengyifan0803
那么strcmp的原型是?为什么在这里是正确的?我之前也做了修改:将int ->char,但是还是不行。
能否帮我修改下?感激不尽!

strcat的原型为char * strcat (char *s, const char *ct)
而您的声明q为int (*q)(const char *,const char *);

所以报错:不能将char *(__cdecl *)(char *,const char *)的函数转换为
'int (__cdecl *)(const char *,const char *)

int strcmp( const char *string1, const char *string2 );