0

if I do:

void foo() {
    if( .. ) {
      inline int baa(..) { return .. }
    } else {
      inline int baa(..) { return .. }
    }

}

And call: baa(..) inside foo function I get implicit declaration of 'baa'. But if I do prototype declaration: inline int baa(int); the error is inline function 'baa' declared but never defined. I'm using inline function to replace macro-function.

How to fix this?

EDIT better: can someone explain why the compiler claims the above error message?

Jack
  • 16,276
  • 55
  • 159
  • 284
  • duplicate of : http://stackoverflow.com/questions/957592/functions-inside-functions-in-c – Jay D Jun 14 '12 at 03:22

3 Answers3

4

I'm using inline function to replace macro-function.

You cannot define functions inside functions, you can only declare them. Declaring them makes their names visible in that scope but those functions still need a definition somewhere, and we know we cannot define them within another function. That's why the compiler says that the function was declared but never defined.

Instead, simply declare your functions inline at a global scope.

K-ballo
  • 80,396
  • 20
  • 159
  • 169
  • when you say "declare your functions inline at a global scope" did you mean `void myfunction()` or `void myfunction() { .. }`. But anyway,in this case, `baa` is declared dynamically depeding of a if-else condition. It's a try to replace to `if(..) #define foo(x) .. else #define foo(y)` that did not works for macros. – Jack Jun 14 '12 at 03:24
  • @Jack: `void myfunction()` is a _declaration_, `void myfunction(){ ... }` is a _definition_. A `#define` inside an `if` is not guarded by the conditional, so your function is not being declared dynamically. In fact, the _C preprocessor_ knows nothing about `if`, `else`, nor any other construct from the _C language_... – K-ballo Jun 14 '12 at 03:26
  • You are right. Now I see that it's bit no-sense what I have tried with macro-function. It will be defined anyway. But about inline functions, it's applied to it as well? Consider this code: http://pastebin.com/KurNMF51 I want to do something like this. The printf(), could to print `2`. – Jack Jun 14 '12 at 03:39
  • 1
    @Jack: No, not like that. Perhaps you can leverage _function pointers_? – K-ballo Jun 14 '12 at 03:44
  • @K-ballo: But do I need point to another existing function or I can do `baa = &((arg){ return ..})` gcc-extensions are very appreciated too. – Jack Jun 14 '12 at 03:53
  • @Jack: You would need to point to existing functions, yes. I wouldn't know about _non-standard extensions_, I avoid them like the plague... – K-ballo Jun 14 '12 at 03:59
  • I usually avoid them too,but sometimes I open an exception to gcc-extensions. :) Thanks very much. I accepted your question. – Jack Jun 14 '12 at 04:08
  • @K-ballo: Do you consider it's bad designer? http://pastebin.com/RBqTjE3i it are inside a loop, but too inside a if-condition that run only one time, after it a break is called. – Jack Jun 14 '12 at 04:18
  • The `foo` in if-statement and `int etc = foo();` was a typo only. – Jack Jun 14 '12 at 04:21
1

Although it's not part of the standard, you could compile with the flag -fnested-functions

eqzx
  • 5,323
  • 4
  • 37
  • 54
  • hm.. it's for GCC? does not works for me(I'm using MinGW): `unrecognzided command line option -fnested-functions` – Jack Jun 14 '12 at 03:27
  • It's not available for gcc on windows.But your answer is valid yet! Thanks and +1. – Jack Jun 14 '12 at 03:58
1

K-ballo's suggestion indeed seems best, but he didn't actually point out the problem.

A nested function, defined in a block, is valid only within the block. Just like a normal variable. You define one function in the if clause, and another in the else clause, and each is valid within that clause. But neither is valid after the if-else has been closed.

It's exactly the same with variables:

if (a) {
    int x = 3;
} else {
    int x = 4;
}
// x isn't valid here.
ugoren
  • 16,023
  • 3
  • 35
  • 65