LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices



Reply
 
Search this Thread
Old 03-01-2010, 03:06 PM   #1
10110111
Member
 
Registered: Jun 2008
Location: St.-Petersburg, Russia
Distribution: (B)LFS, Ubuntu, SliTaz
Posts: 402

Rep: Reputation: 51
How to pass arguments to a function using a loop


I need to pass a large number of arguments to a function which takes variable number of arguments, such as gtk_list_store_new. But it doesn't look nice if i write something like gtk_list_store_new(NUM,TYPE_A,TYPE_B,TYPE_C,...,TYPE_OMEGA); because of large number of arguments. And, it will be a trouble to change number of columns because of need to manually change arguments to large number of such functions.
So, how can i pass all the arguments to a function using a loop? Something like
Code:
for(i=0;i<NUM;i++)
{
 push_arg(args[i]);
}
call_function(func);
? Of course, i could just use asm code for this, but is there a portable way of doing so?

P.S. i mean C language.

Last edited by 10110111; 03-01-2010 at 03:08 PM.
 
Old 03-01-2010, 03:33 PM   #2
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 59
You can look into variable-length argument lists (example here), which is similar to how "printf", "scanf", etc, work. I've never implemented one of these functions, but a quick read on a different site says that there is no information about the type and number of subsequent arguments, so from that to me it seems you can pass your different types TYPE_A, TYPE_B, etc.

Alternatively, you can wrap the arguments in a structure (excuse any syntax errors)
Code:
struct arg_type
{
  char foo_set;
  int foo;

  char bar;
  char bar_set;
};

void f(struct arg_type args)
{
  if ( args.foo_set)
  {
    // do something with args.foo
  }

  // etc...
}

int main(void)
{
  struct arg_type args;
  args.foo_set = 1;
  args.foo = 42;

  f(args);
}
Maybe you could use a "void*[MAX_ARG_LENGTH]", and only populate the relevant index if that argument is "set" or "passed". For example
Code:
// map argument types to specific constant indices
const int INDEX_FOO = 2;
const int INDEX_BAR = 5;
// etc.
const itn MAX_ARG_LENGTH=42;

void foo(void* args[MAX_ARG_LENGTH])
{
  if (args[INDEX_FOO) != null)
  {
    // do something with arg 'foo'
  }

  // etc
}

int main(void)
{
  void* args[MAX_ARG_LENGTH];
  // "pass" value for only "foo"
  int foo = 1234;
  args[INDEX_FOO] = &foo;
  f(args);
}

Last edited by nadroj; 03-01-2010 at 03:41 PM.
 
Old 03-01-2010, 04:24 PM   #3
10110111
Member
 
Registered: Jun 2008
Location: St.-Petersburg, Russia
Distribution: (B)LFS, Ubuntu, SliTaz
Posts: 402

Original Poster
Rep: Reputation: 51
Thanks for the reply. I found using your link that there's no portable way of passing variable list of arguments built on run time to functions such as f(a,...).
Anyway, i found that there does exist a function which will accept an array instead of explicit arguments - gtk_list_store_newv.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to pass arguments in a shell script? Drigo Linux - Newbie 1 11-14-2009 12:55 PM
pass arguments and return values from a function in UNIX gaynut Programming 4 07-10-2008 02:56 AM
Looking for a more effecient way to pass arguments to functions. RHLinuxGUY Programming 10 05-01-2006 11:52 PM
How to pass structure as function arguments ssg14j Programming 2 08-20-2005 10:59 PM
How can we pass arguments to installpkg ? frenchi Slackware 3 04-23-2004 07:43 PM


All times are GMT -5. The time now is 11:44 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration