473,394 Members | 2,048 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,394 software developers and data experts.

variable arguments, odd results

I'm getting odd results from my fuction that takes variable aguments.

What i want to do is take a printf like format and pass that to
mysql_query then i do some processing of the results and return a
pointer to an array of the results.

Everything works if i guess at the length of the query and malloc that
amount. The problem comes when i try to figure out the size of the
varible arguments.

The way i figure this out below is taking directly from FAQ question
15.4 and 15.5.

I also need to be able to tell the size lquery needs to be even if i
pass it an int or float or whatever, not just strings, is that possible
?

What am i doing wrong?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

char ***simplesql_query(int *conn, unsigned int *NumberOfRows, unsigned
int *NumberOfFields, char *query, ...)
{
/* parse the query using printf like format and put that all in
lquery */
va_list argp;
char *lquery;
size_t len;
char *p;

/* figure out the size of the query */
va_start(argp, query);
len = strlen(query);
printf("query=%s:len=%d\n",query,len);
while((p = va_arg(argp, char *)) != NULL)
{
/* p has some odd stuff in it, why???? */
printf("len is %d,p is %s\n",len,p);
len += strlen(p);
}
va_end(argp);

/* add one for \0 */
len++;
if ((lquery = malloc(len)) == NULL)
return NULL;

/* pass off query to vsprintf */
va_start(argp, query);
vsprintf(lquery, query, argp);
va_end(argp);

printf("simplesql_query: %s, lentgh=%d\n", lquery,len);

/*
do mysql work stuff

if (mysql_query(conn, lquery) != 0)
simplesql_error(conn,"SELECT failed");
else
...
*/
return NULL;
}
int main()
{
int *conn = 0;
char ***sqlresults;
int rows, fields;
/* odd results */
char userid2[] = "5";
sqlresults = simplesql_query(conn,&rows,&fields,"SELECT UserID FROM
Users WHERE UserID < %s",userid2);

return 0;
}

Nov 14 '05 #1
3 1536
Greetings,

Eli Criffield wrote:
I'm getting odd results from my fuction that takes variable aguments.

What i want to do is take a printf like format and pass that to
mysql_query then i do some processing of the results and return a
pointer to an array of the results.

Everything works if i guess at the length of the query and malloc that
amount. The problem comes when i try to figure out the size of the
varible arguments.

The way i figure this out below is taking directly from FAQ question
15.4 and 15.5.

I also need to be able to tell the size lquery needs to be even if i
pass it an int or float or whatever, not just strings, is that possible
?

What am i doing wrong?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

char ***simplesql_query(int *conn, unsigned int *NumberOfRows, unsigned
int *NumberOfFields, char *query, ...)
{
/* parse the query using printf like format and put that all in
lquery */
va_list argp;
char *lquery;
size_t len;
char *p;

/* figure out the size of the query */
va_start(argp, query);
len = strlen(query);
printf("query=%s:len=%d\n",query,len);
while((p = va_arg(argp, char *)) != NULL)
You don't pass a NULL when this function is called, so after the first
pass you're getting whatever garbage is lying around.
{
/* p has some odd stuff in it, why???? */
printf("len is %d,p is %s\n",len,p);
len += strlen(p);
}
va_end(argp);
A better way would be use the return from vsnprintf(0, 0, fmt, argp)

/* add one for \0 */
len++;
if ((lquery = malloc(len)) == NULL)
return NULL;

/* pass off query to vsprintf */
va_start(argp, query);
vsprintf(lquery, query, argp);
va_end(argp);

printf("simplesql_query: %s, lentgh=%d\n", lquery,len);

/*
do mysql work stuff

if (mysql_query(conn, lquery) != 0)
simplesql_error(conn,"SELECT failed");
else
...
*/
return NULL;
}
int main()
{
int *conn = 0;
char ***sqlresults;
int rows, fields;
/* odd results */
char userid2[] = "5";
sqlresults = simplesql_query(conn,&rows,&fields,"SELECT UserID FROM
Users WHERE UserID < %s",userid2);

return 0;
}

--
Kyle A. York
Sr. Subordinate Grunt
DSBU

Nov 14 '05 #2
Eli Criffield <el**********@gmail.com> wrote:
I'm getting odd results from my fuction that takes variable aguments.

What i want to do is take a printf like format and pass that to
mysql_query then i do some processing of the results and return a
pointer to an array of the results.

Everything works if i guess at the length of the query and malloc that
amount. The problem comes when i try to figure out the size of the
varible arguments.

The way i figure this out below is taking directly from FAQ question
15.4 and 15.5.
In the suggestion made in FAQ's 15.4. All usage of the function needs a
trailing "(char *) NULL" argument. Your call to the "simplesql_query"
doesn't do so.

If you don't want all you functions to get this NULL pointer in the end,
you should introduce some other way of telling the function how many
arguments there actually are passed.

printf-like functions do this by using %-formats in the format string,
such that va_arg() is only used when there is a %-format in the string.

I also need to be able to tell the size lquery needs to be even if i
pass it an int or float or whatever, not just strings, is that possible
?

What am i doing wrong?

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>

char ***simplesql_query(int *conn, unsigned int *NumberOfRows, unsigned
int *NumberOfFields, char *query, ...)
{
/* parse the query using printf like format and put that all in
lquery */
va_list argp;
char *lquery;
size_t len;
char *p;

/* figure out the size of the query */
va_start(argp, query);
len = strlen(query);
printf("query=%s:len=%d\n",query,len);
while((p = va_arg(argp, char *)) != NULL)
{
/* p has some odd stuff in it, why???? */
printf("len is %d,p is %s\n",len,p);
len += strlen(p);
}
va_end(argp);

/* add one for \0 */
len++;
if ((lquery = malloc(len)) == NULL)
return NULL;

/* pass off query to vsprintf */
va_start(argp, query);
vsprintf(lquery, query, argp);
va_end(argp);

printf("simplesql_query: %s, lentgh=%d\n", lquery,len);

/*
do mysql work stuff

if (mysql_query(conn, lquery) != 0)
simplesql_error(conn,"SELECT failed");
else
...
*/
return NULL;
}
int main()
{
int *conn = 0;
char ***sqlresults;
int rows, fields;
/* odd results */
char userid2[] = "5";
sqlresults = simplesql_query(conn,&rows,&fields,"SELECT UserID FROM
Users WHERE UserID < %s",userid2);

return 0;
}


--
Z (zo**********@web.de)
"LISP is worth learning for the profound enlightenment experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days." -- Eric S. Raymond
Nov 14 '05 #3
Oh i thought somehow the NULL was va_args way of telling you it
doesn't have any more arguments, i guess if i would have looked closer
at the FAQ i would have seen that it gets passed to there example
function.

using vsnprintf(0,0,query,argp) to figure the size works a lot better
anyway.

I'm kicking myself for not thinking of that.

Thanks

Eli

Nov 14 '05 #4

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

Similar topics

7
by: | last post by:
How to call a function with variable argument list from another function again with variable argument list? Example : double average ( int num, ... ); double AFunct1 ( int num, ... ); double...
134
by: James A. Donald | last post by:
I am contemplating getting into Python, which is used by engineers I admire - google and Bram Cohen, but was horrified to read "no variable or argument declarations are necessary." Surely that...
166
by: Graham | last post by:
This has to do with class variables and instances variables. Given the following: <code> class _class: var = 0 #rest of the class
5
by: Jonathan Burd | last post by:
Greetings everyone, I wrote a function to learn about variable-length argument lists. I wonder if there is a better way to detect the end of the argument list than using a sentinel value like...
23
by: Russ Chinoy | last post by:
Hi, This may be a totally newbie question, but I'm stumped. If I have a function such as: function DoSomething(strVarName) { ..... }
3
by: Tomás | last post by:
Let's say we have a variable length argument list function like so: unsigned GetAverage(unsigned a, unsigned b, ...); I wonder does the compiler go through the code looking for invocations of...
1
by: sphinx.moro | last post by:
Hello, I have a function which is coded to receive an unknown number of arguments void Process(int i_ss, int Nerr, ...) { va_list pa; /* variable argument list*/ va_start(pa,...
18
by: Pedro Pinto | last post by:
Hi there once more........ Instead of showing all the code my problem is simple. I've tried to create this function: char temp(char *string){ alterString(string); return string;
3
by: dedalusenator | last post by:
Hello Folks, My first posting here and I am a stuck in figuring out the exact way to update a global variable from within a function that doesnt return any value (because the function is a...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.