Link to home
Start Free TrialLog in
Avatar of shahrine99
shahrine99

asked on

string

Hey, how do I write a C++ function named display that takes any number of arguments. The first argument must be an interger. The remaining arguments will be C-style strings (character pointers). The first argument will indicate how many strings the call contains. The function will print the strings on a single line, with arguments seperated by one space. For example, the call
     display (2, "abc", "def");
will produce the following output:
     abc def

You know what I mean?
SOLUTION
Avatar of sunnycoder
sunnycoder
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi, I will give you some points...

1) #include <stdarg.h> (in case of C) or <cstdarg> (in case of C++)

2) Use va_list to create a pointer to the arguments.

3) Use va_start to initialize the pointer to the last known parameter. (In this case, the numeric argument)

4) Put a for loop to N no. of times, and use va_arg to iterate through each of the arguments. You need to use char* as the second parameter to the va_arg. Print each of the obtained value.

5) Use va_end to clear the pointer.


Let us know if you have any problem writing the code

---
Harish
Avatar of shahrine99
shahrine99

ASKER

nah its not a homework question..im cramming for final exam..and im just looking over the study guide..that question was on the study guide..so i didnt not understand that....my guess would be like

int display = 4;
cout << display << "1" << "2" << "3" << "4" << end;

no?



im suppose to write a program? i thought its just a small code
Its about 10 lines of code ... Give it a try ...

>int display = 4;
>cout << display << "1" << "2" << "3" << "4" << end;
>
>no?
No :)

 display (2, "abc", "def");
will produce the following output:
     abc def
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Harish,

Which of my links has that piece of code ?

sunnycoder
LOL I edited the program given in the first link to suit the asker's requirement. I didn't remove the comments, however.

:)
Isn't that the idea of posting a helpful link for a student? They have to read and understand and make little modifications to tailor it for their needs.

They need to change just a line or two but they need to understand the concept to make those changes.

Now that you have made those changes too, I guess they have nothing left to learn.
I agree. But even after your comment was posted, he was asking how to do that. So I changed it.
dang aight sunny daddy....lol...im learning haha
That was because they did not make any efforts on their own ...
whoa...harish...ur code is confusing me...wat in da world va_start and va_list are?
jeez sunny daddy..i feel dishearten =)...have sympathy for me...me cramming for final exam...my head is spinning lol
Read my first comment
> wat in da world va_start and va_list are?

In C/C++ world, they are macros defined in stdarg.h or cstdarg
my bad...i was scrolling too fast...thanks =)
thanks to sunny too for the links =)