/*** mystrcat: ***/ #include<stdio.h> #include<string.h> char *mystrcat(char *s1,char *s2) { strcat(s1,s2); return s1; } int main() { char s1[100] = "hello"; char s2[100] = " world"; char *s = mystrcat(s1,s2); printf("s = %s ",s); return 0; }
指向函数的指针:
#include<stdio.h> #include<string.h> char *mystrcat(char *s1,char *s2) { strcat(s1,s2); return s1; } char *test(char *(*p)(char *,char *),char *s1,char *s2) { return p(s1,s2); } int main() { char s1[100] = "hello"; char s2[100] = " world"; //char *s = mystrcat(s1,s2); char *s = test(mystrcat,s1,s2); printf("s = %s ",s); return 0; }
typedef简化:
#include<stdio.h> #include<string.h> char *mystrcat(char *s1,char *s2) { strcat(s1,s2); return s1; } typedef char*(*STRCAT)(char *,char *); char *test(STRCAT p,char *s1,char *s2) { return p(s1,s2); } int main() { char s1[100] = "hello"; char s2[100] = " world"; //char *s = mystrcat(s1,s2); char *s = test(mystrcat,s1,s2); printf("s = %s ",s); return 0; }