943,516 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 9199
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 13th, 2007
0

variable arguments in C

Expand Post »
how can we pass variable arguments in any function of C?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hitesh_mathpal is offline Offline
4 posts
since Jul 2007
Jul 13th, 2007
0

Re: variable arguments in C

Administrator
Reputation Points: 6442
Solved Threads: 1393
Bad Cop
Narue is offline Offline
11,807 posts
since Sep 2004
Jul 13th, 2007
0

Re: variable arguments in C

I think this should work, but i didn't test it, please do it yourself.
  1. #include <stdarg.h>
  2.  
  3. void stringstofile (FILE *filehandle, ...)
  4. {
  5. va_list vargs;
  6.  
  7. va_start (vargs, filehandle);
  8. while (va_arg (vargs, char *))
  9. fputs (va_arg (vargs, char *), filehandle);
  10. va_end (vargs);
  11. }
Reputation Points: 85
Solved Threads: 13
Junior Poster
TkTkorrovi is offline Offline
170 posts
since Mar 2005
Jul 13th, 2007
0

Re: variable arguments in C

Google first....
Reputation Points: 21
Solved Threads: 7
Junior Poster in Training
ProgrammersTalk is offline Offline
83 posts
since Jun 2007
Jul 16th, 2007
0

Re: variable arguments in C

how can we pass variable arguments in any function of C?
Simply to answer your question.


  1. #include <stdio.h>
  2.  
  3. int sum;
  4.  
  5. int main() {
  6.  
  7. sum = myfunction(5, 10);
  8.  
  9. }
  10.  
  11. int myfunction(int argument1, int argument2) {
  12.  
  13. return argument1 + argument2;
  14.  
  15. }



Code not tested, but see no reason why it shouldn't work.


Cheers.
Reputation Points: 35
Solved Threads: 5
Junior Poster
dr4g is offline Offline
136 posts
since Apr 2007
Jul 16th, 2007
0

Re: variable arguments in C

Click to Expand / Collapse  Quote originally posted by dr4g ...
Code not tested, but see no reason why it shouldn't work.
Cheers.
Agree it should work but not the way the OP intended because it does not allow for variable number of arguments.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,945 posts
since Aug 2005
Jul 16th, 2007
0

Re: variable arguments in C

1) What's the OP?

2) didn't want to over complicate things for the guy, as he seems to be just starting out in C, giving him a basic understanding of parameter passing would be more beneficial without talking to him about 'VA'.

That's my opinion anyway.
Reputation Points: 35
Solved Threads: 5
Junior Poster
dr4g is offline Offline
136 posts
since Apr 2007
Jul 16th, 2007
0

Re: variable arguments in C

Click to Expand / Collapse  Quote originally posted by dr4g ...
1) What's the OP?

2) didn't want to over complicate things for the guy, as he seems to be just starting out in C, giving him a basic understanding of parameter passing would be more beneficial without talking to him about 'VA'.

That's my opinion anyway.
OP = Original Poster. I thought OP was a pretty common term, but maybe I was wrong. That's what I get for using an anronym.

He asked for variable arguments -- the code you posted did nothing to answer that question. I don't know his experience and neither does anyone else. He must have at least some programming knowledge in order to even ask the question.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,945 posts
since Aug 2005
Jul 16th, 2007
0

Re: variable arguments in C

Dear friend , I am asking about a function like printf(). as it takes the variable argument.How can we define a function like this?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
hitesh_mathpal is offline Offline
4 posts
since Jul 2007
Jul 16th, 2007
0

Re: variable arguments in C

OP = Original Poster. I thought OP was a pretty common term, but maybe I was wrong. That's what I get for using an anronym.

He asked for variable arguments -- the code you posted did nothing to answer that question. I don't know his experience and neither does anyone else. He must have at least some programming knowledge in order to even ask the question.

Haha, i thought he ment passing variable arguments.. as you can pass an array as a parameter (using pointers of course), so i thought he ment how do you pass a variable to a function.

Oh well
Reputation Points: 35
Solved Threads: 5
Junior Poster
dr4g is offline Offline
136 posts
since Apr 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: How to Send Data to IP address
Next Thread in C Forum Timeline: Convert MarkDown to MarkUP





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC