-4

I'm writing this C code that involves passing around a lot of function pointers, and sometimes writing all the arguments that a function pointer takes when defining a function that takes it as a variable can significantly increase the length of the function definition and thus decrease its readability. For instance, say I have this function foo that takes three ints and a function pointer to perform a certain operation. The typical way to declare a function like foo is something along the lines of:

int foo(int a, int b, int c, int (*operation) (int, int, int));

I was wondering if it would be on topic for SO to ask if there was a possible way to avoid the redundancy of having to re-mention the variable types another time in the type of the function pointer.

An answer to this question suggests that it is possible to only use empty parentheses () , which translates in the above example to:

int foo(int a, int b, int c, int (*operation) ());

However, a comment to the same post states that this kind of syntax is going to be removed in C23:

As a side note, this is obsolescent C since year 1989. Empty parenthesis () no prototype style should not be used. And it will finally get removed in upcoming C23

2
  • 15
    Is there any particular reason you think this would be off-topic? It's a narrow programming question with an objective answer.
    – vandench
    Jun 27 at 0:52
  • 1
    @vandench-onstrike I was wondering if it doesn't tilt toward a question about programming style or design or doing something that shouldn't be done. Jun 27 at 8:44

1 Answer 1

4

If we boil down your question, you ask:

How can I avoid this redundancy?

Analyzing this question (-type) by the dont-ask gives us:

  • Is such question about programming? → Yes, you want to know how to code something. It is not off-topic.

  • Is it a subjective question*? → No, you want to make your code more efficient. Efficiency is objective, you can measure it. Your question should clarify by what metric (time, resource consumption, code/file size, read- & maintainability, etc) you want to make your code more efficient.

Your question is good to go, as long as you provide enough detail.


* Some subjective questions are allowed, as per the dont-ask:

All subjective questions are expected to be constructive. What does that mean? Constructive subjective questions:

  • inspire answers that explain “why” and “how”
  • tend to have long, not short, answers
  • have a constructive, fair, and impartial tone
  • invite sharing experiences over opinions
  • insist that opinion be backed up with facts and references
  • are more than just mindless social fun
3
  • 3
    It’s not about efficiency of compiled code though, it’s about readability and avoiding redundancy. It’s still on topic, but not for the reasons mentioned in this answer. Jun 27 at 14:41
  • 5
    "Efficiency is objective, you can measure it (time, resource consumption, code/file size, etc)." Questions about efficiency require OP to clarify the metrics they care about and what acceptable values of said metrics will be. Questions asking only "how can I make this code more efficient" are close-worthy as needing details.
    – TylerH
    Jun 27 at 19:18
  • 1
    @CrisLuengo readability means having to spend less time on (trying to understand) a project, making you more efficient.
    – A-Tech
    Jun 28 at 10:12

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .