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
?
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;
}
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.