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

question about variable-argument lists

Tao
wstring StringFormat(wstring str0, ...)

{

wstring result = str0;

va_list list;

va_start(list, str0);

int i=0;

for(;;)

{

wchar_t* p=va_arg(list, wchar_t*);

if(p==0)

break;

wstring pattern;

pattern = L"{" + string_streaming::to_wstring(i) + L"}";

//Replace(result, pattern.c_str(), p);

i++;

}

va_end(list);

return result;

}

main()

{

StringFormat("A", "B");

}

The for loop should stop after 1 iteration, but it keeps running and the
wchar_t* p is set to some strange values.

thanks.


Mar 5 '07 #1
1 1119
On Mon, 5 Mar 2007 11:50:32 -0500, "Tao" <tt*******@hotmail.comwrote:
>wstring StringFormat(wstring str0, ...)

{

wstring result = str0;

va_list list;

va_start(list, str0);

int i=0;

for(;;)

{

wchar_t* p=va_arg(list, wchar_t*);

if(p==0)

break;

wstring pattern;

pattern = L"{" + string_streaming::to_wstring(i) + L"}";

//Replace(result, pattern.c_str(), p);

i++;

}

va_end(list);

return result;

}

main()

{

StringFormat("A", "B");

}

The for loop should stop after 1 iteration, but it keeps running and the
wchar_t* p is set to some strange values.
You need to write:

StringFormat(L"A", L"B", (wchar_t*) 0);

It's important to cast 0 used as a pointer in variable argument lists,
particularly on 64-bit Windows, where sizeof(int) == 4 and sizeof(pointer)
== 8. I'm not sure how 64-bit compilers define NULL; perhaps they widen
this integer value to 64 bits, but I'd do the cast anyway. It helps a
little with self-documentation, and you'll be covered if you ever use a
compiler that has a different representation for integer zero and null
pointer values.

--
Doug Harrison
Visual C++ MVP
Mar 5 '07 #2

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

Similar topics

23
by: NotYetaNurd | last post by:
for(int i=0;i<6;i++) { // } wont i go out of scope after the for loop ...?
13
by: gmccallum | last post by:
General Info: A struct is stored on the stack and a class on the heap. A struct is a value type while a class is a reference type. Question: What if a struct contains a string...
29
by: MP | last post by:
Greets, context: vb6/ado/.mdb/jet 4.0 (no access)/sql beginning learner, first database, planning stages (I think the underlying question here is whether to normalize or not to normalize this...
7
by: Matt | last post by:
I've asked this question to some developers that are much more experienced than I, and gotten different answers --- and I can't find anything about it in the documentation. Dim vs. Private as a...
20
by: David | last post by:
I feel like an idiot asking this but here goes: I understand the 'concept' of scope and passing data by value and/or by reference but I am confused on some specifics. class example{ int i; //my...
2
by: =?Utf-8?B?RGlmZmlkZW50?= | last post by:
Hello All, I have a method is which I am accessing a session variable several times and every time I retrieve it from session using Session.ToString(). Is it advisable to retrieve this session...
20
by: tshad | last post by:
Using VS 2003, I am trying to take a class that I created to create new variable types to handle nulls and track changes to standard variable types. This is for use with database variables. This...
5
by: somenath | last post by:
Hi All , I have one question regarding scope and lifetime of variable. #include <stdio.h> int main(int argc, char *argv) { int *intp = NULL; char *sptr = NULL;
8
by: Kid Programmer | last post by:
Hello guys. I am trying to create a calculator program in a GUI window. When I compile the code I get the following errors: Severity and Description Path Resource Location Creation Time Id The...
12
by: Bryan Parkoff | last post by:
I write my large project in C++ source code. My C++ source code contains approximate four thousand small functions. Most of them are inline. I define variables and functions in the global scope....
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...
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
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
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...

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.