Solved

What's wrong with this???

Posted on 1998-03-26
21
468 Views
Last Modified: 2008-02-01
What's wrong with this???

VOID os_printf(char *ctl, ...)
  {
    va_list argList;

    va_start(argList, ctl + 1);
    myPrintf("os_printf - ");
    myPrintf(ctl, argList);
    va_end(argList);
    myPrintf("\n");
  }
0
Comment
Question by:tflai
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 11
  • 10
21 Comments
 
LVL 22

Expert Comment

by:nietod
ID: 1183836
What are you trying to do with the "+1" in the in va_start()?  What are you trying to do period?
0
 
LVL 4

Author Comment

by:tflai
ID: 1183837
Basically just to port the os_printf() to my printf() functions.
0
 
LVL 4

Author Comment

by:tflai
ID: 1183838
I tried without +1 also...
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Expert Comment

by:nietod
ID: 1183839
Is myprintf() overloaded?  Your first call has only one parameter.  I assume there is a myprintf() that takes a va_list as its second argument.  That should be fine, but I think you are initializing "arglst" incorrectly.  You should not be adding 1.  Try

VOID os_printf(char *ctl, ...)
      {
        va_list argList;

        va_start(argList, ctl);
        myPrintf("os_printf - ");
        myPrintf(ctl, argList);
        myPrintf("\n");
      }

note you shouldn't need the va_end, either.



0
 
LVL 4

Author Comment

by:tflai
ID: 1183840
To make long story short, I've been trying to port a TCP/IP stack to our embedded system environment.  It uses os_printf() which the developer would have to replace it with os-dependent printf() command.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183841
Opps, I hadn't seen you message that you tried without +1, when I posted last. What does the myprintf() code look like?
0
 
LVL 4

Author Comment

by:tflai
ID: 1183842
myprintf() basically takes in (char * ctl, ...) and goes through each % identifier and format the message with the corresponding argument.  It function just like printf().

0
 
LVL 22

Expert Comment

by:nietod
ID: 1183843
Wait a second.  It is not declared with a ... arguement is it?  It should take a va_list as a second argument.
0
 
LVL 4

Author Comment

by:tflai
ID: 1183844
Yeh, it's declared with ... as second argument.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183845
when you call myprintf() like

    myPrintf(ctl, argList);

you are not passing the parameters that were passed to os_printf().  You are passing (as the second parameter) a pointer to where those arguments are stored on the stack.  so myPrintf() is getting only two parameters (regardless of what was passed to os-printf()) and the second parameter is a pointer to a parameter in another function, not the regular parameter.  

If this is what you are doing, this can be made to work. You have to change myprintf() a little.  I'll wait for your response before I give details, in case II'm wrong about your approach.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183846
Well, that was a short wait.  Do you need help fixing it?
0
 
LVL 4

Author Comment

by:tflai
ID: 1183847
YES, I think you're on the right track.  So, how should I make it so that myPrintf() gets the exact same paramters passed in as os_printf()?  Thanks.
0
 
LVL 22

Accepted Solution

by:
nietod earned 100 total points
ID: 1183848
You can't exacly.  You can give myprinf() ACCESS to os_printf()'s variable parameters, but you can't PASS it the parameters.  The difference is that myprintf() will not be a variable argument function.  You cannot call it in other cases with a variable number or parameters.  It will always take two parameters.  (If that is a problem, you can get around it by writing an interface procedure that does take a variable number of parameters and then calls myprintf() with only two parameters.)

What you need to do is make the second parameter to myprintf() a va_list.  (and then don't have a "..." parameter).  Then in myprintf() use va_arg() to get parameters using the passed va_list.  Note that this is unusual, but will work.  va-arg is usually used to get a procedures own arguments, but it can get another procedure's arguments as well.  Are you familiar with va_arg or do you need help with that?  if so, try to post your code.
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183849
Apparently, you are good to go.  So I'm going to go.  I'll check in a few hours if you need more.
0
 
LVL 4

Author Comment

by:tflai
ID: 1183850
Okay, I got it:
The hardware dependent printf() looked like this:
myPrintf(LPSTR format, ...)
{
va_list ap;
va_start(ap, format);
format(PutChar, format, ap);
va_end(ap);
}

So, my os_printf() should be:
VOID os_printf(char *ctl, ...)
{
va_list ArgList;
va_start(ArgList, ctl);
format(PutChar, ctl, ArgList);
va_end(ArgList);
}
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183851
That looks right.  Does it work?  Note, you shouldn't need the va_end() for this, at least not for what you've shown.
0
 
LVL 4

Author Comment

by:tflai
ID: 1183852
It still don't work...  This is weird!
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183853
What does not work?  Compiler or run-time?  which procedure/what behavior?

Am I right in assuming that
(1) format() is existing code that you are trying to make use of,
(2) OS_printf() is code written to use format() that does work, and
(3) myprintf() is something you are writing modeling os_printf()?

If the code is too long to post, you could email it to me.  nietod@theshop.net.  If you do that, include a note pointing me to the right place.
0
 
LVL 4

Author Comment

by:tflai
ID: 1183854
1) format() is a chunk of code already provided by our kernel wrapper around AMX RTOS kernel.
2) os_printf() is used by FUSION TCP/IP stack that has to be defined for os-dependent output.
3) hwprintf() is another function provided by our kernel wrapper.  It will completely mimic printf()'s functionality.

Nietod, thanks for the help and the patience.  This is driving me crazy.  With the extremely tight deadline to start out with and that I'm solely responsible for making the IP network to work between PC side and the embedded side, it's really frustrating.  I've done a lot on the PC side already, like TDI-client kernel-mode drivers, IP handshake protocol driver.  But I've just been introduced to the embedded side recently...  sigh...
0
 
LVL 4

Author Comment

by:tflai
ID: 1183855
Well, I did a quick fix by:
#define os_printf Dprintf
(But this technique doesn't allow me to distinguish between os_printf, os_warn, or os_panic,...)
The va_list thing is still bothering me, but I have more to solve with this tight deadline.  I guess I'll leave it as it.  But I still really appreciate what you, nietod,  has helped.  Thanks.  :-)
0
 
LVL 22

Expert Comment

by:nietod
ID: 1183856
Well, when you get back to it.  Post your questions/progress here.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question