Thread: forward declarations of nested functions in GNU C

  1. #1
    Registered User
    Join Date
    Dec 2017
    Posts
    6

    forward declarations of nested functions in GNU C

    Hello,
    I encountered a problem when I tried to write a program in GNU C, which allows nested (local) functions to be used. I wasn't able to make a forward declaration to work. The problem is illustrated with the following short program

    Code:
    // GNU c program demonstrating problem with forward declarations 
    // of nested functions.
    // Compiler produces error "static-declaration follows non-static 
    //declaration";
    // when I place the keyword "static" in front of "void Inner2();", I still
    // get an error
    // Program was tested on gcc (Debian 8.3.0-6) as well as on
    // gcc (Rev2, Built by MSYS2 project) 9.2.0
    #include<stdio.h>
    
    void Outer(){
      void Inner2(); // "non-static declaration"
      void Inner1(){
        Inner2();
      }
      void Inner2(){ // "static-declaration"
        printf("%s","Hello World!");
      }
    
      Inner1();
    }
    
    int main(){
      Outer();
    }
    I would appreciate any information how to make forward declarations of nested functions work.
    Regards,
    bostjanv

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    834
    There is no standard answer as this is non-standard functionality.
    However, for gcc you need to put the keyword "auto" in front of the forward declaration of a nested function.
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    6
    Thanks a lot! Your answer worked. And Happy New Year.
    bostjanv

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Understanding forward declarations of classes, pimpl
    By Boxknife in forum C++ Programming
    Replies: 2
    Last Post: 04-22-2010, 01:29 AM
  2. Does gcc hate Forward declarations?
    By SevenThunders in forum C++ Programming
    Replies: 12
    Last Post: 03-16-2009, 02:03 PM
  3. Replies: 10
    Last Post: 12-16-2008, 10:28 AM
  4. forward class declarations
    By manzoor in forum C++ Programming
    Replies: 17
    Last Post: 12-05-2008, 03:55 AM
  5. Forward Declarations in .net
    By Cornpops in forum C++ Programming
    Replies: 2
    Last Post: 04-01-2003, 02:22 PM

Tags for this Thread