1

I have made one simple variable argument list function in C. But it does not work. When I call that function with one argument, and then check that argument inside that function, that argument lost its value. e.g. In the following code, when I check the value of the "format" it always remain NULL .i.e. It is always displaying "format is NULL." in the debug message. Please guide me, what are the possibilities for this cause.

Calling the function:    lcdPrintf( "Change" );

int lcdPrintf( char * format, ... ) 
{
    if ( *format ) {
        printf("format is not NULL.\r\n");
    }
    else {
        printf("format is NULL.\r\n");
    }

     return -1;
}
3
  • Works here like a charm; that's definitely more in this. Commented Oct 23, 2012 at 22:07
  • You ought to check format, not *format. Anyway, I don't see how it can tell you 'format is NULL'. Unless you called it with lcdPrintf("") perhaps? Commented Oct 23, 2012 at 22:07
  • Thank you everybody for reply. The code is definitely correct. As I am working on Renesas RX63N Microcontroller, I think, the stack might be filled. Or the memory where the variable argument is saved, might be filled. But I am not sure, exactly where this variable argument is saved. And how can I check whether the memory is filled or not! Please guide me, How to track this memory issue. Commented Oct 24, 2012 at 1:11

3 Answers 3

1

You are testing the value of the first character pointed by format when using if ( *format ), use if ( format ) if you want to check the validity of your pointer. But with the call you wrote, it should work anyhow.

The use of variable argument requires stdarg.h and the use of the macros va_start, va_arg and va_end to work with it.

Variable argument handling needs to know what is the type of each argument you are using. That is where the format string is useful in printf. Each of you argument has some type (%s is a char *, %d is an integer), and it helps va_arg macro to know how many bytes it has to read to get the next argument value.

Here is a simple example of the use of va_args

#include <stdarg.h>

void printIntegers(int count, ...)
{
    va_list ap;
    int i;

    va_start(ap, count);
    for (i = 0; i < count; i++) {
        int v = va_arg(ap, int);

        printf("%d\n", v);
    }
    va_end(ap);                      
}

int main()
{
    printIntegers(2, 12, 42);
}
Sign up to request clarification or add additional context in comments.

Comments

0

I've tested your function using the code below and it seems to work. Is it possible that the problem stems from somewhere else in your code?

#include <stdio.h>

int lcdPrintf( char * format, ... ) 
{
    if ( *format ) {
        printf("format is not NULL.\r\n");
    }
    else {
        printf("format is NULL.\r\n");
    }   
    return 1;
}

int main(){ 
    lcdPrintf("Test"); // Prints "format is not NULL."  
    return (0);
}

Comments

0

Please guide me, what are the possibilities for this cause.

Your code contains an error, which makes you check the first character of the format string, in this case 'C' (for "Change").

So, there is one possibility: that the format string you passed has a zero first character, i.e., it is empty.

#include <stdio.h>

int lcdPrintf( char * format, ... )
{
    /* if you want to check whether format is null, the test ought to be */
    /* if (format) ..., not if (*format) ... */

    if ( *format ) {
        printf("format is not NULL.\r\n");
    }
    else {
        printf("format is NULL.\r\n");
    }
    return 0;
}

int main(void)
{
        lcdPrintf("");
        return 0;
}

This will return "format is NULL". I see no other way, if you call that code as you specified (if you didn't, all bets are off :-) )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.