4

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?

5 Answers 5

6

If you would like to create an array of strings, you are missing an asterisk, and terminating zeros:

char str1[] = {'f','i','\0'};
char str2[] = {'s','e','\0'};
char str3[] = {'t','h','\0'};
char *arry_of_string[] = {str1,str2,str3};

There is an easier way of doing the individual strings, too:

char str1[] = "fi";
char str2[] = "se";
char str3[] = "th";
char *arry_of_string[] = {str1,str2,str3};

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 as char x[] = {'.', '.', ... '\0'} construct.

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

2 Comments

thanks for pointing out the \0 terminate fault in my code, and easier way. there is a close relationship between array and pointer
@yozloy, there is a close relationship between array and pointers, but make sure you don't make the common mistake of thinking they are the same.
2

you can use it like this:(notice that if you want to print an array of char, u must have it termitated by '\0')

int main()
{
    char str1[] = {'f','i','\0'};
    char str2[] = {'s','e','\0'};
    char str3[] = {'t','h','\0'};
    char* arry_of_string[] = {str1,str2,str3};

    for (int i=0;i<3;i++)
    {
        printf("%s\n",arry_of_string[i]);

    }
    return 0;

}

Comments

1

You are probably looking for a pointer here rather than a direct array.

char *arry_of_string[] = {str1,str2,str3};

An array is a collection of values, a pointer is a list of addresses containing values, so a char pointer is a pointer to the address of the arrays containing your strings (character arrays). and breathe

4 Comments

cplusplus dot com is a terrible website. Please try to refer to other sources.
@Shahbaz any better website for reference?
Great explanation, But I need time to digest it
@yozloy, Wikipedia? Most of google search results with "C pointer", such as this.
1

you could just use:

const char *array_of_string[] = {"fi", "se", "th"};

int i;
for (i=0;i<3;i++) {
    printf("%s\n", array_of_string[i]);
}

if you want to be concise...

2 Comments

That should generate a warning unless you declare it as const char * rather than just char *.
This looks handy, but I think it involve more deeper topic like static array
0

When you specify the string as

char* myString = "abcd";

It creates a pointer of type array and points to the base address or the 0th element of the character array. So myString points to a. Using a char* is useful because c provides a good way of printing out the entire array using

printf("%s",myString);

Also pointer is useful to use when you dont know or dont want to specify the length of the char array. Your question should be solved if you do this

  char *str1 = "fi";
  char *str2 = "se";
  char *str3 = "th";
  char* arry_of_string[] = {str1,str2,str3};
  int i;

  for (i=0;i<3;i++)
  {
    printf("%s\n",arry_of_string[i]);
  }
  return 0;

Feel free to mark the question answered if you are satisfied.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.