Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / March 2007

Tip: Looking for answers? Try searching our database.

question about variable-argument lists

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tao - 05 Mar 2007 16:50 GMT
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.
Doug Harrison [MVP] - 05 Mar 2007 17:35 GMT
>wstring StringFormat(wstring str0, ...)
>
[quoted text clipped - 44 lines]
>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.

Signature

Doug Harrison
Visual C++ MVP


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.