typedef和C中函数的指针

Yen*_*Liu 9 c pointers typedef function

以下是Richard Reese的"理解和使用C指针"中的示例.我的问题是它应该是第7行的"typedef int(*fptrOperation)......"吗?我尝试了他们两个,但他们都运作良好.我搜索了typedef和指针在线运行两天的用法,但仍然没有想出来.谢谢你的帮助~~

#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;
}    
Run Code Online (Sandbox Code Playgroud)

und*_*gor 5

不要紧.

这是因为对于函数参数,函数类型会自动转换为指向函数的指针(ISO/IEC 9899: 2011,6.7.6.3,§8):

参数声明为''函数返回类型''应调整为''指向函数返回类型'的指针,如6.3.2.1所述.