Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

typedef and pointer to function in C

Here is an example from 'Understanding and Using C Pointers' by Richard Reese. My question is should it be "typedef int (*fptrOperation)......" in the 7th line? I tried both of them, but they all worked well. I searched the usage of typedef and pointer to function online for two days but still didnit figure it out. Thanks for any help~~

#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;
}    
like image 400
Yen Hung Liu Avatar asked Oct 06 '15 09:10

Yen Hung Liu


2 Answers

It does not matter.

This is because for function parameters, function types are automatically translated to pointer-to-function (ISO/IEC 9899:2011, 6.7.6.3, §8):

A declaration of a parameter as ‘‘function returning type’’ shall be adjusted to ‘‘pointer to function returning type’’, as in 6.3.2.1.

like image 135
undur_gongor Avatar answered Nov 09 '22 13:11

undur_gongor


C99 6.3.2.1 Lvalues, arrays, and function designators:

4 A function designator is an expression that has function type. Except when it is the operand of the sizeof operator or the unary & operator, a function designator with type ‘‘function returning type’’ is converted to an expression that has type ‘‘pointer to function returning type’’.

6.5.3.2 Address and indirection operators:

4 The unary * operator denotes indirection. If the operand points to a function, the result is a function designator;

6.7.5.3 Function declarators (including prototypes):

8 A declaration of a parameter as ‘‘function returning type’’ shall be adjusted to ‘‘pointer to function returning type’’, as in 6.3.2.1.

So, your example and even the following is all valid:

#include <stdio.h>

void foo(void)
{
  puts("foo");
}

int main(void)
{
  foo();
  (&foo)();
  (*foo)();
  return 0;
}
like image 21
Alexey Frunze Avatar answered Nov 09 '22 15:11

Alexey Frunze