0

I just could not get how the typedef function pointers are passed to the "uniqueOrderedListCreate" function below, what I know is when we declare a typedef of a function pointer we need to add a parameter when passing as an argument for example:

UniqueOrderedList uniqueOrderedListCreate(copyElements a, freeElements b, 
elementsEquals c, elementGreaterThan d);

Is there something that I misunderstand here?

#ifndef UNIQUEORDEREDLIST_H_
#define UNIQUEORDEREDLIST_H_
#include <stdbool.h>


typedef struct uniqueOrderedList_t* UniqueOrderedList;

typedef void* Element;

typedef Element (*copyElements)(Element);

typedef void (*freeElements)(Element);

typedef bool (*elementsEquals)(Element, Element);

typedef bool (*elementGreaterThan)(Element e1, Element e2);


UniqueOrderedList uniqueOrderedListCreate(copyElements, freeElements, 
elementsEquals, elementGreaterThan);
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
D.A
  • 13
  • 4
  • You've created a typedef that is a pointer to the list. Then, a typedef for an element in the list. Then four typedefs that are pointers to functions for various operations that can be performed on elements. Then, a function declaration that takes pointers to function pointers that will perform these four operations. It's all good as far as it goes. So, do you want an example of a _call_ to `uniqueOrderedListCreate` and/or _actual_ operator functions (e.g.) `Element my_copy_elements(Element ele) { Element ele2; do_stuff; return ele2; }` – Craig Estey Dec 11 '18 at 02:58
  • Note [Is it a good idea to typedef pointers](https://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers) — the short answer is "No, except perhaps for function pointers". Your `UniqueOrderedList` type contravenes that advice. Also [beware the `_t` suffix](https://stackoverflow.com/questions/231760/); it is reserved by POSIX. Mostly, that won't matter, but on those occasions when it does, your code is what has to change; the system is adhering to the rules of POSIX. – Jonathan Leffler Dec 11 '18 at 04:56

1 Answers1

0

It is not necessary to give function parameters names in function declarations.

UniqueOrderedList uniqueOrderedListCreate(copyElements, freeElements,
    elementsEquals, elementGreaterThan);

This simply declares a function named uniqueOrderedListCreate which takes four unnamed parameters of the specified types and returns a UniqueOrderedList. Where the function definition is given, the parameters must be given names:

UniqueOrderedList uniqueOrderedListCreate(copyElements a, freeElements b,
    elementsEquals c, elementGreaterThan d)
{
    /* ... */
}

There's also no requirement that the names given in a declaration match the names given in the definition. Only the function's signature (return type and parameter types) needs to match.

TypeIA
  • 16,916
  • 1
  • 38
  • 52
  • Still not get it, If we wont use the parameters(which is the function that will be passed) then why passing the pointers as arguments in the first place? – D.A Dec 11 '18 at 18:19
  • @D.A. The point is you don't have to name them in a _declaration_, only a _definition_. The declaration may appear, for example, in a header file, without the parameter names; then the definition appears in a source file with the parameter names. – TypeIA Dec 11 '18 at 18:56
  • So in the .c files I need to put the parameters, right? it seems like the way normal functions work, am I correct? – D.A Dec 11 '18 at 19:11
  • @D.A. You need to understand the difference between a [function declaration](https://en.cppreference.com/w/c/language/function_declaration) and a [function definition](https://en.cppreference.com/w/c/language/function_definition). Once you have that, it's simple: parameters in _definitions_ must always be named, but names _may_ be omitted in _declarations_. The parameter _types_ must always be specified. – TypeIA Dec 11 '18 at 19:54