Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 89027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:39:08+00:00 2026-05-10T22:39:08+00:00

I need to declare an array of pointers to functions like so: extern void

  • 0

I need to declare an array of pointers to functions like so:

extern void function1(void); extern void function2(void); ...  void (*MESSAGE_HANDLERS[])(void) = {    function1,    function2,    ... }; 

However, I want the the array to be declared as constant — both the data in the array and the pointer to the data. Unfortunately, I do not recall where to place the const key-word(s).

I’m assuming the actual pointer, MESSAGE_HANDLERS in this case, is already constant because it is declared as an array. On the otherhand, couldn’t the function pointers within the array be change at runtime if it is declared as shown?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T22:39:09+00:00Added an answer on May 10, 2026 at 10:39 pm

    There is a technique to remember how to build such type. First try to read pointers starting from their name and read from right to left.

    How to declare that stuff without help?

    Arrays

    T t[5]; 

    is an array of 5 T. To make T a function type, you write the return-type to the left, and the parameters to the right:

    void t[5](void); 

    would be an array of 5 functions returning void and taking no parameters. But functions itself can’t be stuffed in arrays! They are not objects. Only pointers to them can.

    What about

    void * t[5](void); 

    That’s still wrong as it would just change the return-type to be a pointer to void. You have to use parentheses:

    void (*t[5])(void); 

    and this will actually work. t is an array of 5 pointers to functions returning void and taking no parameters.

    Great! What about an array of pointers to arras? That’s very similar. The element type appears at the left, and the dimension at the right. Again, parentheses are needed because otherwise the array would become a multidimensional array of integer pointers:

    int (*t[5])[3]; 

    That’s it! An array of 5 pointers to arrays of 3 int.

    What about functions?

    What we have just learned is true about functions too. Let’s declare a function taking an int that returns a pointer to another function taking no parameter and returning void:

    void (*f(int))(void); 

    we need parentheses again for he same reason as above. We could now call it, and call the returned function pointed to again.

    f(10)(); 

    Returning a pointer to function returning another pointer to function

    What about this?

    f(10)(true)(3.4); 

    ? In other words, how would a function taking int returning a pointer to a function taking bool returning a pointer to a function taking double and returning void would look like? The answer is that you just nest them:

    void (*(*f(int))(bool))(double); 

    You could do so endless times. Indeed, you can also return a pointer to an array just like you can a pointer to a function:

    int (*(*f(int))(bool))[3]; 

    This is a function taking int returning a pointer to a function taking bool returning a pointer to an array of 3 int

    What does it have to do with const?

    Now that the above explained how to build up complexer types from fundamental types, you can put const at places where you now know where they belong to. Just consider:

    T c * c * c ... * c name; 

    The T is the basic type that we end up pointing to at the end. The c stands for either const or not const. For example

    int const * const * name; 

    will declare name to have the type pointer to a constant pointer to a constant int. You can change name, but you cannot change *name, which would be of type

    int const * const 

    and neither **name, which would be of type

    int const 

    Let’s apply this to a function pointer of above:

    void (* const t[5])(void); 

    This would actually declare the array to contain constant pointers. So after creating (and initializing) the array, the pointers are const, because the const appeared after the star. Note that we cannot put a const before the star in this case, since there are no pointers to constant functions. Functions simply can’t be const as that would not make sense. So the following is not valid:

    void (const * t[5])(void); 

    Conclusion

    The C++ and C way of declaring functions and arrays actually is actually a bit confusing. You have to get your head around it first, but if you understand it, you can write very compact function declarations using it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 80k
  • Answers 81k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer How about this: Simulate a Windows Service using ASP.NET to… May 11, 2026 at 4:24 pm
  • Editorial Team
    Editorial Team added an answer I use the following instruction: :%y+ May 11, 2026 at 4:24 pm
  • Editorial Team
    Editorial Team added an answer Well it might all be too much trouble than it's… May 11, 2026 at 4:24 pm

Related Questions

I need a way to represent a 2-D array (a dense matrix) of doubles
Ok, so that title probably doesn't explain my question well. Hopefully this makes sense.
I've the following structure in C#: [StructLayoutAttribute(LayoutKind.Sequential)] public struct RECORD { public uint m1;
I have been given a header with the following declaration: //The index of 1

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.