Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 975141
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:32:16+00:00 2026-05-16T03:32:16+00:00

I’m trying to make a generic function taking a variable argument list. A part

  • 0

I’m trying to make a generic function taking a variable argument list. A part of the design is that some of these functions call each other. Unfortunately it doesn’t seem to work. As you can see if you run the simple code below, the call to command() always fails, but the direct call to marshal_size() succeeds in decoding the two strings “FIRST_STR_ARG” and “SECOND_STR_ARG” according to the format string “FORMAT_STRING”.

What is wrong in my reasoning?

The sample code compiles equally well with “g++ main.cpp” or “gcc main.c”.

Thanks,
  jules

#include <stdarg.h>
#include <stdio.h>
#include <inttypes.h>
#include <string.h>
#include <stdlib.h>

#define MARSHAL_FORMAT "%s%s"
#define FIRST_STR_ARG "THIS_IS_ARG_ONE"
#define SECOND_STR_ARG "THIS_IS_ARG_TWO"

#define d(msg__, ...) do { printf("%s@%d: "msg__"\n", __FILE__, __LINE__, ## __VA_ARGS__); } while (0)

static uint32_t
marshal_size(const char *format, ...)
{
    uint32_t retv = 0;
    uint8_t ub;
    uint16_t uw;
    uint32_t ul;
    char *s;
    va_list ap;

    if (!format || !strlen(format))
            return 0;
    d("format = %s \n", format);

    va_start(ap, format);
    for (; '\0' != *format; format++) {

            d("*format = %c \n", *format);
            if ('%' == *format) {
                    format++;
                    if ('u' == *format)
                            format++;
            } else {
                    d("FORMAT ERROR\n");
                    continue;
            }
            d("*format = %c \n", *format);

            switch (*format) {
            case 's':
                    s = va_arg(ap, char*);
                    d("va_arg = %s\n", (s ? s : "NULL"));
                    if (s)
                            retv += strlen(s) + 1;
                    break;
            case 'l':
                    ul = va_arg(ap, uint32_t);
                    retv += sizeof(uint32_t);
                    break;
            case 'w':
                    uw = (uint16_t)va_arg(ap, int);
                    retv += sizeof(uint16_t);
                    break;
            case 'b':
                    ub = (uint8_t)va_arg(ap, int);
                    retv += sizeof(uint8_t);
                    break;
            default:
                    goto exit;
            }

            continue;
    exit:
            break;
    }

    va_end(ap);

    return retv;
}

static uint32_t
command(const char * const format,
    ...)
{
    uint32_t retv;
    va_list ap;

    va_start(ap, format);

    retv = marshal_size(format, ap);

    va_end(ap);

    return retv;
}

int
main(int argc, char *argv)
{
    uint32_t size;

    size = command(MARSHAL_FORMAT, FIRST_STR_ARG, SECOND_STR_ARG);
    d("size = %d", size);

    size = marshal_size(MARSHAL_FORMAT, FIRST_STR_ARG, SECOND_STR_ARG);
    d("size = %d", size);

    return EXIT_SUCCESS;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:32:17+00:00Added an answer on May 16, 2026 at 3:32 am

    You have to make marshal_size take a va_list rather than .... See Question 15.12 in the FAQ at c-faq.com.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need a function that will clean a strings' special characters. I do NOT
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.