15

Here is the declaration

dll_DoublyLinkedNode *dll_search(const dll_DoublyLinkedList list, void *key, int (*compare)(void *data, void *key)){

Should I split it? Should I just leave it as it is? Perhaps I should do something different?

1
  • First off, typedef int (*cmp_func)(void *, void *);
    – user529758
    Commented May 10, 2013 at 21:44

2 Answers 2

20

This is completely a matter of taste, but I'd be a fan of something along the lines of:

dll_DoublyLinkedNode *dll_search(
  const dll_DoublyLinkedList list, 
  void *key, 
  int (*compare)(void *data, void *key)
){

Additionally, you could typedef the function pointer type you reference and give it a handy name.

17

There are many conventions for writing large function declarations in C. Here are some of the most common ones:

      /////////////////////////////
     //  Type //    Meaning    ///
    /////////////////////////////
   //  `.`  // Single space   //
  //  `-|` // Half an indent //
 //  `->` // Full indent    //
/////////////////////////////

// One line
static inline int namespace_class_method(class_t *self, a_t a, b_t b, c_t c);

// Traditional C
static inline int namespace_class_method(self, a, b, c)
..class_t *self;
..a_t a;
..b_t b;
..c_t c;
{
../* Traditional K&R C had no declarations */
}

// Line wrapping
static inline int namespace_class_method(class_t *self, a_t a,
b_t b, c_t c);

// Naive
static inline int namespace_class_method(class_t *self, a_t a,
--->b_t b, c_t c);

// Linux, VIM[1]
static inline int namespace_class_method(class_t *self, a_t a,
........b_t b, c_t c);

// GNU
static inline int
namespace_class_method(class_t *self, a_t a,
.......................b_t b, c_t c);

// Java[2]

static inline int
namespace_class_method
-|(
--->class_t *self,
--->a_t a,
--->b_t b,
--->c_t c
-|);

// Haskell style
static inline int
namespace_class_method
-|( class_t *self
-|, a_t      a
-|, b_t      b
-|, c_t      c );

Chose one and stick to it. Consistency and readability are more important than religion and style.

[1]: With cindent, turned on for C/C++ by default.
[2]: Not sure if it was in Java, JavaScript, or somewhere else, but I've seen this one used extensively before.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.