ここでは、Richard Reeseの「Cのポインタの理解と使用」の例を示します。 私の質問は7行目で "typedef int(* fptrOperation)......"であるべきですか? 私は両方を試しましたが、すべてうまく働いていました。 typedefの使い方とポインタを2日間オンラインで検索しましたが、まだそれを把握していませんでした。それは問題ではありません任意の助け~~typedefとCの関数へのポインタ
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
typedef int (fptrOperation)(const char*, const char*);//
char* stringToLower(const char* string) {
char *tmp = (char*) malloc(strlen(string) + 1);
char *start = tmp;
while (*string != 0) {
*tmp++ = tolower(*string++);
}
*tmp = 0;
return start;
}
int compare(const char* s1, const char* s2) {
return strcmp(s1,s2);
}
int compareIgnoreCase(const char* s1, const char* s2) {
char* t1 = stringToLower(s1);
char* t2 = stringToLower(s2);
int result = strcmp(t1, t2);
free(t1);
free(t2);
return result;
}
void displayNames(char* names[], int size) {
for(int i=0; i<size; i++) {
printf("%s ",names[i]);
}
printf("\n");
}
void sort(char *array[], int size, fptrOperation operation) {
int swap = 1;
while(swap) {
swap = 0;
for(int i=0; i<size-1; i++) {
if(operation(array[i],array[i+1]) > 0){
swap = 1;
char *tmp = array[i];
array[i] = array[i+1];
array[i+1] = tmp;
}
}
}
}
int main(int argc, char const *argv[])
{
char* names[] = {"Bob", "Ted", "Carol", "Alice", "alice"};
sort(names,5,compareIgnoreCase);
displayNames(names,5);
return 0;
}
https://stackoverflow.com/questions/17914057/is-an-asterisk-optional-in-a-function-ポインタ –
gcc -Wallオプションで警告しない、私は関数ポインタを宣言する方法の制限はないと思います。コンパイラは 'sort(names、5、compareIgnoreCase);'がコンパイルされるとき、 'compareIgnoreCase'のアドレスが使用される他の方法がないので、マジックそのものを行います。 – LPs