Help me to understand

This is a discussion on Help me to understand within the C Programming forums, part of the General Programming Boards category; can sumone help in understanding this piece of code- Code: main() { int (*functable[2])(char *format, ...) ={printf, scanf}; int i ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    46

    Help me to understand

    can sumone help in understanding this piece of code-
    Code:
    main()
    {
          int (*functable[2])(char *format, ...) ={printf, scanf};
          int i = 100;
          (*functable[0])("%d", i);
          (*functable[1])("%d", i);
          (*functable[1])("%d", i);
          (*functable[0])("%d", &i);
    }
    it gives error while compilation

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Code:
    main()
    {
          // Create an array of function pointers.  Index 0 points to printf, index 1 points to scanf
          int (*functable[2])(char *format, ...) ={printf, scanf};
    
          int i = 100;
    
          // Equivalent to: printf("%d", i);
          (*functable[0])("%d", i);
    
          // Equivalent to: scanf("%d", i);
          (*functable[1])("%d", i);
    
          // Equivalent to: scanf("%d", i);
          (*functable[1])("%d", i);
    
          // Equivalent to: printf("%d", &i);
          (*functable[0])("%d", &i);
    }
    Note that the 2 scanf calls are incorrect since scanf should take a pointer as a parameter.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Also I get a warning about the initialization (you might too) since that isn't how printf and scanf are prototyped. (It's const char *format).

  4. #4
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    951
    had no idea that you could create pointers to functions. mind == blown.

  5. #5
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by Epy View Post
    had no idea that you could create pointers to functions. mind == blown.
    So now it looks like you're ready for qsort.

  6. #6
    Epy
    Epy is offline
    Fortran lover Epy's Avatar
    Join Date
    Sep 2009
    Location
    California, USA
    Posts
    951
    Quote Originally Posted by tabstop View Post
    So now it looks like you're ready for qsort.
    Wow, well now I know what vl-sort is a wrapper of (in AutoLISP).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 21
    Last Post: 10-14-2006, 04:38 AM
  2. Need software to help to understand C source code
    By Kincider in forum C++ Programming
    Replies: 1
    Last Post: 09-28-2006, 09:44 PM
  3. Replies: 13
    Last Post: 08-24-2006, 12:22 AM
  4. Need help to understand x["AC"] in the following code
    By jcourcel in forum C Programming
    Replies: 11
    Last Post: 06-06-2006, 01:13 AM
  5. I don't understand K&R example
    By rllovera in forum C Programming
    Replies: 8
    Last Post: 10-25-2004, 10:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21