Variable argument function as a parameter of a variable argument function

A

AikidoGuy

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!
 
A

Ark

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

Eric Sosman

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?
 
K

Keith Thompson

AikidoGuy said:
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?
 
A

AikidoGuy

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 :)
 
K

Keith Thompson

AikidoGuy said:
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>
 
A

AikidoGuy

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.
 
J

James Kuyper

On 11/20/2011 07:05 PM, AikidoGuy wrote:
[You should identify who you're responding to]
....
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.
 
A

AikidoGuy

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! :)
 
J

James Kuyper

This is becoming very philosophical.

It's not philosophy; it's hard practical advice about effective
techniques for asking questions.
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.
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.
 
S

Seebs

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top