0

I'm a newbie in C so please excuse some of my mistakes. I'm wondering if there is a possible way to store multiple string/array values in one string? Here is an example to give you an idea of what I am trying to do.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
const char *lister[] = {"H", "E", "LL", "O"}; //forgot brackets, fixed now

int main()
{
    char *mystring[] = ("%s%s", lister[1], lister[2]);
    printf ("%s\n", mystring);
    return 0;
}

Not sure if that makes any sense at all, but looking for ways to store string values into another string or array. Thanks! :) EDIT: strncat fixes this problem, if you encountered the same problem then go to http://www.tutorialspoint.com/c_standard_library/c_function_strncat.htm to learn more, thanks to computerfreaker for suggesting strncat!

5
  • You can look into strcat or strncat. Both functions append (conCATenate) one string to another. strcat appends one entire string to another; strncat is slightly safer because you specify the number of characters from the second string to append to the first. Commented Mar 14, 2014 at 12:29
  • The mystring part won't work - use strcpy and strcat to copy and contatenate strings. Make sure you allocate a buffer first. Commented Mar 14, 2014 at 12:30
  • 1
    @computerfreaker you should probably make your comment an answer so the OP can accept it. Commented Mar 14, 2014 at 12:53
  • 1
    @Mauren Done. Thanks for flagging me, I wouldn't have realized op opted to work with my suggestion otherwise! Commented Mar 14, 2014 at 13:02
  • char multistring[] = "one\0two\0three"; printf("%s, %s, %s\n", multistring, multistring + 4, multistring + 8); Commented Mar 14, 2014 at 13:03

4 Answers 4

3

You are missing a pair of square brackets:

const char *lister[] = {"H", "E", "LL", "O"};
//                ^^
//               Here

To put several strings into one with a format string you can use sprintf, like this:

// Prepare the buffer for the output
char buf[100];
// The format string and parameters come from your code.
sprintf(buf, "%s%s", lister[1], lister[2]);

Note that your program would produce the values of the second and the third string, because the initial index in a C array is zero, not one.

Sign up to request clarification or add additional context in comments.

Comments

2

you can use sprintf for coping multiple string to one string with your own format also.

sprintf(destination_string, "%s----%s", source_string1,source_string2);

1 Comment

Splendid, seems like the shortest and most readable way to do this.
2

This const char *lister = {"H", "E", "LL", "O"}; should be either this:

const char **lister = {"H", "E", "LL", "O"};

or

const char *lister[] = {"H", "E", "LL", "O"};

Comments

1

You should look into strcat or strncat. Both functions append (conCATenate) one string to another.

strcat appends one entire string to another; you're required to make sure the destination buffer is big enough to hold both strings as well as a trailing null character (\0).

strncat is slightly safer because you specify the number of characters from the second string to append to the first, which means you can always be sure your buffer is big enough to hold everything you need it to.

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.