Thread: why the code can compile -- about function pointer

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    1,579

    why the code can compile -- about function pointer

    Hello everyone,


    I am learning code from others, but I do not know why the following code section can compile?

    Why assignment p = new (void (*[3])()) is ok? Are the left side and right side of assignment having compatible type?

    Code:
    void (**p)();
    
    int main()
    {
    	p = new (void (*[3])());
    	return 0;
    }

    thanks in advance,
    George

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    p is defined as having type pointer-to-pointer-to-F, where F is function-returning-void-with-no-arguments.

    void (*[3])() is an array of pointer-to-F; new will return a pointer to the first element, which is therefore of type pointer-to-pointer-to-F.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I suppose typedefs can help a lot to clear up things:
    Code:
        typedef void(*func)();
        func* p = new func[3];
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    May 2006
    Posts
    1,579
    Thanks tabstop,


    Your reply is clear.

    The code,

    Code:
    	new void (*[3])();
    will create an array of 3 function pointers, and the function pointer points to functions whose prototype is -- return value void and empty parameter list?

    Quote Originally Posted by tabstop View Post
    p is defined as having type pointer-to-pointer-to-F, where F is function-returning-void-with-no-arguments.

    void (*[3])() is an array of pointer-to-F; new will return a pointer to the first element, which is therefore of type pointer-to-pointer-to-F.
    Thanks anon,


    I agree. If written in that way, I will have no question. :-)

    Quote Originally Posted by anon View Post
    I suppose typedefs can help a lot to clear up things:
    Code:
        typedef void(*func)();
        func* p = new func[3];

    have a good weekend,
    George

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  2. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  3. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM
  4. I need help to compile this code...
    By wise_ron in forum C Programming
    Replies: 17
    Last Post: 05-07-2006, 12:22 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM