1

Is there any way to get more than 1 string in one array?

#include <stdio.h>
int main (void)
{
        char str[4] = {"Linux", "Ubuntu", "Arch", "Void"};
        for (int i = 0; i < 4; i++) {
                printf("%d", str[i]);
                printf("%s", str[i]);
        }
        printf("%s", str);
}

I just trying to do it. But didn't get it?

roschach
  • 8,390
  • 14
  • 74
  • 124
Lidan Suidan
  • 31
  • 1
  • 5
  • Change the `str[4]` to `*str[4]`. Also, instead of `printf("%d", str[i])` I would do `printf("%d", i)`. Also, that `printf("%s", str);` at the end makes no sense, I'd remove that. – Blaze Jan 11 '19 at 08:10
  • wow thanks, but what did `*str[]` mean? the (*)mark on an Array? – Lidan Suidan Jan 11 '19 at 08:16
  • 1
    In addition to everything what @Blaze said, you also need to change `char` to `const char`, i.e. you should have `const char *str[4]`. – Duck Dodgers Jan 11 '19 at 08:17
  • 1
    Possible duplicate of [How do I create an array of strings in C?](https://stackoverflow.com/questions/1088622/how-do-i-create-an-array-of-strings-in-c) – Duck Dodgers Jan 11 '19 at 08:18
  • @LidanSuidan With the added `*` it's not an array of `char`, but an array of `char` pointers. Those represent strings (they point to the place where the string is stored at). The string literals, such as `"Linux"`, cause the actual strings to be in your program's binary (the actual strings aren't stored in `str`) and `"Linux"` just decays into such a `char` pointer. – Blaze Jan 11 '19 at 08:20
  • Thank you so much @Blaze – Lidan Suidan Jan 11 '19 at 08:24

2 Answers2

4

How store multiple string or create an array of strings in C, by using 1D array?

The short answer is: You can't

A string in C is by itself a char array (with a zero termination) so there is no way to have multiple strings in a 1D array.

You can make it a 2D array like:

int main()
{
  // Make a 2D array to store
  // 4 strings with 9 as max strlen
  char str[4][10] = {"Linux", "Ubuntu", "Arch", "Void"};

  for (int i=0; i<4; ++i) printf("%s\n", str[i]);
  return 0;
}

Another approach is to use a 1D array of char pointers to string literals - like:

int main()
{
  // Make a 1D array to store
  // 4 char pointers
  char *str[4] = {"Linux", "Ubuntu", "Arch", "Void"};

  for (int i=0; i<4; ++i) printf("%s\n", str[i]);
  return 0;
}

but notice that the strings are not saved in the array. The compiler place the strings somewhere in memory and the array just holds pointers to those strings.

Also notice that in the second example you are not allowed to modify the strings later in the program. In the first example you are allowed to change the strings after the initialization, e.g. doing strcpy(str[0], "Centos");

BTW: This may be of interrest Are string literals const?

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
  • You *can* store multiple 0-terminated strings in the same array - the issue is accessing anything beyond the initial string. You'd need pointers into the original array, or something that could read past a terminator, etc. – John Bode Jan 11 '19 at 15:03
  • @JohnBode true... but multiple "strings" saved like that ain't C strings. A C string ends at the first zero-termination. Anything after the termination just isn't part of the string. I can't think of any use case where it would make sense to do something like that. – Support Ukraine Jan 11 '19 at 17:17
  • Sure they are - a string is simply a sequence of character values including a 0-valued terminator. How those sequences are mapped onto storage doesn't determine whether they are or are not strings. Consider this - `char strs[] = "foo\0bar"; char *s1 = strs; char *s2 = &strs[4];` - what happens if you pass `s2` to any library function expecting a string? Will it work as expected? If not, why not? How is that effectively any different from `char strs[][3] = {"foo", "bar"}; char *s1 = strs[0]; char *s2 = strs[1];`? – John Bode Jan 11 '19 at 17:33
0

It is possible to store multiple strings in a 1D array - the issue is accessing anything beyond the initial string. For example:

char strs[] = “foo\0bar\0bletch\0blurga”;

The 1D array strs contains 4 strings, but if we pass strs to any library function, only ”foo” will be used.. We would need to maintain a separate array of pointers into strs to access anything beyond the first string:

char *ptrs[] = {strs, &strs[4], &strs[8], &strs[15]};

If you need your strings to be contiguous in memory then this is a valid aproach, but it is cumbersome, and will be a massive pain in the ass if you need to update any of them. Better to use a 2D array.

John Bode
  • 119,563
  • 19
  • 122
  • 198