473,785 Members | 2,618 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

No format string passed to variable argument list function

Hi,

I have a simple printf-like function:

int print(const char *format, ...)
{
char buffer[1024];
va_list argptr;
int i;

va_start(argptr , format);
i = vsnprintf(buffe r, sizeof(buffer), format, argptr);
va_end(argptr);

buffer[sizeof(buffer)-1] = '\0';

printf("%s\n",b uffer); /* this bit just for the sake of testing */

return i;
}

If I call the function using something like:
char message[50];
strcpy(message, "hi there");
print("%s",mess age);

everything works, but if I do:
print(message);

it doesn't (program crashes with an abort).

Is there something wrong with what I'm doing, or should I be looking
elsewhere to work out the cause of my crash?

Thanks a lot,
Adam
Oct 15 '08 #1
30 2757
Adam <ne**@snowstone .org.ukwrites:
Hi,

I have a simple printf-like function:

int print(const char *format, ...)
{
char buffer[1024];
va_list argptr;
int i;

va_start(argptr , format);
i = vsnprintf(buffe r, sizeof(buffer), format, argptr);
va_end(argptr);

buffer[sizeof(buffer)-1] = '\0';

printf("%s\n",b uffer); /* this bit just for the sake of testing */

return i;
}

If I call the function using something like:
char message[50];
strcpy(message, "hi there");
print("%s",mess age);

everything works, but if I do:
print(message);

it doesn't (program crashes with an abort).
I don't see a problem myself. I took your function and added a main function:

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

/* your function */

int main(void) {
print("%s\n", "Hello world!");
print("Goodbye world!\n");
return 0;
}

and it compiles and runs without problems on three different systems.

There could be something that I'm missing, though, which doesn't show up
on those systems. Can you post a complete program that aborts, and tell
us the system and compiler you're using?

It certainly seems like v*printf should be able to handle a va_list from
a function with no extra arguments, though the standard doesn't
explicitly seem to say so. It's conceivable that there's a bug in your
standard library, but hard to say just yet.
Oct 15 '08 #2
Adam <ne**@snowstone .org.ukwrites:
I have a simple printf-like function:

int print(const char *format, ...)
{
char buffer[1024];
va_list argptr;
int i;

va_start(argptr , format);
i = vsnprintf(buffe r, sizeof(buffer), format, argptr);
va_end(argptr);

buffer[sizeof(buffer)-1] = '\0';
This isn't your problem, but what's the purpose of the above?
vsnprintf() should already write a '\0'-terminated string to buffer;
you're just setting character 1023 (which is likely far beyond the end
of the string) to '\0'.
printf("%s\n",b uffer); /* this bit just for the sake of testing */

return i;
}

If I call the function using something like:
char message[50];
strcpy(message, "hi there");
print("%s",mess age);

everything works, but if I do:
print(message);

it doesn't (program crashes with an abort).

Is there something wrong with what I'm doing, or should I be looking
elsewhere to work out the cause of my crash?
It would help if you could post a small, complete, compilable program
that demonstrates the problem. I don't see anything in what you
posted that explains the symptom you're seeing.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 16 '08 #3
Adam wrote:
Hi,

I have a simple printf-like function:
Did you include <stdarg.h>?

--
Ian Collins
Oct 16 '08 #4
Adam <n...@snowstone .org.ukwrote:
I have a simple printf-like function:

int print(const char *format, ...)
{
* char buffer[1024];
Maybe make this static. There's no upper or lower limit
on automatic storage, but I prefer not to allocate more
than 256 bytes in a single function.
* va_list argptr;
* int i;

* va_start(argptr , format);
* i = vsnprintf(buffe r, sizeof(buffer), format, argptr);
Note that [v]snprintf is new in C99. Although many C90
implementations have it as an extension, they may
differ in semantics and prototype.

Did you include the correct header or prototype?
Is print defined before or after use? If after,
did you prototype it correctly before use?
* va_end(argptr);

* buffer[sizeof(buffer)-1] = '\0';
This shouldn't be necessary.
* printf("%s\n",b uffer); /* this bit just for the sake of testing */
puts(buffer) is better since the result string could have
% characters in it that will be subject to conversion.
* return i;
}

If I call the function using something like:
char message[50];
strcpy(message, "hi there");
char message[50] = "hi there";
print("%s",mess age);

everything works, but if I do:
print(message);

it doesn't (program crashes with an abort).
Post a compilable program that exhibits the problem.

Also try print("%s\n", message);
Is there something wrong with what I'm doing,
Nothing obvious.
or should I be looking elsewhere to work out the
cause of my crash?
If you're using a C90 extension, you should check
the manual on what vsnprintf does.

