C aptitude

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • aarklon@gmail.com

    C aptitude

    Hi all,

    I was going through the book "Test your skills in C" by Thamarai selvi
    and R murugesan, publisheb by Tata Mcgraw-Hill,
    ISBN 0-07-044759-4 , the following are from this book,
    now my question is to what extent the following are true..???

    1) a function with no action is allowed and is known as dummy function

    2) (int(*(*)[3])(char*))ptr is the correct way to cast ptr to a
    pointer to a 3 element array of function pointers with a return value
    of int and a single parameter of a pointer to char



    3)how could stderr be re directed from within a program to force all
    error messages to be written to the end of the text file "error.log"
    instead of location specified by the operating system

    a) stderr = fopen("error.lo g","w");
    b) freopen("error. log","a",stderr );
    c) stderr = freopen(stderr, "a","error.log" );
    d) stderr = fopen("error.lo g","a");

    the answer is given as d option

    4) #include<stdlib .h>
    #include <stdio.h>
    char *format="%d";

    void func();

    int main(void)
    {
    int x;
    func(scanf,&x);
    printf("%d\n",x );

    return EXIT_SUCCESS;
    }

    referring to the sample code given above, which of the following
    would be correct implementation
    for func ?

    a) void func( int (*y)(const char*,...),int* x)
    { (*y)(format,x); }

    b) void func( int (*y)(const char*,...),int* x)
    { (*y)(format,&x) ; }

    answer is given as a option
  • santosh

    #2
    Re: C aptitude

    aarklon@gmail.c om wrote:
    Hi all,
    >
    I was going through the book "Test your skills in C" by Thamarai selvi
    and R murugesan, publisheb by Tata Mcgraw-Hill,
    ISBN 0-07-044759-4 , the following are from this book,
    now my question is to what extent the following are true..???
    >
    1) a function with no action is allowed and is known as dummy function
    True. Sometimes they are also called as stub functions.
    2) (int(*(*)[3])(char*))ptr is the correct way to cast ptr to a
    pointer to a 3 element array of function pointers with a return value
    of int and a single parameter of a pointer to char
    I think the following should do it:

    (int(*)[3](char *))ptr
    3)how could stderr be re directed from within a program to force all
    error messages to be written to the end of the text file "error.log"
    instead of location specified by the operating system
    >
    a) stderr = fopen("error.lo g","w");
    b) freopen("error. log","a",stderr );
    c) stderr = freopen(stderr, "a","error.log" );
    d) stderr = fopen("error.lo g","a");
    >
    the answer is given as d option
    Wrong. The correct option is (b). The pre-defined streams need not be
    lvalues.
    4) #include<stdlib .h>
    #include <stdio.h>
    char *format="%d";
    >
    void func();
    >
    int main(void)
    {
    int x;
    func(scanf,&x);
    printf("%d\n",x );
    >
    return EXIT_SUCCESS;
    }
    >
    referring to the sample code given above, which of the following
    would be correct implementation
    for func ?
    >
    a) void func( int (*y)(const char*,...),int* x)
    { (*y)(format,x); }
    >
    b) void func( int (*y)(const char*,...),int* x)
    { (*y)(format,&x) ; }
    >
    answer is given as a option
    Yes. That's right. Note though that the call could as well have been:

    y(format, x);

    or

    (****y)(format, x);

    Comment

    • Flash Gordon

      #3
      Re: C aptitude

      aarklon@gmail.c om wrote, On 25/04/08 19:42:
      Hi all,
      >
      I was going through the book "Test your skills in C" by Thamarai selvi
      and R murugesan, publisheb by Tata Mcgraw-Hill,
      ISBN 0-07-044759-4 , the following are from this book,
      now my question is to what extent the following are true..???
      >
      1) a function with no action is allowed and is known as dummy function
      It is true that it is allowed. It is also true that such functions are
      often known (by me at least) as dummy functions. However, whether they
      are called dummy functions is nothing to do with C.
      2) (int(*(*)[3])(char*))ptr is the correct way to cast ptr to a
      pointer to a 3 element array of function pointers with a return value
      of int and a single parameter of a pointer to char
      I can't think of a good reason for doing such a thing. If there ever was
      a good reason I would not do it like that (and would reject anything
      like that in a code review) since it can be made far clearer by using a
      typedef for the function type (or function pointer type if you prefer).
      3)how could stderr be re directed from within a program to force all
      error messages to be written to the end of the text file "error.log"
      instead of location specified by the operating system
      >
      a) stderr = fopen("error.lo g","w");
      b) freopen("error. log","a",stderr );
      c) stderr = freopen(stderr, "a","error.log" );
      d) stderr = fopen("error.lo g","a");
      >
      the answer is given as d option
      stderr need not be a modifiable lvalue. I.e. you may not be able to do
      the assignment in d. According to the standard the primary use of
      freopen is to change the file associated with stdin/out/err for this
      very reason! So that leave b as the only possibility, so if the book
      really does say d it is wrong.
      4) #include<stdlib .h>
      #include <stdio.h>
      char *format="%d";
      >
      void func();
      >
      int main(void)
      {
      int x;
      func(scanf,&x);
      printf("%d\n",x );
      >
      return EXIT_SUCCESS;
      }
      >
      referring to the sample code given above, which of the following
      would be correct implementation
      for func ?
      >
      a) void func( int (*y)(const char*,...),int* x)
      { (*y)(format,x); }
      >
      b) void func( int (*y)(const char*,...),int* x)
      { (*y)(format,&x) ; }
      >
      answer is given as a option
      Of the two, a is the only possible valid answer (the correct answer
      depends on the version of the C standard in use). However, I don't like
      the code anyway :-)
      --
      Flash Gordon

      Comment

      • Barry Schwarz

        #4
        Re: C aptitude

        On Sat, 26 Apr 2008 02:53:29 +0530, santosh <santosh.k83@gm ail.com>
        wrote:
        >aarklon@gmail. com wrote:
        >
        snip
        >2) (int(*(*)[3])(char*))ptr is the correct way to cast ptr to a
        >pointer to a 3 element array of function pointers with a return value
        >of int and a single parameter of a pointer to char
        >
        >I think the following should do it:
        >
        (int(*)[3](char *))ptr
        Nope. The original is correct.



        Remove del for email

        Comment

        • Malcolm McLean

          #5
          Re: C aptitude


          "Flash Gordon" <spam@flash-gordon.me.ukwro te in message
          >
          >1) a function with no action is allowed and is known as dummy function
          >
          It is true that it is allowed. It is also true that such functions are
          often known (by me at least) as dummy functions. However, whether they are
          called dummy functions is nothing to do with C.
          >
          You mean the standard doesn't define anything known as the "dummy" function.
          However it is implicit in that

          void dummy(void)
          {
          }

          is allowed by C rules of grammar. It wasn't a necessary decision to allow
          functions with no code in the body at all.

          Dummy functions differ from stub functions. A dummy function does nothing, a
          stub function returns valid but trival data. For instance if you wanted to
          read a bar code, but had no barcode reader, you might write a stub function

          int readbarcode(cha r *code)
          {
          strcpy(code, "12345678901234 5");
          return 0;
          }

          as a placeholder until the real reader arrives.

          Both are perfectly valid C programming concepts.

          --
          Free games and programming goodies.


          Comment

          • Flash Gordon

            #6
            Re: C aptitude

            Malcolm McLean wrote, On 26/04/08 11:13:
            >
            "Flash Gordon" <spam@flash-gordon.me.ukwro te in message
            >>
            >>1) a function with no action is allowed and is known as dummy function
            >>
            >It is true that it is allowed. It is also true that such functions are
            >often known (by me at least) as dummy functions. However, whether they
            >are called dummy functions is nothing to do with C.
            >>
            You mean the standard doesn't define anything known as the "dummy"
            function. However it is implicit in that
            >
            void dummy(void)
            {
            }
            >
            is allowed by C rules of grammar. It wasn't a necessary decision to
            allow functions with no code in the body at all.
            Yes, and I stated that a function "with no action" is allowed. In fact,
            nothing you say above contradicts anything I said and all it adds is an
            example.
            Dummy functions differ from stub functions. A dummy function does
            nothing, a stub function returns valid but trival data. For instance if
            <snip>

            Not always. However, it has nothing to do with the question.
            Both are perfectly valid C programming concepts.
            I stated that the one that was asked about was valid. I said nothing
            about the other for the same reason I said nothing about FFTs, the
            reason being it was not what was being asked.
            --
            Flash Gordon

            Comment

            Working...