0

I've tried this: char **foo, which is is a string array,
but warnings generate, when I try to assign values to it:
char **baz = {"Hello,", "world!"}
So, ...

  • how to create a dynamic string (char *) array in C, without specifying the size?
  • how do to assign values to a char ** array?

NOTE:

  • char *not_this[50] isn't the answer
  • this is my 2nd question on Stackoverflow
Shoury
  • 3
  • 3
  • 1
    If you don't know the size of an array, how do you know if you can safely assign values to it? – Andrew Henle Jul 01 '21 at 13:19
  • 2
    You cannot allocate memory without specifying size in some way. – bereal Jul 01 '21 at 13:21
  • Use dynamic allocation to allocate memory for the array. Surely _any_ introduction to C covers the topic. – KamilCuk Jul 01 '21 at 13:22
  • @bereal But you don't need to do `char str[50]`, you can do `char *str`, where you don't specify the size, but how do you do that for string arrays? – Shoury Jul 01 '21 at 13:24
  • 1
    There is no such thing as "dynamic strings" in C. There even is no real string type in C. Strings in C are null terminated arrays of `char`. That's about all. There are some string helper functions in `string.h` and `scanf`/`printf` family of functions support input and output of C strings. – Jabberwocky Jul 01 '21 at 13:24
  • You can do `char *baz[] = {"Hello,", "world!"};` and `baz` will be an array of 2 elements. Is that what you want? – Kevin Jul 01 '21 at 13:26
  • 2
    @Shoury `char *str;` just declares a pointer that points nowhere until you assign something to it. What are you _actually_ trying to achieve? Furthermore you write _"but warnings generate, when I try to assign values to it"._ Show that code as a [mcve]. Read this: [ask] – Jabberwocky Jul 01 '21 at 13:26
  • 1
    Please define *string array* because there is no such concept in C. You probably comes from another language and don't use the correct wording. You may refer to *array of char* or *array of pointers to char* or similar concept. Strings in C are just null terminated array of char. Of course one could write a C library to define *string* just as they like. This is not what I would call *C string*. – fpiette Jul 01 '21 at 13:29
  • @Kevin But how do I later add an Element to it? – Shoury Jul 01 '21 at 13:33
  • 2
    `malloc()`,`realloc()` on `char **` should do it. – alex01011 Jul 01 '21 at 13:51

3 Answers3

1

You can create an array of char * as follows:

char *baz[] = {"Hello,", "world!"};
dbush
  • 205,898
  • 23
  • 218
  • 273
  • But is there a way to add elements to it, later? – Shoury Jul 01 '21 at 13:44
  • 3
    @Shoury The size of an array is fixed at the time it is declared, either explicitly or implicitly via number of initializers. If you want an array whose size can change you need to allocate it dynamically with `malloc` and resize it with `realloc`. – dbush Jul 01 '21 at 13:45
1

C is willing to automatically size, allocate, and initialize arrays for you. With one exception, however, there is no way to automatically initialize a pointer that points to some other array.

Here is automatic sizing and allocation of an array:

int a[] = {1, 2, 3};

The brackets [] are empty -- you don't have to explicitly specify the size -- because the compiler can see from the initializer that the array should have size 3.

Here is another example, using char:

char s1[] = {'H', 'e', 'l', 'l', 'o', 0};

Here is a much more convenient shortcut form:

char s2[] = "world";

Here is the exception I was talking about:

char *s3 = "hidden";

In this case, the compiler allocates a hidden, unnamed array containing the string "hidden", then initializes s3 to point to it. This is just about perfectly equivalent to the more explicit setup

char a2[] = "hidden";
char *s3a = a2;

except for the visibility of the intermediate array a2.

Putting all this together, you can initialize an array of pointers to char like this:

char *strarray[] = { "Hello", "world" };

This is equivalent (again, except for the visibility of the actual arrays holding the strings) to the more explicit

char a3[] = "Hello";
char a4[] = "world";

char *strarray2[] = { a3, a4 };

But you can not directly do anything along the lines of

char **dynstrarray = { ... };

The reason you can't do it is because dynstrarray is fundamentally a pointer, and there's no general mechanism in C to automatically allocate something for a pointer like this to point to. Again, the only time C will automatically allocate something for a pointer to point to is the special case for pointer to char, as demonstrated by s3 above.

So if you want to initialize a pointer like dynstrarray, your only choice is to do it explicitly, yourself. You could initialize it to point to an array of pointers to char that you had already constructed, like this:

char a3[] = "Hello";
char a4[] = "world";
char *strarray2[] = { a3, a4 };
char **dynstrarray[] = strarray2;

Or, more commonly, you would use malloc to dynamically allocate some memory for dynstrarray to point to. In that case (but only in that case) you would also have the option of resizing dynstrarray later, using realloc.

Steve Summit
  • 45,437
  • 7
  • 70
  • 103
-1

have a look at this answer. You then only need to swap every occurence of int for char*.

Natively there are no dynamically sized arrays in C (if this is what you want) but you can create them on your own as shown by the linked answer.

HannesH
  • 932
  • 2
  • 12