1

In a header file a definition is given by

static void (*foo[CONST]) (void);

What is its meaning ?

Alexander Stolz
  • 7,454
  • 12
  • 57
  • 64
bubble
  • 3,408
  • 5
  • 29
  • 51
  • 3
    this article is quite useful to understand how to read c declarations http://www.antlr.org/wiki/display/CS652/How+To+Read+C+Declarations – Eineki Feb 20 '12 at 12:56
  • If this was found in a header file, it strongly suggests that the program design is seriously flawed in some manner. The static keyword means that the variable can only be accessed from inside the very same file, in this case the h-file. In 99% of all cases, it doesn't make sense to define variables in h-files, nor does it make sense to implement function definitions there either. – Lundin Feb 20 '12 at 13:03
  • Read the chapter function pointer http://stackoverflow.com/questions/252748/how-to-use-array-of-function-pointers – Rasmi Ranjan Nayak Feb 20 '12 at 13:17
  • @Lundin My understanding of header files says that when we include a headerfile in a source file its effect is as if all the contents of header file gets copied to the place where that header file was included. That would mean if I put static before any identifier in header file it will create a private copy of the identifier for the source file. Please comment if I am correct – bubble Feb 20 '12 at 15:13
  • @bubble: you're right, Lundin is mistaken. An object at file scope with the `static` keyword means there's a different copy of that object for each translation unit containing the declaration. That is to say, a different copy for each source file that includes the header. – Steve Jessop Feb 20 '12 at 15:15
  • @bubble Yeah I was wrong, apparently it is allowed and works as you describe. _However_, the important part of my comment is not what the compiler will do with the static, but rather about proper OO program design. `static` in this context means "things that are definitely private", while the contents of the header are "things that are definitely public". It does not make sense to expose static variables outside the current code module. Nor does it make sense to allocate anything inside a header file. Whether the language and/or the compiler allows it or not, is of peripheral interest. – Lundin Feb 20 '12 at 15:40

3 Answers3

7

It's meaning is an array with CONST amount of elements of function pointers with signature void f(void);

These things get used most often for callbacks, for example the function atexit.

orlp
  • 112,504
  • 36
  • 218
  • 315
  • 1
    @KennyTM: actually, `static` here indicates internal linkage if the declaration is at file scope, and static storage if the declaration is in function scope. So yes, it does have static storage either way, but given that it's "in a header file", that's probably not because of the `static` keyword :-) – Steve Jessop Feb 20 '12 at 12:54
  • About `static` keyword in C, it's used to hide the function from other compiled files during link edition. – sinsedrix Feb 20 '12 at 13:03
1

Find the leftmost identifier and work your way out, remembering that () and [] bind before * (*ap[] is an array of pointers, (*pa)[] is a pointer to an array, *fp() is a function returning a pointer, (*pf)() is a pointer to a function):

              foo                  -- foo
              foo[CONST]           -- is a CONST-element array
             *foo[CONST]           -- of pointers
            (*foo[CONST])(    )    -- to functions
            (*foo[CONST])(void)    -- taking no parameters
       void (*foo[CONST])(void)    -- and returning void
static void (*foo[CONST])(void)    -- and foo has static extent
                                   -- meaning it is not accessible by name
                                   -- outside of the current translation unit (file)
John Bode
  • 119,563
  • 19
  • 122
  • 198
0

Read the chapter on function pointer..

How can I use an array of function pointers?

Community
  • 1
  • 1
Rasmi Ranjan Nayak
  • 11,510
  • 29
  • 82
  • 122