Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #13639 > unrolled thread

Variable argument function as a parameter of a variable argument function

Started byAikidoGuy <aikidoguy@gmail.com>
First post2011-11-19 23:02 -0800
Last post2011-11-21 22:43 +0000
Articles 13 — 7 participants

Back to article view | Back to comp.lang.c


Contents

  Variable argument function as a parameter of a variable argument function AikidoGuy <aikidoguy@gmail.com> - 2011-11-19 23:02 -0800
    Re: Variable argument function as a parameter of a variable argument function Ark <akhasin@macroexpressions.com> - 2011-11-20 03:14 -0500
    Re: Variable argument function as a parameter of a variable argument function Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-11-20 08:07 -0500
      Re: Variable argument function as a parameter of a variable argument function Eric Sosman <esosman@ieee-dot-org.invalid> - 2011-11-20 08:11 -0500
    Re: Variable argument function as a parameter of a variable argument function Keith Thompson <kst-u@mib.org> - 2011-11-20 08:19 -0800
    Re: Variable argument function as a parameter of a variable argument function pete <pfiland@mindspring.com> - 2011-11-20 13:33 -0500
      Re: Variable argument function as a parameter of a variable argument function AikidoGuy <aikidoguy@gmail.com> - 2011-11-20 15:29 -0800
        Re: Variable argument function as a parameter of a variable argument function Keith Thompson <kst-u@mib.org> - 2011-11-20 15:58 -0800
          Re: Variable argument function as a parameter of a variable argument function AikidoGuy <aikidoguy@gmail.com> - 2011-11-20 16:05 -0800
            Re: Variable argument function as a parameter of a variable argument function James Kuyper <jameskuyper@verizon.net> - 2011-11-20 20:45 -0500
              Re: Variable argument function as a parameter of a variable argument function AikidoGuy <aikidoguy@gmail.com> - 2011-11-20 20:18 -0800
                Re: Variable argument function as a parameter of a variable argument function James Kuyper <jameskuyper@verizon.net> - 2011-11-21 07:22 -0500
                  Re: Variable argument function as a parameter of a variable argument function Seebs <usenet-nospam@seebs.net> - 2011-11-21 22:43 +0000

#13639 — Variable argument function as a parameter of a variable argument function

FromAikidoGuy <aikidoguy@gmail.com>
Date2011-11-19 23:02 -0800
SubjectVariable argument function as a parameter of a variable argument function
Message-ID<00f0752d-f768-437a-87c5-0b3767c7411f@l24g2000yqm.googlegroups.com>
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!

[toc] | [next] | [standalone]


#13640

FromArk <akhasin@macroexpressions.com>
Date2011-11-20 03:14 -0500
Message-ID<jaactv$c0j$1@news.albasani.net>
In reply to#13639

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))); // :)

[toc] | [prev] | [next] | [standalone]


#13642

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2011-11-20 08:07 -0500
Message-ID<jaau2h$59h$1@dont-email.me>
In reply to#13639
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
esosman@ieee-dot-org.invalid

[toc] | [prev] | [next] | [standalone]


#13643

FromEric Sosman <esosman@ieee-dot-org.invalid>
Date2011-11-20 08:11 -0500
Message-ID<jaauap$59h$2@dont-email.me>
In reply to#13642
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
esosman@ieee-dot-org.invalid

[toc] | [prev] | [next] | [standalone]


#13645

FromKeith Thompson <kst-u@mib.org>
Date2011-11-20 08:19 -0800
Message-ID<lnobw6wyud.fsf@nuthaus.mib.org>
In reply to#13639
AikidoGuy <aikidoguy@gmail.com> 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) kst-u@mib.org  <http://www.ghoti.net/~kst>
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#13646

Frompete <pfiland@mindspring.com>
Date2011-11-20 13:33 -0500
Message-ID<4EC94804.4E9F@mindspring.com>
In reply to#13639
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!

/* BEGIN output from new.c */

Yes, it is possible.

/* END output from new.c */




/* BEGIN new.c */

#include <stdio.h>
#include <stdarg.h>

char *varArgFcnReturningAString(char *s, const char *format, ...)
{
    va_list arg;
    
    va_start(arg, format);
    vsprintf(s, format, arg);
    va_end(arg);
    return s;
}

int 
main(void)
{
    char *b = "%s";
    char c[] = "Yes, it is possible.";
    char a[sizeof c] = {0};

    puts("\n/* BEGIN output from new.c */\n");

    printf("%s", varArgFcnReturningAString(a, b, c));
    
    puts("\n\n/* END output from new.c */");
    return 0;
}



-- 
pete

[toc] | [prev] | [next] | [standalone]


#13666

FromAikidoGuy <aikidoguy@gmail.com>
Date2011-11-20 15:29 -0800
Message-ID<e6730a8e-3cb9-49a5-aaaa-c7a36945474d@o1g2000vbe.googlegroups.com>
In reply to#13646
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 :)

[toc] | [prev] | [next] | [standalone]


#13667

FromKeith Thompson <kst-u@mib.org>
Date2011-11-20 15:58 -0800
Message-ID<lnaa7qwdm2.fsf@nuthaus.mib.org>
In reply to#13666
AikidoGuy <aikidoguy@gmail.com> 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) kst-u@mib.org  <http://www.ghoti.net/~kst>
"We must do something.  This is something.  Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

[toc] | [prev] | [next] | [standalone]


#13668

FromAikidoGuy <aikidoguy@gmail.com>
Date2011-11-20 16:05 -0800
Message-ID<ab6d0b3a-83b3-4b48-a65b-b00fa41a97cb@g21g2000yqc.googlegroups.com>
In reply to#13667
> 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.

[toc] | [prev] | [next] | [standalone]


#13670

FromJames Kuyper <jameskuyper@verizon.net>
Date2011-11-20 20:45 -0500
Message-ID<jacafp$2a4$1@dont-email.me>
In reply to#13668
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

[toc] | [prev] | [next] | [standalone]


#13672

FromAikidoGuy <aikidoguy@gmail.com>
Date2011-11-20 20:18 -0800
Message-ID<6e6c6503-88f2-41eb-a5e2-d584ee8d477b@q16g2000yqn.googlegroups.com>
In reply to#13670
> > 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! :)

[toc] | [prev] | [next] | [standalone]


#13696

FromJames Kuyper <jameskuyper@verizon.net>
Date2011-11-21 07:22 -0500
Message-ID<jadfr4$n9u$1@dont-email.me>
In reply to#13672
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

[toc] | [prev] | [next] | [standalone]


#13752

FromSeebs <usenet-nospam@seebs.net>
Date2011-11-21 22:43 +0000
Message-ID<slrnjclepv.30df.usenet-nospam@guild.seebs.net>
In reply to#13696
On 2011-11-21, James Kuyper <jameskuyper@verizon.net> 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 / usenet-nospam@seebs.net
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.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.c


csiph-web