--
Peter
Oct 16 '08 #5
Ian Collins <ia******@hotma il.comwrites:
Adam wrote:
>I have a simple printf-like function:
Did you include <stdarg.h>?
He must have; otherwise the identifier "va_arg" wouldn't be visible.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 16 '08 #6
Keith Thompson wrote:
Ian Collins <ia******@hotma il.comwrites:
>Adam wrote:
>>I have a simple printf-like function:
Did you include <stdarg.h>?

He must have; otherwise the identifier "va_arg" wouldn't be visible.
Or va_list. Yes it was a silly question!

--
Ian Collins
Oct 16 '08 #7
Ian Collins <ian-n...@hotmail.co mwrote:
Keith Thompson wrote:
Ian Collins <ian-n...@hotmail.co mwrites:
Adam wrote:
I have a simple printf-like function:
>
Did you include <stdarg.h>?
He must have; otherwise the identifier "va_arg"
wouldn't be visible.

Or va_list. *Yes it was a silly question!
Not necessarily. How do you know Adam isn't using
<varargs.h>?

--
Peter
Oct 16 '08 #8
Peter Nilsson <ai***@acay.com .auwrites:
Adam <n...@snowstone .org.ukwrote:
<snip>
>Â* printf("%s\n",b uffer); /* this bit just for the sake of testing */

puts(buffer) is better since the result string could have
% characters in it that will be subject to conversion.
Did you misread the printf? It looks fine to me (though puts probably
has the edge).

--
Ben.
Oct 16 '08 #9
Ian Collins <ia******@hotma il.comwrites:
Keith Thompson wrote:
>Ian Collins <ia******@hotma il.comwrites:
>>Adam wrote:
I have a simple printf-like function:

Did you include <stdarg.h>?

He must have; otherwise the identifier "va_arg" wouldn't be visible.
Or va_list. Yes it was a silly question!
Yes, I meant va_list.

--
Keith Thompson (The_Other_Keit h) ks***@mib.org <http://www.ghoti.net/~kst>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Oct 16 '08 #10

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

Similar topics

4
13545
by: Andrew Chalk | last post by:
I am a raw beginner to Python. I just read in "Learning Python" that assigning to a string argument inside a function does not change the string in the caller. I want an assignment in the function to alter the passed string in the caller. Is there any way to do this? For example def SafeAdd(self, Variable, Value): if self.form.has_key( Value ): Variable = self.form.value
3
4520
by: Lyn | last post by:
Hi, I have a Search input form which collects from the user a person's name. I am using LIKE with a "%" suffix in the SQL so that the user does not have to type in the full name. When they hit the Search button, a query is run to search the Person table for a match. This produces a recordset (I am using ADO). If the RecordCount is zero, they get a No Match message. If the RecordCount is 1, a DoCmd.OpenForm is performed to open the...
6
2836
by: Stuart McGraw | last post by:
I am looking for a VBA "format" or "template" function, that is, a function that takes a format string and a varying number of arguments, and substitutes the argument values into the format string as specified in the format string. For example fmt("this is $1 format string. arg2 is $2", "short", 3) would return the string "this is short format string. arg2 is 3" Now, the above is easy to write and I have done so. What I want is...
7
4364
by: al | last post by:
char s = "This string literal"; or char *s= "This string literal"; Both define a string literal. Both suppose to be read-only and not to be modified according to Standard. And both have type of "const char *". Right? But why does the compiler I am using allow s to be modified, instead of generating compile error?
23
3615
by: Nascimento | last post by:
Hello, How to I do to return a string as a result of a function. I wrote the following function: char prt_tralha(int num) { int i; char tralha;
35
2469
by: michael.casey | last post by:
The purpose of this post is to obtain the communities opinion of the usefulness, efficiency, and most importantly the correctness of this small piece of code. I thank everyone in advance for your time in considering this post, and for your comments. I wrote this function to simplify the task of combining strings using mixed sources. I often see the use of sprintf() which results in several lines of code, and more processing than really...
6
1490
by: AT | last post by:
Can someone please explain me the following: quote from documentation "Unlike other intrinsic data types, String is a reference type. When a variable of reference type is passed as an argument to a function or subroutine, a reference to the memory address where the data is stored is passed instead of the actual value of the string. " Code
232
13349
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first set of examples, after decoding the HTML FORM contents, merely verifies the text within a field to make sure it is a valid representation of an integer, without any junk thrown in, i.e. it must satisfy the regular expression: ^ *?+ *$ If the...
21
55635
by: phpCodeHead | last post by:
Code which should allow my constructor to accept arguments: <?php class Person { function __construct($name) { $this->name = $name; } function getName()
0
9645
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9481
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10155
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10095
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8979
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7502
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5383
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3656
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2881
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.