2

I have this string that is presented in the form of character array temp. And I want to insert this character array to another array temp_list and then print out the contents of this array. In other words, storing many character arrays in a single array. Can anyone tell me if this is possible and how can I make it work?

This is an example of what I am trying to accomplish:

int main()
{
    char temp[5] = "begin";
    char temp_list [10];
    temp_list[0] = temp;

    for (int i = 0; i < strlen(temp_list); i++)
    {
        printf("Labels: %s,", temp_list[i]);
    }
}

When I run this program, it prints out gibberish.

Any form of guidance would be very appreciated. Thank you.

Edit:

Thank you for the answers. They are all really helpful. But I have another question... what if I have multiple character arrays that I want to insert to temp_list? Using strcpy multiple times seem to not work, since I am assuming the function basically replaces the entire content of the temp_list with the string passed with strcpy?

3
  • 2
    You need to make room for the null character. Commented Feb 25, 2019 at 15:32
  • You should enable all warnings and listen to them. temp_list[0] = temp; This is not how a string is copied. Commented Feb 25, 2019 at 15:33
  • Does that code compile? You have an assignment from char* to char which shouldn’t be ok. Commented Feb 25, 2019 at 15:33

2 Answers 2

5

There are a bunch of misconceptions regarding strings. Your array temp needs to be big enough to also store the null-terminator, so it needs a size of at least 6 in this case:

char temp[6] = "begin"; // 5 chars plus the null terminator

To copy the string, use strcpy:

char temp_list[10];
strcpy(temp_list, temp);

To print it, pass temp_list, not temp_list[i], also you don't need that loop:

printf("%s\n", temp_list);

The final program could look like this:

int main()
{
    char temp[6] = "begin";
    char temp_list[10];
    strcpy(temp_list, temp);
    printf("%s\n", temp_list);
    return 0;
}
Sign up to request clarification or add additional context in comments.

7 Comments

Thank you. But what if I would like to insert more than one character array? I guess, strcpy would no longer be an option, right? Since the function replaces the entire array with one string?
Depends on how you want to insert more. strcpy copies the string, but doesn't change anything after the null-terminator. You can also use memcpy to copy a fixed amount of bytes, and strcat lets you concatenate strings. For instance if after strcpy(temp_list, temp); you would call strcat(temp_list, "Hi!"); it would print beginHi!
I was thinking to make it like {"hello", "world"} sort of array. Is that not possible in C?
You can try: char temp[][10] = {"hello", "world"}; char temp_list[20]; strcpy(temp_list, temp[0]); strcat(temp_list, temp[1]);
@Tina In this case you probably want temp_list to be a 2D array, too, like this: char temp_list[2][20]; strcpy(temp_list[0], temp[0]); strcpy(temp_list[1], temp[1]); printf("%s %s\n", temp_list[0], temp_list[1]);
|
4

You have three problems here. First, temp is not big enough to hold the string "begin". Strings in C are null terminated, so this string actually takes up 6 bytes, not 5. So make temp big enough to hold this string:

char temp[6] = "begin";

Or better yet:

char temp[] = "begin";

Which sizes the array exactly as needed for the string. The second problem is here:

temp_list[0] = temp;

You're assigning an array (actually a pointer to the array's first element) to the first element of another array. That's a type mismatch of assigning a char * to a char. Even if the types matched, that's not how strings are copied. For that, use the strcpy function:

strcpy(temp_list, temp);

Finally, you're not printing the result correctly:

for (int i = 0; i < strlen(temp_list); i++)
{
    printf("Labels: %s,", temp_list[i]);
}

The %s format specifier expects a pointer to a char array in order to print a string, but you're passing in a single characters. Mismatching format specifiers invokes undefined behavior.

For printing single characters, use %c instead:

for (int i = 0; i < strlen(temp_list); i++)
{
    printf("Labels: %c,", temp_list[i]);
}

Or you can get rid of the loop and just print the whole string using %s:

printf("Labels: %s", temp_list);

2 Comments

One more thing: he is using incorrect format specifier to print char in printf.
@machine_1 Good catch. Updated.

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.