LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
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 04-22-2004, 12:28 AM   #1
jpbarto
Senior Member
 
Registered: Mar 2003
Location: Pittsburgh, PA
Distribution: Gentoo / NetBSD
Posts: 1,251

Rep: Reputation: 45
C Function Arguments


I would like to create a function similar to functions like printf and fprintf in C.

In stdio.h, a function such as fprintf is defined as
extern int fprintf (FILE *__restrict __stream, __const char *__restrict __format, ...);

I'm assuming the '...' portion references the fact that multiple variables can be passed to fprintf (if you have a const char* of "%s %d %f %s" you would then pass four additional arguments.

Is this how I define a function if I want to create such functionality? And if so how do I access these additional arguments once inside the function? (is this another argc, argv scenario?)

Thanks for any help,
jpbarto
 
Old 04-22-2004, 12:43 AM   #2
eantoranz
Senior Member
 
Registered: Apr 2003
Location: Colombia
Distribution: Kubuntu, Debian, Knoppix
Posts: 1,982
Blog Entries: 1

Rep: Reputation: 83
http://www.si.hhs.nl/~bertn/gnu_libc/libc_toc.html

Variadic functions
 
Old 04-22-2004, 01:36 AM   #3
jpbarto
Senior Member
 
Registered: Mar 2003
Location: Pittsburgh, PA
Distribution: Gentoo / NetBSD
Posts: 1,251

Original Poster
Rep: Reputation: 45
Excellent! Thank you so much. And I'll be bookmarking that page.

Thanks,
jpbarto
 
Old 04-22-2004, 01:39 AM   #4
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
Here's a function I wrote and added to my library of useful things

Code:
char *tprintf(char *fmt, ...)
{
  static char buff[4096];
  va_list ap;

  va_start(ap, fmt);
  vsnprintf(buff, sizeof(buff), fmt, ap);
  va_end(ap);

  return buff;
}
 
Old 04-23-2004, 01:01 AM   #5
jpbarto
Senior Member
 
Registered: Mar 2003
Location: Pittsburgh, PA
Distribution: Gentoo / NetBSD
Posts: 1,251

Original Poster
Rep: Reputation: 45
According to the example (and the manual) over at the GNU C library page, I got the impression that va_start's first argument was va_list and the second was an int indicating the number of the last argument. Did I get the wrong impression?

From your example I'd imagine that format might contain a string of "%s %d\n"?, so this can be passed and will initialize a va_list properly?
 
Old 04-23-2004, 01:18 AM   #6
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
Quote:
The parameter last is the name of the last parameter
before the variable argument list, i.e., the last parame-
ter of which the calling function knows the type.
last is the second argument to va_start() here. fmt from my example is a string like "%d %s %p" as you suspected. Anything following the format string in my tprintf() function are variables to be used for substitution. Therefore the fmt is the last paramater with a known type.

Here's an example usage of my tprintf() function just in case it helps visualize it. It works just like sprintf(), except it returns the formatted string instead of storing it in a buffer you pass to it:

send_msg(player, tprintf("%d apples", num_apples));

send_msg() is of course a hypothetical function that I just came up with for the example

To explain why it needs to know the last argument that isn't part of the substitution variable list, consider reversing the first 2 arguments of sprintf() so the protype looks like: sprintf(char *fmt, char *buffer, ...);

In this case, the second argument to va_start() would be buffer, not fmt.

Last edited by itsme86; 04-23-2004 at 01:22 AM.
 
Old 04-23-2004, 02:59 PM   #7
jpbarto
Senior Member
 
Registered: Mar 2003
Location: Pittsburgh, PA
Distribution: Gentoo / NetBSD
Posts: 1,251

Original Poster
Rep: Reputation: 45
I see, thanks for the clarification.
 
  


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 structure as function arguments ssg14j Programming 2 08-20-2005 10:59 PM
How to import arguments to system() function skie_knite007 Programming 5 03-25-2005 09:49 PM
Python function arguments are dictionaries? johnMG Programming 1 07-24-2004 07:29 PM
Arguments in PHP function declaration Parksy Programming 5 07-04-2004 05:22 PM
Function accept() and its arguments. krajzega Programming 1 01-20-2004 02:26 AM


All times are GMT -5. The time now is 11:38 AM.

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