//program to display message using function as a parameter
#include<stdio.h>
#include<process.h>
//return name
char name_display(char *name)
{
return *name;
}
//display name
void display(char (*display)(char *name1),char *name2)
{
char name3;
name3=display(name2);
printf("name is %s",name3);
}
void main()
{
display(name_display,"message");
}
答案 0 :(得分:1)
当然:printf("name is %s",name3);
想打印一个字符串,但name3
只是char
。要么修复它:
printf("name is %c",name3);
或将您的代码修改为:
char* name_display(char *name)
{
return name;
}
//display name
void display(char* (*display)(char *name1),char *name2)
{
char* name3;
ie:使用字符串而不是字符。
答案 1 :(得分:1)
尝试以下更改 - (查看程序中的修复程序)
#include<stdio.h>
#include<string.h>
//return name
char* name_display(char *name) // Fix 1, when you are returning char array use char *
{
return name; // Fix 2 (return only the name of the array, no need of *name)
}
//display name
void display(char* (*display_call)(char *name1),char *name2) // Don't use same Function name. When you look at the program again it may confuse you
{
char *name3; // Fix 3
name3 = display_call(name2);
printf("name is %s \n",name3);
}
void main()
{
display(name_display,"message");
}