C++函数指针的调用
//5.cpp:Definestheentrypointfortheconsoleapplication.//注意其中int(*cmp)(constchar*,const...
// 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,但是还是不行。
能否帮我修改下?感激不尽! 展开
//注意其中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,但是还是不行。
能否帮我修改下?感激不尽! 展开
2个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询