Declaring static function returning pointer to extern function 
Author Message
 Declaring static function returning pointer to extern function

I want to declare a function that has static linkage and returns a
pointer to a function with extern linkage.  Do the following
declarations acheive that, or do they declare a function that returns
a pointer to a function with static linkage?

typedef void Foo(void);
static Foo * Bar(void){
     //Bar

Quote:
}



Wed, 29 Sep 2004 03:24:31 GMT  
 Declaring static function returning pointer to extern function

Quote:

> I want to declare a function that has static linkage and returns a
> pointer to a function with extern linkage.  

Linkage is not part of a type.  Think about it for a second: do
you declare a `pointer to int with external linkage' or a
`pointer to int with internal linkage'?  No, and the same is true
for function pointer types.


Wed, 29 Sep 2004 03:31:18 GMT  
 Declaring static function returning pointer to extern function

Quote:
> I want to declare a function that has static linkage and returns a
> pointer to a function with extern linkage.  Do the following
> declarations acheive that, or do they declare a function that returns
> a pointer to a function with static linkage?

> typedef void Foo(void);
> static Foo * Bar(void){
>      //Bar
> }

Not pretty but it works...

#include <stdio.h>

struct FuncChoice
{
    void (*pFunc)(void);

Quote:
};

int fooVar;
int barVar;

void foo(void)
{
    fooVar = 1000;

Quote:
}

void bar(void)
{
    barVar = 2000;

Quote:
}

static struct FuncChoice makeItFoo(void)
{
    struct FuncChoice temp = { foo };
    return temp;

Quote:
}

static struct FuncChoice  makeItBar(void)
{
    struct FuncChoice temp = { bar };
    return temp;

Quote:
}

int main(void)
{
    struct FuncChoice choice;

    choice = makeItFoo();
    choice.pFunc();

    choice = makeItBar();
    choice.pFunc();

    printf("fooVar = %d, barVar = %d\n", fooVar, barVar);

    return 0;

Quote:
}



Wed, 29 Sep 2004 03:56:24 GMT  
 Declaring static function returning pointer to extern function
Hi Matt,

Quote:
> I want to declare a function that has static linkage and returns a
> pointer to a function with extern linkage.  Do the following

Your given example will work fine.

Directives affecting the scope of a function (or any other symbol, for that
matter) only come into play in statements that access the function by name
(e.g. pFcn = printf;). The compiler has no way of storing such directives in
a pointer to said function, and will make no effort to determine them if the
function is accessed through said function pointer. Thus, a function that
returns a pointer to a function with static linkage can not expect the
compiler to enforce said static linkage. In fact it should expect otherwise:
that the referenced function should be callable by anyone who happens to
have the address of it.

------------
Timothy J. Smith
Member, Technical Staff
Broadband Storage Inc.

(949) 737-2743
------------



Sun, 03 Oct 2004 07:14:57 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Pointer to function returning pointer to function returning...

2. How to declare a function returning const pointer

3. declaring functions returning pointers

4. Question about signal()/pointers to functions that return pointers to functions

5. How to use (pointer to function), and function and pointer to (pointer to function)

6. Declaring a function extern?

7. Why declare returned pointers static?

8. Getting pointer to non-static member function from C callback function

9. Function returning a function-pointer - how to prototype ?

10. About function that returns function pointer

11. Function returning pointers to functions (repost)

12. Function returning pointers to functions

 

 
Powered by phpBB® Forum Software