3

How can I concatenate all the values in argv to one string using snprintf?

if i pass in values like ./prog val1 val2 val3 val4

my string char all_values[MAX_LEN] should be "val1 val2 val3 val4"

How can I do this efficiently using snprintf()?

4
  • 5
    Is this a homework problem? I only ask because it will affect how precise an answer I give. Commented Apr 26, 2011 at 21:09
  • No it is not a homework problem. I had not been working in C for some time now and I am unable to figure this out correctly. Commented Apr 26, 2011 at 21:16
  • Why are you interested in using snprintf()? As opposed to, e.g., strcat()? Commented Apr 26, 2011 at 21:30
  • 1
    If your goal is to pass them to another program with system or something, this is a very bad approach. Instead use execv or similar. Commented Apr 26, 2011 at 23:28

3 Answers 3

6
#include <stdio.h>

#define MAX_LEN 16
int main(int ac, char **av) {
   char buffer[MAX_LEN];
   buffer[0] = 0;
   int offset = 0;
   while(av++,--ac) {
      int toWrite = MAX_LEN-offset;
      int written = snprintf(buffer+offset, toWrite, "%s ", *av);
      if(toWrite < written) {
          break;
      }
      offset += written;
   }
   printf("%s\n", buffer);
}
Sign up to request clarification or add additional context in comments.

2 Comments

No need for the buffer[MAX_LEN-1] = 0; as "output bytes beyond the n-1st shall be discarded instead of being written to the array, and a null byte is written at the end of the bytes actually written into the array" (POSIX, pubs.opengroup.org/onlinepubs/9699919799/functions/fprintf.html)
Thanks. I was working from the Linux man page, and couldn't find that assurance. I'll edit my answer.
1

If you to want make a print of N arguments, you can do

int i = 1 ; // first parameter is a program name  
while(i < argc )
{
   printf("%s",argv[1]);
   i++;
}

But if you want to use a string in other processor,you would really concatenate then. Maybe with:

char* string_result; 

int  i = 1; 

int  size_total = 0;
bool space_needed = false;

while(i < argc) { // argc contain the number of arguments
   size_total += strlen(argv[i])+1; //+1 for a new space each time.
   i++;
}

if(i > 2) {
    space_needed = true;
    size_total -= 1; //no need for space at end of string
}

string_result = (char*)malloc((size_total+1)*sizeof(char));

string_result[0] = 0 ; // redundant?

i = 1;

while(i < argc) {
   strcat(string_result,argv[i]); // caution to concatenate argv string, memory of OS. 
   if(space_needed && (i+1) < argc)
        strcat(string_result, " "); //space so it looks better.
   i++;
}

//free pointer when done using it.
free(string_result);

Comments

0

Assuming sizoeof(char)==1, untested code!

#include <stdlib.h>
#include <string.h>

    int  i ; 
    int  size_total = 0;
    size_t *lens=(size_t *)malloc((argc)*sizeof(size_t));
    for (i=1;i < argc; i++) {
        lens[i]=strlen(argv[i]);
        size_total += lens[i]+1;
    }
    concatinated = (char*)malloc(size_total);
    char *start=concatinated;
    for (i=1;i < argc; i++) {
        memcpy(start, argv[i], lens[i]);
        start+=lens[i];
        *start=' ';
        start++;
    }
    start--;
    *start=0;
    free(lens);

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.