473,387 Members | 1,606 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

variadic function calling variadic function

I'm not sure if I'm doing something stupid, or if I'm not understanding
how to use variadic functions. These functions appear to be working
individually. In ast_call, I can vprintf the argument list and it
looks fine. If I invoke write_cmd, it seems to work fine. But when I
invoke write_cmd in ast_call with argp, I get garbage in argp in
write_cmd. I don't know where it's breaking.

int ast_call(struct ast_connection *conn, char *msg, ...)
{
va_list argp;

va_start(argp,msg);
if(!write_cmd(conn->socket,msg,argp)) {
va_end(argp);
return write_FAIL;
}
va_end(argp);
if(!conn->params.wait_for_resp)
return AST_SUCCESS;
return ast_wait_peek_by_id(conn,conn->cnt++);
}

static int write_cmd(int socket, char *msg, ...)
{
int n=0,len=0,slen;
va_list argp;
char buf[BUFSIZE];

va_start(argp,msg);
vsprintf(buf,msg,argp);
va_end(argp);
slen = strlen(buf);
puts(buf);
for(len=0; len<slen;) {
if((n = write(socket,buf+len,slen-len)) < 0)
return 0;
len += n;
}
return 1;
}

May 3 '06 #1
5 3653
go******@signalsguru.net schrieb:
I'm not sure if I'm doing something stupid, or if I'm not understanding
how to use variadic functions. These functions appear to be working
individually. In ast_call, I can vprintf the argument list and it
looks fine. If I invoke write_cmd, it seems to work fine. But when I
invoke write_cmd in ast_call with argp, I get garbage in argp in
write_cmd. I don't know where it's breaking.

int ast_call(struct ast_connection *conn, char *msg, ...)
{
va_list argp;

va_start(argp,msg);
if(!write_cmd(conn->socket,msg,argp)) {
va_end(argp);
return write_FAIL;
}
va_end(argp);
if(!conn->params.wait_for_resp)
return AST_SUCCESS;
return ast_wait_peek_by_id(conn,conn->cnt++);
}

static int write_cmd(int socket, char *msg, ...)
{
int n=0,len=0,slen;
va_list argp;
char buf[BUFSIZE];

va_start(argp,msg);
vsprintf(buf,msg,argp);
va_end(argp);
slen = strlen(buf);
puts(buf);
for(len=0; len<slen;) {
if((n = write(socket,buf+len,slen-len)) < 0)
return 0;
len += n;
}
return 1;
}


http://c-faq.com/varargs/ , especially 15.5 and 15.6

-Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
May 3 '06 #2

Michael Mair wrote:
<snip>

http://c-faq.com/varargs/ , especially 15.5 and 15.6

-Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


Isn't that what I'm doing? I don't understand what 15.6 has to do with
my problem. I called

va_start(argp,msg);
vsprintf(buf,msg,argp);
va_end(argp);
and

va_start(argp,msg);
if(!write_cmd(conn->socket,msg,argp)) {
va_end(argp);
return write_FAIL;
}
va_end(argp);

I'm just not seeing it. It must be obvious to someone. Thanks.

May 3 '06 #3
In article <11**********************@j73g2000cwa.googlegroups .com>
<go******@signalsguru.net> wrote:
I'm not sure if I'm doing something stupid, or if I'm not understanding
how to use variadic functions.
Michael Mair already pointed you at the FAQ entry. I will add
a few more remarks here:
... In ast_call, I can vprintf the argument list and it
looks fine.
Note, you are calling vprintf(), not printf(), when it "works fine".
If I invoke write_cmd [directly], it seems to work fine.
Here you are calling printf-- er, I mean write_cmd(), not vprintf--
oops, I mean vwrite_cmd().
But when I invoke write_cmd in ast_call ...
You need to call vwrite_cmd().

Of course, you first need to *write* vwrite_cmd(). This is easy
since you already have all the appropriate code:
static int write_cmd(int socket, char *msg, ...)
{
int n=0,len=0,slen;
va_list argp;
char buf[BUFSIZE];

va_start(argp,msg);
vsprintf(buf,msg,argp);
va_end(argp);
slen = strlen(buf);
puts(buf);
for(len=0; len<slen;) {
if((n = write(socket,buf+len,slen-len)) < 0)
return 0;
len += n;
}
return 1;
}


