Thread: problem with a function

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    32

    problem with a function

    Hi there

    I've come to a point where i need to do something like this that is i won't know how many parameters the user would pass. Take a look at the function below:

    Code:
    int myfunction(char buf, int no, const mystruct s1,...);
    umm i need to capture all the structs the user passes in an array... 'no' will be holding the number of structs passed.... Working with arrays i know a bit, however i have never yet worked with these type of functions.... Can some1 help me on this pls?

  2. #2

  3. #3
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Don't over complicate things unnecessarily:
    Code:
    int myfunction(char buf, int no, const mystruct* sArray);
    to access all the structures, simply write:
    Code:
    int i;
    for (i = 0; i < no; i++)
    {
    //something with sArray[i];
    }

  4. #4
    Registered User
    Join Date
    Apr 2007
    Posts
    32
    thx.. yeah i know about that way... however i have the "complicate" things as its the only way the end user will communicate through the function, I can't force him to pass the function an array...

  5. #5
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    I'm not sure how you can make sure the "end user" pass each argument individually any more than you could make him do it as an array.

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,398
    Quote Originally Posted by MacGyver View Post
    I'm not sure how you can make sure the "end user" pass each argument individually any more than you could make him do it as an array.
    Not to mention it makes it impossible to call the function when you don't already know how many structures you need to pass. Variadic functions are usually the worst choice for such things.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Posts
    32
    its because i got a structure for a task i'm doing and i must follow, as for the array part i already did it, however i'm now trying to collect them individually, put them in an array and pass the array to the function i already implemented which is working fine...

    However i never worked with variadic function or what they're called... i didn't even know they were called like that before... Anyway i guess thanks all the same :s

  8. #8
    Lean Mean Coding Machine KONI's Avatar
    Join Date
    Mar 2007
    Location
    Luxembourg, Europe
    Posts
    444
    Here's an example using variable arguments, I've written not so long ago.

  9. #9
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by Cmaniac View Post
    However i never worked with variadic function
    printf() is a variadic function
    Last edited by itsme86; 04-17-2007 at 10:57 AM.
    If you understand what you're doing, you're not learning anything.

  10. #10
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,211
    Quote Originally Posted by itsme86 View Post
    prinf() is a variadic function
    Which goes to show there are usages for variadic functions.

    I'd much rather have printf() in that form rather than having to build an array of void *'s everytime I want to call it.

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,398
    Quote Originally Posted by MacGyver View Post
    I'd much rather have printf() in that form rather than having to build an array of void *'s everytime I want to call it.
    So would I, but in the case of printf(), you always know how many arguments you will be passing, because that number is strictly dictated by the conversion specifiers. In the case of a function which processes a bunch of structures in some way, you may not know how many structures need processing until runtime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 10-29-2008, 06:33 AM
  2. wxWidgets link problem
    By cboard_member in forum C++ Programming
    Replies: 2
    Last Post: 02-11-2006, 02:36 PM
  3. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Problem with function pointers
    By vNvNation in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2004, 06:49 AM