Variable argument function as a parameter of a variable argument function

Discussion in 'C Programming' started by AikidoGuy, Nov 20, 2011.

  1. AikidoGuy

    AikidoGuy Guest

    Hi everyone,

    I've done a little searching, but I can't quite find an answer to my
    question. Everything I've found is more related to how to construct a
    variable argument function, and that's not my question.

    Suppose I have a variable argument function. Is it possible to use it
    to initialize a parameter of another variable argument function?

    For example, printf("%s", varArgFcnReturningAString(a,b,c));

    I ask because I'm not sure if this is supported by the standard and/or
    could be compiler specific.

    Thank you for any help, tips, and suggestions!
     
    AikidoGuy, Nov 20, 2011
    #1
    1. Advertising

  2. AikidoGuy

    Ark Guest

    Re: Variable argument function as a parameter of a variable argumentfunction

    On 11/20/2011 2:02 AM, AikidoGuy wrote:
    > Hi everyone,
    >
    > I've done a little searching, but I can't quite find an answer to my
    > question. Everything I've found is more related to how to construct a
    > variable argument function, and that's not my question.
    >
    > Suppose I have a variable argument function. Is it possible to use it
    > to initialize a parameter of another variable argument function?
    >
    > For example, printf("%s", varArgFcnReturningAString(a,b,c));
    >
    > I ask because I'm not sure if this is supported by the standard and/or
    > could be compiler specific.
    >
    > Thank you for any help, tips, and suggestions!


    Not sure what you're unsure about.
    Before a function call (printf in this case), all arguments are
    evaluated; think of this as varArgFcnReturningAString(a,b,c) replaced
    with a pointer value it returned.
    What is different here between a variadic function and a "normal" one?
    You can even log printf's return code:
    mylog(printf("%s", varArgFcnReturningAString(a,b,c))); // :)
     
    Ark, Nov 20, 2011
    #2
    1. Advertising

  3. AikidoGuy

    Eric Sosman Guest

    Re: Variable argument function as a parameter of a variable argumentfunction

    On 11/20/2011 2:02 AM, AikidoGuy wrote:
    > Hi everyone,
    >
    > I've done a little searching, but I can't quite find an answer to my
    > question. [...]


    Your question is number 15.12 on the comp.lang.c Frequently
    Asked Questions (FAQ) page at <http://www.c-faq.com/>.

    --
    Eric Sosman
    d
     
    Eric Sosman, Nov 20, 2011
    #3
  4. AikidoGuy

    Eric Sosman Guest

    Re: Variable argument function as a parameter of a variable argumentfunction

    On 11/20/2011 8:07 AM, Eric Sosman wrote:
    > On 11/20/2011 2:02 AM, AikidoGuy wrote:
    >> Hi everyone,
    >>
    >> I've done a little searching, but I can't quite find an answer to my
    >> question. [...]

    >
    > Your question is number 15.12 on the comp.lang.c Frequently
    > Asked Questions (FAQ) page at <http://www.c-faq.com/>.


    ... or maybe not. On re-reading your question, I'm less sure
    that I understand what you're asking. If the FAQ's answer is not
    what you were looking for, could you please elaborate?

    --
    Eric Sosman
    d
     
    Eric Sosman, Nov 20, 2011
    #4
  5. AikidoGuy <> writes:
    > I've done a little searching, but I can't quite find an answer to my
    > question. Everything I've found is more related to how to construct a
    > variable argument function, and that's not my question.
    >
    > Suppose I have a variable argument function. Is it possible to use it
    > to initialize a parameter of another variable argument function?
    >
    > For example, printf("%s", varArgFcnReturningAString(a,b,c));
    >
    > I ask because I'm not sure if this is supported by the standard and/or
    > could be compiler specific.


    varArgFcnReturningAString(a,b,c) is (presumably) and expression of
    type char*. printf("%s", blah) requires an expression of type char*.
    What problem do you imagine there might be? Why should a call to
    a variadic function in this context be treated any differently than
    a call to a non-variadic function?

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 20, 2011
    #5
  6. AikidoGuy

    AikidoGuy Guest

    Indeed, FAQ 15.12 was not my question... but Pete's response has
    confirmed that my original question is possible. I was also
    considering to construct such an example, but with a new child, it is
    difficult to find time. Thanks for your help Pete!

    The motivation behind the original question was because my code was
    not working as expected and I thought that it might be due to the fact
    that maybe a variadic function inside another variadic function could
    be the cause of my problem. I thought to myself that perhaps a
    compiler could get confused (somehow extract the wrong elements off
    the stack for the internal variadic function ...). I see that my
    thoughts were incorrect. So, I have looked again at my code that
    prompted my original question and I found that I had a subtle bug in
    there... oops :(

    Thanks everyone for your answers and suggestions! Much appreciated :)
     
    AikidoGuy, Nov 20, 2011
    #6
  7. AikidoGuy <> writes:
    > Indeed, FAQ 15.12 was not my question... but Pete's response has
    > confirmed that my original question is possible. I was also
    > considering to construct such an example, but with a new child, it is
    > difficult to find time. Thanks for your help Pete!
    >
    > The motivation behind the original question was because my code was
    > not working as expected and I thought that it might be due to the fact
    > that maybe a variadic function inside another variadic function could
    > be the cause of my problem. I thought to myself that perhaps a
    > compiler could get confused (somehow extract the wrong elements off
    > the stack for the internal variadic function ...). I see that my
    > thoughts were incorrect. So, I have looked again at my code that
    > prompted my original question and I found that I had a subtle bug in
    > there... oops :(
    >
    > Thanks everyone for your answers and suggestions! Much appreciated :)


    You've illustrated why it's a good idea to ask about the actual problem
    you're having, rather than making an assumption about what caused the
    problem and asking about that.

    It's known as an "XY problem": "You want to do X, and you think Y is the
    best way of doing so. Instead of asking about X, you ask about Y."
    <http://www.perlmonks.org/?node_id=542341>

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Nov 20, 2011
    #7
  8. AikidoGuy

    AikidoGuy Guest

    > You've illustrated why it's a good idea to ask about the actual problem
    > you're having, rather than making an assumption about what caused the
    > problem and asking about that.


    Sorry... have to disagree. I was asking about a simplified version of
    my
    problem in order to eliminate that possibility. It is a parsimonious
    approach.
    And, in fact, if I had provided my original problem, then I would have
    had
    to also provide a great deal more explanation, which I did not think
    appropriate.

    > It's known as an "XY problem": "You want to do X, and you think Y is the
    > best way of doing so. Instead of asking about X, you ask about Y."


    I did have an XY problem, but I wanted to do X and was checking to see
    if
    Y was the problem. I found out that it was not the problem and then
    was
    able to determine for myself how to solve X. I see nothing wrong with
    this
    approach. I believe it reduces what I need to explain to the group
    since
    I asked my simpler issue first.
     
    AikidoGuy, Nov 21, 2011
    #8
  9. AikidoGuy

    James Kuyper Guest

    Re: Variable argument function as a parameter of a variable argumentfunction

    On 11/20/2011 07:05 PM, AikidoGuy wrote:
    [You should identify who you're responding to]
    ....
    >> It's known as an "XY problem": "You want to do X, and you think Y is the
    >> best way of doing so. Instead of asking about X, you ask about Y."

    >
    > I did have an XY problem, but I wanted to do X and was checking to see
    > if
    > Y was the problem. I found out that it was not the problem and then
    > was
    > able to determine for myself how to solve X. I see nothing wrong with
    > this
    > approach. I believe it reduces what I need to explain to the group
    > since
    > I asked my simpler issue first.


    The fundamental problem with taking that approach is that you're
    assuming that you'll be able to solve X, if Y turns out not to be the
    right solution.

    If you know enough about X to justify that assumption, you should first
    try solving X on the assumption that Y is not the right solution; only
    ask about Y if not-Y leads you to a dead-end.

    If you do not know enough about X to justify that assumption, as is by
    far the most common case, asking about Y could end up drawing you into a
    long, drawn-out discussion of Y that does nothing to solve your real
    problem. In my experience on this newsgroup, such useless discussions
    are by far the most common result of the "XY" approach.
    --
    James Kuyper
     
    James Kuyper, Nov 21, 2011
    #9
  10. AikidoGuy

    AikidoGuy Guest

    > > I asked my simpler issue first.
    >
    > The fundamental problem with taking that approach is that you're
    > assuming that you'll be able to solve X, if Y turns out not to be the
    > right solution.


    This is becoming very philosophical.

    > If you know enough about X to justify that assumption, you should first
    > try solving X on the assumption that Y is not the right solution; only
    > ask about Y if not-Y leads you to a dead-end.


    My question to the group was about Y. I obtained answers about Y.
    To question whether or not I am able to solve X is to question my
    ability and not to answer my question.

    > If you do not know enough about X to justify that assumption, as is by
    > far the most common case, asking about Y could end up drawing you into a
    > long, drawn-out discussion of Y that does nothing to solve your real
    > problem. In my experience on this newsgroup, such useless discussions
    > are by far the most common result of the "XY" approach.


    Generalities are certainly nice things to state. And I am not up to
    date on how others discuss their problems. So I apologize for my
    inappropriate behaviour on this newsgroup.

    I focused my original question to something concrete and specific
    about Y. And I was able to obtain very helpful advice from everyone
    in this newsgroup. I appreciate the comments since I have learned
    something in the process. THANKS! :)
     
    AikidoGuy, Nov 21, 2011
    #10
  11. AikidoGuy

    James Kuyper Guest

    Re: Variable argument function as a parameter of a variable argumentfunction

    On 11/20/2011 11:18 PM, AikidoGuy wrote:
    >>> I asked my simpler issue first.

    >>
    >> The fundamental problem with taking that approach is that you're
    >> assuming that you'll be able to solve X, if Y turns out not to be the
    >> right solution.

    >
    > This is becoming very philosophical.


    It's not philosophy; it's hard practical advice about effective
    techniques for asking questions.

    >> If you know enough about X to justify that assumption, you should first
    >> try solving X on the assumption that Y is not the right solution; only
    >> ask about Y if not-Y leads you to a dead-end.

    >
    > My question to the group was about Y. I obtained answers about Y.
    > To question whether or not I am able to solve X is to question my
    > ability and not to answer my question.


    You shouldn't be thin-skinned about that. If you need to ask any
    question at all, that implies that there's some things you don't know
    about the topic of your question. There's nothing unusual about that, we
    all have gaps in our knowledge, just gaps of different sizes and in
    different locations. The more elementary the question, the more it
    implies that you don't know. This particular question implied a fairly
    basic misunderstanding about how functions worked, justifying a
    suspicion that there's a fair bit you don't know about C.

    It's quite common for people to ask the wrong question; questioning them
    to determine what the right question should have been is more helpful
    than answering the wrong question that they asked. The particular
    question you asked was so bizarre as to strongly justify a suspicion
    that this might be one of those cases.

    In particular, there's one key disadvantage to your approach that I
    didn't address in my earlier comments. What if Y did "solve" your
    problem, but was far from being the best solution? I've frequently seen
    questions about extremely complicated and elaborate "solutions", that
    could, when the questions had been answered, do what they were intended
    to do, but were extremely unlikely to be the best solution to whatever
    the real problem was.

    I've found that the people who ask these questions are almost never
    willing to describe the real problem that they're trying to solve, which
    makes it difficult to prove that they asked the wrong question. I
    suspect that they're embarrassed about something that they would have to
    explain - perhaps it's something illegal, or maybe just that they don't
    want people to see how bad their coding skills are. A few have claimed
    that they had security issues to worry about, which sounds fairly
    legitimate - but almost any code that poses security issues can be
    rewritten to present the same coding problem without violating security.

    >> If you do not know enough about X to justify that assumption, as is by
    >> far the most common case, asking about Y could end up drawing you into a
    >> long, drawn-out discussion of Y that does nothing to solve your real
    >> problem. In my experience on this newsgroup, such useless discussions
    >> are by far the most common result of the "XY" approach.

    >
    > Generalities are certainly nice things to state. And I am not up to
    > date on how others discuss their problems. So I apologize for my
    > inappropriate behaviour on this newsgroup.


    It's not inappropriate behavior; it's an unproductive approach to asking
    for help.
    --
    James Kuyper
     
    James Kuyper, Nov 21, 2011
    #11
  12. AikidoGuy

    Seebs Guest

    Re: Variable argument function as a parameter of a variableargument function

    On 2011-11-21, James Kuyper <> wrote:
    > It's quite common for people to ask the wrong question; questioning them
    > to determine what the right question should have been is more helpful
    > than answering the wrong question that they asked.


    There is a reason for this.

    I start with a problem "A". I try to solve it myself, because I know that
    if I can solve it, I am better off doing so than waiting on help. I come up
    with a proposed solution, "B". I can't make it work. It is time to go for
    help.

    .... At which point, I'm *thinking about* B. So I ask for help with B. But
    B isn't really my problem; A is.

    > In particular, there's one key disadvantage to your approach that I
    > didn't address in my earlier comments. What if Y did "solve" your
    > problem, but was far from being the best solution?


    And note that if anyone thinks your problem is homework, this is fairly
    likely to happen. I love to offer "helpful" solutions to homework. It
    entertains me.

    -s
    --
    Copyright 2011, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    I am not speaking for my employer, although they do rent some of my opinions.
     
    Seebs, Nov 21, 2011
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ben Kial
    Replies:
    1
    Views:
    678
    Eric Enright
    Nov 15, 2004
  2. S?ren Gammelmark
    Replies:
    1
    Views:
    1,930
    Eric Sosman
    Jan 7, 2005
  3. Reckoner
    Replies:
    11
    Views:
    718
    Steven D'Aprano
    Jan 19, 2009
  4. Mister B
    Replies:
    8
    Views:
    570
    Nick Keighley
    Aug 26, 2010
  5. AzamSharp
    Replies:
    2
    Views:
    187
Loading...

Share This Page