function definition

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Tinkertim

    #16
    Re: function definition

    On Oct 1, 3:02 pm, Keith Thompson <ks...@mib.orgw rote:
    A format *string* is just one of many possible ways to specify the
    arguments. Here's a rather contrived example:
    Thanks, Keith! :) I've learned something new today. With a splash of
    sanity, that is entirely convenient for corner cases.

    Cheers,
    --Tim


    Comment

    • Nick Keighley

      #17
      Re: function definition

      On 1 Oct, 07:53, Nick Keighley <nick_keighley_ nos...@hotmail. com>
      wrote:
      On 1 Oct, 07:22, Tinkertim <tinker...@gmai l.comwrote:
      On Oct 1, 1:01 pm, Keith Thompson <ks...@mib.orgw rote:
      The point is that a printf-style format string isn't the only way to
      determine the number and type of arguments [in a varargs function]
      that are supposed to be
      passed (though I think it's the only method used by any of the
      functions in the standard library).
      >
      yes
      <pedantic>
      there's scanf() formats too
      </pedantic>

      --
      Nick Keighley

      Comment

      • James Kuyper

        #18
        Re: function definition

        Tinkertim wrote:
        On Oct 1, 1:01 pm, Keith Thompson <ks...@mib.orgw rote:
        ....
        >For example, I've seen variadic
        >functions that take a series of char* arguments, with the end of the
        >list marked by a (char*)NULL argument.
        >
        In that example, the function assumes all arguments to be char * type,
        yes? You know your done when you hit a null argument. I'm not sure how
        you could mix types without passing some kind of definition of the
        incoming format.
        >
        I wasn't saying that a format was needed in all cases, just in cases
        where arguments are of mixed types .. or is that what you were saying
        that you've seen done? Sorry if I'm a little dense today :)
        There's many other ways to handle mixed argument types. For instance,
        in the IMSL C library, variable arguments are passed in groups. Each
        group starts with an enumeration constant that identifies the type of
        the group. Since the enumeration constant always has a type of 'int',
        the function knows how to parse it. Then, knowing the group type, the
        library function knows the precise type of each of the remaining members
        of the argument group. A code value of 0 marks the end of the argument list.

        The only general statement that you can make is that, knowing the values
        of each of the first n arguments, the variadic function must have enough
        information to determine whether there is another argument, and if so,
        what type it has.

        Comment

        • Joachim Schmitz

          #19
          Re: function definition

          Keith Thompson wrote:
          Tinkertim <tinkertim@gmai l.comwrites:
          >On Sep 30, 11:19 pm, Nick Keighley <nick_keighley_ nos...@hotmail. com>
          >wrote:
          >>>In very lay terms, "..." means arguments that complement format.
          >>>
          >>if I understood this I'm sure I'd disagree. Ellipsis (...) means
          >>the number and type of arguments is not specified. It allows you
          >>to pass a variable number of arguments with number specified
          >>(somehow), possibly by the fixed arguments.
          >>
          >That's what I meant by complement, sorry. The number and type of
          >arguments in the case above is determined by the format passed, where
          >in your example its determined by a simple integer. %s %d determines
          >two arguments of varying types, where as n = 5 determines five
          >arguments of type int.
          >>
          >Complement is probably a bad choice of wording, however it appeared
          >that the OP is not a native speaker so I tried to speak generally.
          >
          The point is that a printf-style format string isn't the only way to
          determine the number and type of arguments that are supposed to be
          passed (though I think it's the only method used by any of the
          functions in the standard library). For example, I've seen variadic
          functions that take a series of char* arguments, with the end of the
          list marked by a (char*)NULL argument.
          Another example is POSIX's open() which has an optional 3rd argument (mode_t
          mode), which is only needed (and looked at) depending on the value of the
          2nd arg (int oflag, needs to have O_CREAT set).

          Bye, Jojo


          Comment

          • Keith Thompson

            #20
            Re: function definition

            Nick Keighley <nick_keighley_ nospam@hotmail. comwrites:
            On 1 Oct, 07:53, Nick Keighley <nick_keighley_ nos...@hotmail. com>
            wrote:
            >On 1 Oct, 07:22, Tinkertim <tinker...@gmai l.comwrote:
            On Oct 1, 1:01 pm, Keith Thompson <ks...@mib.orgw rote:
            >
            The point is that a printf-style format string isn't the only way to
            determine the number and type of arguments [in a varargs function]
            that are supposed to be
            passed (though I think it's the only method used by any of the
            functions in the standard library).
            >>
            >yes
            >
            <pedantic>
            there's scanf() formats too
            </pedantic>
            Well, yes, but I assert that scanf's format strings are in the *style*
            of printf's format strings.

            --
            Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
            Nokia
            "We must do something. This is something. Therefore, we must do this."
            -- Antony Jay and Jonathan Lynn, "Yes Minister"

            Comment

            Working...