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

  • SEARCH
  • Home
  • 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 6733167
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T10:44:12+00:00 2026-05-26T10:44:12+00:00

I made a function in C, that concatenates a variable number of strings. That’s

  • 0

I made a function in C, that concatenates a variable number of strings.
That’s my code:

char* texto(const char* s, ...){
        va_list args;
        char *tmp;
        char *res;
        size_t len = strlen(s);

        // pega um handle ao início da lista de parâmetros
        va_start(args, s);
        // calcula o tamanho total de todas as strings - pega o próximo parâmetro da lista, até chegar no NULL
        while ((tmp = va_arg(args, char*))){
            len += strlen(tmp);
        }
        va_end(args);

        res = malloc(len+1);
        if (!res){
                fprintf(stderr, "Erro ao alocar string. Função 'texto'\n");
                exit(EXIT_FAILURE);
        }

        // cria a string concatenada
        strcpy(res, s);

        va_start(args, s);
        // pega o próximo parâmetro da lista, até chegar no NULL
        while ((tmp  = va_arg(args, char*))){ 
            strcat(res, tmp); 
        }
        va_end(args);

        return res;
}

I’m using like this:

char* txt = texto("a", "b", "c", "d", "e", NULL);
//txt is now: "abcde"

It works fine.
But I can’t pass numeric parameters to this function, only strings.
I need to change the function to work like this:

char* txt = texto("a", "b", 1, "d", 4.5, "e", NULL);
//txt is now: "ab1d4.5e"

How can I do that?
How I get parameters with diferent types using va_arg()?

The solution I found until now is create a function int2str():

inline char* int2str(int inteiro){
    char* str = malloc(10);
    sprintf(str, "%d", inteiro);
    return str;
}

But I have to use this way:

char* txtnum = int2str(23);
char* txt = texto("a", txtnum, NULL);
free(txtnum);

otherwise, I got a memory leak…

I could use the function int2str() inside the function texto() but I don’t know how to check the type of the parameters!

Ps.: I’m using C, not C++ for my code.

  • 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-26T10:44:12+00:00Added an answer on May 26, 2026 at 10:44 am

    If you’re willing to take a gcc specific solution you can change int2str to be this macro:

    # define int2str(i)                                                           \
      (__extension__                                                              \
        ({                                                                        \
          char *new = (char *) __builtin_alloca (10);                        \
          sprintf(new, "%d", i); \
          new;                                  \
        }))
    

    This then means you can write:

    char* txt = texto("a", int2str(23), NULL);
    

    without leaking. It’s very non portable though.

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

Sidebar

Related Questions

I've made some function that generates an email template. Code it generates is pure
I have got an array that consists of strings. I have made a function
I have made a function that validates inputs with for different kind of strings.
I've got a function that creates two tables. I've made every variable between the
Anyone got a ready made function that will take an XML string and return
I'm trying to use Seq.cache with a function that I made that returns a
Hi have made this function which is made to replicate an error that I
I have made a function to cound the weeks in a year, and that
I've made this snippet that clicks a link after 10th second: function timeout() {
Hi I have made this function that takes a table and prepare the label

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.