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?

Ricardo Altamirano
  • 14,650
  • 21
  • 72
  • 105
mko
  • 21,334
  • 49
  • 130
  • 191

5 Answers5

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.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • thanks for pointing out the \0 terminate fault in my code, and easier way. there is a close relationship between array and pointer – mko Jul 27 '12 at 12:34
  • 1
    @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. – Shahbaz Jul 27 '12 at 12:44
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;

}
duleshi
  • 1,966
  • 2
  • 21
  • 32
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

John Mitchell
  • 9,653
  • 9
  • 57
  • 91
  • cplusplus dot com is a terrible website. Please try to refer to other sources. – Shahbaz Jul 27 '12 at 12:19
  • @Shahbaz any better website for reference? – mko Jul 27 '12 at 12:37
  • Great explanation, But I need time to digest it – mko Jul 27 '12 at 12:39
  • @yozloy, [Wikipedia](http://en.wikipedia.org/wiki/Pointer_%28computer_programming%29)? Most of google search results with "C pointer", such as [this](http://cslibrary.stanford.edu/106/). – Shahbaz Jul 27 '12 at 12:43
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...

bph
  • 10,728
  • 15
  • 60
  • 135
  • 1
    That should generate a warning unless you declare it as `const char *` rather than just `char *`. – Paul R Jul 27 '12 at 12:20
  • This looks handy, but I think it involve more deeper topic like static array – mko Jul 27 '12 at 12:36
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.

Maverick
  • 91
  • 3
  • 10