ptrToFunc.c
#include <stdio.h>
#include <string.h>
int main(void){
char *(*ptr_strcpy)(char *,char *) ; //関数へのポインタを定義 (誤)
char str1[10];
char str2[] = "hoge";
ptr_strcpy = strcpy; //ここでエラーになる
printf( "%s\n", str1);
return 0;
}
コンパイルしてみると、下記の警告メッセージが出る。
warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
ptr_strcpy = strcpy;
あれ、char*はstrcpy関数の戻り値でしょ 関数へのポインタとなる「ptr_strcpy」の各引数の型もちゃんと定義してるね
調べてみると、実は2番目の引数に
const修飾子があるようだ…
char *(*ptr_strcpy)(char *, const char *) ; //const修飾子を2番目の引数に追加
問題なくコンパイルできた。