27028 total geeks with 3522 solutions
Recent challengers:
 Welcome, you are an anonymous user! [register] [login] Get a yourname@osix.net email address 

Articles

GEEK

User's box
Username:
Password:

Forgot password?
New account

Shoutbox
timsattemme
<strong><a href="http:/ /www.tiffany cooutleton.c om/">cheap tiffany and co jewelry</a>< /strong> <br > <strong><a href="http:/ /www.tiffany cooutleton.c om/">stores sell tiffany jewelry</a>< /strong> <br > <strong><a href="http:/ /www.tiffany cooutleton.c om/">sell t
timsattemme
[b]<a href="http:/ /www.iccah.o rg/">the north face outlet</a>[/ b] [url=http:// thenorthface sale7387.web s.com]the north face outlet[/url] [b]<strong>< a href="http:/ /www.iccah.o rg/">the north face outlet</a></ strong> <a href="http:/ /thenorthfac esale7387.we
timsattemme
[b]<a href="http:/ /www.swisswa tchesonsale. net/rolex-wa tchesgt-c-79 .html">B<str ong><a href="http:/ /www.swisswa tchesonsale. net/rolex-wa tchesgt-c-79 .html">Buy Rolex Replica Watches</a>< /strong> <br > <strong><a href="http:/ /www.swisswa tchesonsale. net/rolex-wa
timsattemme
<ul><li><str ong><a href="http:/ /www.swisswa tchesonsale. net/">Cheap Rolex Watches</a>< /strong> </l i><li><stron g><a href="http:/ /www.swisswa tchesonsale. net/">replic a watches</a>< /strong> </l i><li><stron g><a href="http:/ /www.swisswa tchesonsale. net/">Cheap wa
timsattemme
<ul><li><str ong><a href="http:/ /www.pofag.o rg/">replica watches</a>< /strong> </l i><li><stron g><a href="http:/ /www.pofag.o rg/">replica watches</a>< /strong> </l i><li><stron g><a href="http:/ /www.pofag.o rg/">quality replica watches</a>< /strong> </l i></ul><br>

Donate
Donate and help us fund new challenges
Donate!
Due Date: Sep 30
September Goal: $40.00
Gross: $0.00
Net Balance: $0.00
Left to go: $40.00
Contributors


News Feeds
The Register
Facebook"s Oculus
unveils 360-degree
VR head tracking
"Crescent Bay"
prototype
Google+ GOING,
GOING ... ? Newbie
Gmailers no longer
forced into
mandatory ID slurp
Last chance to
snaffle LOHAN
Kickstarter goodies
Ex-basketball star
CUFFED on suspicion
of stealing Apple
gear worth $14k
All the top tech
news from Vulture
Central this week
Vodafone to buy 140
Phones 4u stores
from stricken
retailer
A spin of roulette
in the sporty Ford
Fiesta Black
Almunia faces
GRILLING from MEPs
over FAILED Google
settlement deal
"People have
forgotten just how
late the first
iPhone arrived ..."
Weekend reads:
Shark, Chapter and
Verse and World War
One retold in Ring
of Steel
Slashdot
"Reactive"
Development Turns
2.0
Is Alibaba
Comparable To a US
Company?
Researchers Report
Largest DNA Origami
To Date
Emails Cast
Unflattering Light
On Internal
Politics of
Healthcare.gov
Rollout
Emails Cast
Unflattering Light
On Internal
Politics of
Obamacare Website
Rollo
Washington DC To
Return To Automatic
Metro Trains
New "Crescent Bay"
VR Headset Revealed
and Demo"d At
Oculus Connect
Is Google"s Non-Tax
Based Public School
Funding Cause For
Celebration?
Ask Slashdot:
Alternate Software
For Use On
Smartboards?
Video Released,
Crowdfunding
Underway For Axiom
Open Source Cinema
Camera
Article viewer

Dynamic argument list



Written by:shab
Published by:Nightscript
Published on:2004-01-05 00:31:04
Topic:C
Search OSI about C.More articles by shab.
 viewed 17078 times send this article printer friendly

Digg this!
    Rate this article :
If your reletivly new to C/C++ you might not have noticed it, but im rather sure you have used it. Useful when your writing functions like for example printf.

This is not a very big subject really, it's give things we will look @ in this article. I will list them below, with an description below.

  • ...
  • va_list
  • va_start
  • va_arg
  • va_end

...
This is what you put in your function declaration, if you have seen printf it looks like this:
 int printf(const char *, ...);

The ... show where the dynamic arguments begins, it can never be first and it can never be static arguments after it, you will understand why later.

va_list
The control handle for the arguments, on most systems it's a void *, but some tend to still hold on to char * for some reason.

control handle is not correct acually, it is the memory address that the argument is in, I will explain this more in va_start, you make one like this(rather obvious).
va_list va; /* Replace va with your name */


va_start
This in a macro, so no prototype can be shown. But this is how you could use it for example:
 void a(char b, ...) {
 va_list va;

 va_start(va, b);
}


What va_start really does it that is takes the memory address of b(argument 2) and place it in va_list, hence it's a void *. That means the second argument should be the argument before the dynamic arguments begins.

va_arg
Just like va_start this is a macro, this one is a simple one just now, I really want to show you a prototype, but im afraid that's rather impossible. But here is an code example.
void a(char b, ...) {
 char c;
 va_list va;

 va_start(va, b);
 c = va_arg(va, char); }


The first argument is just like va_start the memory address, the second is how much memory it should move, if you would place int there is would be four, it also "return"(hence it's a macro) that memory.

va_end
I havn't acually checked what this acually does, but it probably free the memory, you use it like this:
void a(char b, ...) {
 char c;
 va_list va;

 va_start(va, b);
 c = va_arg(va, char);
 va_end(va); }


One single argument, as simple as anything.

Example
Here is little function that I made up for example, I just coded it out of thin air while writing this article so excuse any misbehaviour.
#include <stdarg.h> /* I don't think I said it but here is it declared. */
#include <stdio.h>

void print(char *one, ...) {
 va_list va;
 char *p;

 for(p = one; p; p = va_arg(va, char *))
  puts(p);
 va_end(va);

 return; }


I hope that all works ok, otherwise it could be very embarassing.

Did you like this article? There are hundreds more.

Comments:
Anonymous
2006-04-21 20:33:52
wtf?? Mindless illiterate ramblings?
bb
2006-04-21 23:41:05
lol - it does look a bit like that doesnt it!
Anonymously add a comment: (or register here)
(registration is really fast and we send you no spam)
BB Code is enabled.
Captcha Number:


Blogs: (People who have posted blogs on this subject..)
amisauv
Creating a Lexical Analyzer in C on Tue 9th Dec 11am
#include<stdio.h> #include<string.h> #include<conio.h> #include<ctype.h> /*************************************** ************************* Functions prototype. **************************************** *************************/ void Open_File(
amisauv
Controling digital circuit through computer on Tue 9th Dec 10am
this code access the lpt port.here only 4 of the total 8 pins are used but can be modified for full 8 pins.it has a complete GUI with mouse & keyboard interactive control panel.works well in win98, but not in winxp. #include<stdio.h> #include<conio.
amisauv
/* Computerised Electrical Equipment Control */ /* PC BASED DEVICE CONTROLLER * on Tue 9th Dec 10am
#include<stdio.h> #include<conio.h> #include<dos.h> void main() { void tone(void); int p=0x0378; char ex={"Created By Mrc"}; int j; char ex1={"For Further Details & Improvements"}; int k; char ex2={"Contact : E-mail : anbudan
amisauv
Calendar Program on Tue 9th Dec 10am
This program prints Weekdays of specified date. It even prints calendar of a given year too. /*Ccalendar library*/ #include<stdio.h> #include<string.h> #include<conio.h> int getNumberOfDays(int month,int year) { switch(month) { case
amisauv
Calculator: on Tue 9th Dec 10am
#include"graphics.h" #include"dos.h" #include"stdio.h" #include"math.h" union REGS i,o; char text={ "7","8","9","*","4","5","6","/","1","2", "3","+","0","00",".","-","M","M+", "M-","+/-","MR","MC","x^2","sr","OFF","A C","CE","="}; int s=0,k=0,pass
amisauv
INFECTED CODES WRITTEN IN C\C++ on Tue 9th Dec 10am
This is a simple code that changes system time and date. It is written using c/c++ but can be easily converted to java. #include "stdio.h" #include "process.h" #include "dos.h" int main(void) { struct date new_date; struct date old_date; s
amisauv
A C programme which can print the file name it is kept in on Tue 9th Dec 9am
amisauv
BOOTSECTOR EDITOR: on Tue 9th Dec 9am
Code : /*program to save the partion table of your hard disk for future use. it will save your partition table in a file partition.dat */ #include<stdio.h> #include<bios.h> #include<conio.h> #include<stdlib.h> #include<ctype.h> void main () {
amisauv
BLINKING STAR : on Tue 9th Dec 9am
#include<conio.h> #include<graphics.h> #include<stdlib.h> #include<dos.h> void main() { int gdriver=DETECT,gmode; int i,x,y; initgraph(&gdriver,&gmode,"e: cgi"); while(!kbhit()) { x=random(640); y=random(480); setcolor
amisauv
// To print semicolons using C programming without using semicolons any where i on Tue 9th Dec 9am
// To print semicolons using C programming without using semicolons any where in the C code in program. // #include<stdio.h> #include<conio.h> void main() { char a; a=59; if(printf("%c",a)){} getch();

Test Yourself: (why not try testing your skill on this subject? Clicking the link will start the test.)
BSD sockets API by skrye

This is a test of your knowledge of the BSD socket interface
C Programming by keoki

This test is aimed at a C programmer that is at an intermediate level.


     
Your Ad Here
 
Copyright Open Source Institute, 2006