I want to create an array of strings. Here is the code:
#include <stdio.h>
int main(void){
char str1[] = {'f','i'};
char str2[] = {'s','e'};
char str3[] = {'t','h'};
char arry_of_string[] = {str1,str2,str3};
printf("%s\n",arry_of_string[1]);
return 0;
}
This is the line that doesn’t work:
char arry_of_string[] = {str1,str2,str3};
How do I correct it?
If you would like to create an array of strings, you are missing an asterisk, and terminating zeros:
There is an easier way of doing the individual strings, too:
When you use the
char x[] = "..."construct, the content of your string literal (which includes a terminating zero) is copied into memory that you are allowed to write, producing the same effect aschar x[] = {'.', '.', ... '\0'}construct.