EmbeddedRelated.com
Forums

Iar va_list issue

Started by Bukitoo September 11, 2008
Hi!

I am programming a function in with I pass as arguent many strings and it
concateates all of them.

Basically I 've modified iar's help example (COPIED BELOW):
#include
void va_cat(char *s, ...)
{
char *t;
va_list ap;

va_start(ap, s);
while (t = va_arg(ap, char *)) null pointer ends list
{
s += strlen(s); skip to end
strcpy(s, t); and copy a string
}
va_end(ap);
}

But the null pointer assignment when the list finishes never shows up.

Why is that ? Wha Am I doing wrong ?


Beginning Microcontrollers with the MSP430

Bukitoo wrote:
> I am programming a function in with I pass as arguent many strings and it
> concateates all of them.
>
> Basically I 've modified iar's help example (COPIED BELOW):

Hi!

One rule of thumb is to post a complete example, in this case you should
demonstrate how you call your function...

Anyway, I tested the function and it appears to run smoothly here. The
following program prints "one two three", as expected:

#include
#include
#include
void va_cat(char *s, ...)
{
char *t;
va_list ap;

va_start(ap, s);
while (t = va_arg(ap, char *)) // null pointer ends list
{
s += strlen(s); // skip to end
strcpy(s, t); // and copy a string
}
va_end(ap);
}

#include
int main()
{
char buf[100] = "";

va_cat(buf, "one ", "two ", "three", NULL);

puts(buf);
}

-- Anders Lindgren, IAR Systems
--
Disclaimer: Opinions expressed in this posting are strictly my own and
not necessarily those of my employer.

Thanks Anders!
Yesterday I've realized that I had to pass as argument a NULL pointer at the
end. That was my missundestanding about the example.
Now I have the function working correctly.

Thanks!!!

_____

De: m... [mailto:m...] En nombre de Anders
Lindgren
Enviado el: viernes, 12 de septiembre de 2008 8:51
Para: m...
Asunto: Re: [msp430] Iar va_list issue

Bukitoo wrote:
> I am programming a function in with I pass as arguent many strings and it
> concateates all of them.
>
> Basically I 've modified iar's help example (COPIED BELOW):

Hi!

One rule of thumb is to post a complete example, in this case you should
demonstrate how you call your function...

Anyway, I tested the function and it appears to run smoothly here. The
following program prints "one two three", as expected:

#include
#include
#include
void va_cat(char *s, ...)
{
char *t;
va_list ap;

va_start(ap, s);
while (t = va_arg(ap, char *)) // null pointer ends list
{
s += strlen(s); // skip to end
strcpy(s, t); // and copy a string
}
va_end(ap);
}

#include
int main()
{
char buf[100] = "";

va_cat(buf, "one ", "two ", "three", NULL);

puts(buf);
}

-- Anders Lindgren, IAR Systems
--
Disclaimer: Opinions expressed in this posting are strictly my own and
not necessarily those of my employer.