Thread: accessing a static function

  1. #1
    Registered User
    Join Date
    Oct 2022
    Posts
    92

    accessing a static function

    I am trying indirectly accessing a static function using function pointer within a program. , I've encountered errors that I'm struggling to resolve.

    main.c

    Code:
    #include<stdio.h>
    
    void (*fp)();
    
    
    static void foo()
    {
    	printf("Welcome \n");
    	
    }
    
    
    int main()
    {
        fp = &foo;
    	
    	return 0;
    
    }


    file1.c
    Code:
    extern void (*fp)();
    
    fp();


    Output
    Code:
    file1.c:3:1: warning: data definition has no type or storage class fp();
     ^~
    file1.c:3:1: warning: type defaults to 'int' in declaration of 'fp' [-Wimplicit-int]
    file1.c:3:1: error: 'fp' redeclared as different kind of symbol
    file1.c:1:15: note: previous declaration of 'fp' was here
    
     extern void (*fp)();


  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Works for me.
    Code:
    $ cat foo.c
    #include<stdio.h>
    
    void bar();
    
    void (*fp)();
    
    static void foo()
    {
        printf("Welcome \n");
    }
    
    int main()
    {
        fp = &foo;
        bar();
        return 0;
    }
    $ cat bar.c
    #include <stdio.h>
    extern void (*fp)();
    
    void bar() {
        fp();
    }
    $ gcc foo.c bar.c
    $ ./a.out 
    Welcome
    But what you posted of file1.c is broken to the point of useless, so it's impossible to say for sure what you might be doing wrong.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2023
    Posts
    7
    Quote Originally Posted by Salem View Post
    Works for me.
    Code:
    $ cat foo.c
    #include<stdio.h>
    
    void bar();
    
    void (*fp)();
    
    static void foo()
    {
        printf("Welcome \n");
    }
    
    int main()
    {
        fp = &foo;
        bar();
        return 0;
    }
    $ cat bar.c
    #include <stdio.h>
    extern void (*fp)();
    
    void bar() {
        fp();
    }
    $ gcc foo.c bar.c
    $ ./a.out 
    Welcome
    But what you posted of file1.c is broken to the point of useless, so it's impossible to say for sure what you might be doing wrong.
    hi as i am in process of learning, i think there is one issue with his code, i would say his file1.c 3rd row acts as redeclaration as it is without type specified it is defaulted to int, i am not sure but he probably can't call fp(); in global scope of file1.c module. What do you think?

  4. #4
    Registered User
    Join Date
    Oct 2022
    Posts
    92
    Quote Originally Posted by Salem View Post
    Works for me..
    When I ran your code on my window PC I got output "The system cannot execute the specified program"

    Code:
    command 
    C:\Users\Kittu\Desktop\C Example>gcc -o hello main.c file1.c
    
    
    C:\Users\Kittu\Desktop\C Example>hello
    The system cannot execute the specified program.
    I used my file names

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    You're on windows.
    Try calling it hello.exe
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Oct 2022
    Posts
    92
    Quote Originally Posted by Salem View Post
    You're on windows.
    Try calling it hello.exe
    Yes It works

    I don't understand why need of bar function in your code ?

    why it can't be like

    Code:
    externvoid(*fp)();
    fp();

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > why it can't be like
    Why would you think that?

    You can't just call a function randomly from a line like that.

    It has to be inside a function definition.

    As it stands, it looks like some old-style function declaration - declare fp() as a function (implicitly) returning int, and taking unspecified parameters.

    It certainly doesn't call the function.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Static function static or not?
    By Vespasian_2 in forum C Programming
    Replies: 7
    Last Post: 04-26-2017, 02:04 AM
  2. Replies: 3
    Last Post: 04-25-2017, 10:25 AM
  3. accessing static functions in header via macro
    By wirmius in forum C Programming
    Replies: 1
    Last Post: 05-11-2013, 06:52 AM
  4. Replies: 8
    Last Post: 01-19-2009, 07:42 PM
  5. Replies: 6
    Last Post: 12-13-2007, 08:20 PM

Tags for this Thread