Change the ", ..." part to take a "va_list" argument, stop calling
va_start and va_end (since you already have the va_list), and leave
the rest of the code as-is. Then rewrite write_cmd() in terms of
a call to vwrite_cmd() (to eliminate redundancy and thus improve
maintainability, and incidentally save code space too).
--
In-Real-Life: Chris Torek, Wind River Systems
Salt Lake City, UT, USA (40°39.22'N, 111°50.29'W) +1 801 277 2603
email: forget about it http://web.torek.net/torek/index.html
Reading email is like searching for food in the garbage, thanks to spammers.
May 3 '06 #4
go******@signalsguru.net wrote:
Michael Mair wrote:
<snip>
http://c-faq.com/varargs/ , especially 15.5 and 15.6

-Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


Isn't that what I'm doing? I don't understand what 15.6 has to do with
my problem. I called

va_start(argp,msg);
vsprintf(buf,msg,argp);
va_end(argp);
and

va_start(argp,msg);
if(!write_cmd(conn->socket,msg,argp)) {
va_end(argp);
return write_FAIL;
}
va_end(argp);

I'm just not seeing it. It must be obvious to someone. Thanks.


Michael did not point you at the most appropriate question, which is 15.12

He possibly expected you to work it out from how vsprintf works.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc
May 3 '06 #5

Flash Gordon wrote:
go******@signalsguru.net wrote:
Michael Mair wrote:
<snip>
http://c-faq.com/varargs/ , especially 15.5 and 15.6

-Michael
--
E-Mail: Mine is an /at/ gmx /dot/ de address.


Isn't that what I'm doing? I don't understand what 15.6 has to do with
my problem. I called

va_start(argp,msg);
vsprintf(buf,msg,argp);
va_end(argp);
and

va_start(argp,msg);
if(!write_cmd(conn->socket,msg,argp)) {
va_end(argp);
return write_FAIL;
}
va_end(argp);

I'm just not seeing it. It must be obvious to someone. Thanks.


Michael did not point you at the most appropriate question, which is 15.12

He possibly expected you to work it out from how vsprintf works.
--
Flash Gordon, living in interesting times.
Web site - http://home.flash-gordon.me.uk/
comp.lang.c posting guidelines and intro:
http://clc-wiki.net/wiki/Intro_to_clc

For some reason the vprintf/printf thing wasn't clicking. The last two
posts put me on the right track. Thanks.

May 3 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Colin Walters | last post by:
Hi, I have a program which had a little function that always returned TRUE, it looked like this: int always_true_function (void *dummy, ...) { return 1; }
89
by: Sweety | last post by:
hi, Is main function address is 657. its show in all compiler. try it & say why? bye,
5
by: Christopher Benson-Manica | last post by:
Do variadic functions (such as printf()) always scan the format string for format specifiers, even when there are no additional arguments? If it is instead a QoI issue, what would be true for a...
19
by: Ross A. Finlayson | last post by:
Hi, I hope you can help me understand the varargs facility. Say I am programming in ISO C including stdarg.h and I declare a function as so: void log_printf(const char* logfilename, const...
2
by: pinkfloydhomer | last post by:
Can I use printf inside a variadic function that I am writing? Like: void print(const char * format,...) { va_list va; va_start(va, format); char buffer; // yeah, I know......
12
by: Laurent Deniau | last post by:
I was playing a bit with the preprocessor of gcc (4.1.1). The following macros expand to: #define A(...) __VA_ARGS__ #define B(x,...) __VA_ARGS__ A() -nothing, *no warning* A(x) -x ...
9
by: CryptiqueGuy | last post by:
Consider the variadic function with the following prototype: int foo(int num,...); Here 'num' specifies the number of arguments, and assume that all the arguments that should be passed to this...
8
by: keith | last post by:
Someone please help me out here - I'm having a 'bad brain' day. I have a number of C logging functions, prototyped as, say: extern "C" { mylog1(char *arg1, char *arg2, char *arg3);...
8
by: Christof Warlich | last post by:
Hi, is there any way to access individual elements in the body of a variadic macro? I only found __VA_ARGS__, which always expands to the complete list. Thanks for any help, Christof
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.