0

I am trying to figure out how to create an array of strings (considering I know the max length of each string).

char** strings = NULL;
strings = malloc (5*sizeof(char*));

Once I did that, how can I just fill the array without the need to allocate each string separately? Lets say I know the max length of a string is 20, how to I set it?

After the allocation of the string I wish to do the following:

strings[0] = "string";
strings[1] = "another string";

etc.

Thanks

7
  • 2
    char (*strings)[20+1] = malloc(5*sizeof(*strings)); strcpy(strings[0], "string");... Commented Aug 20, 2017 at 19:52
  • See stackoverflow.com/questions/14097651/… Commented Aug 20, 2017 at 19:54
  • 1
    If you are assigning each (literal) string pointer manually, then you know how many there are, so dynamic allocation is not needed. You could achieve the same thing like this: char *strings[5] = { "string", "another string", }; Commented Aug 20, 2017 at 19:57
  • 1
    what is the source of your strings (constant, stream, file, string variable, ...)? Commented Aug 20, 2017 at 20:02
  • 1
    I'm reading it from a file Commented Aug 20, 2017 at 20:08

2 Answers 2

1

You can declare an array of pointers to char and then assign string literals to those pointers

char *strings[5]; 

strings[0] = "string";
strings[1] = "another string"; 
/* ... */ 

But note that, these strings will be immutable.

You can also use an array of char arrays

char strings[5][20];    // As you know max length of string is 20
strcpy(strings[0], "string");
strcpy(strings[1], "another string");
/* ... */

One of the advantage of latter is strings will be mutable.

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

2 Comments

Thanks, I'll try to use it. One more question. Is it possible to use your code but to first initialize all strings to NULL?
NULL is used to initialize pointers, so initialising all pointers to NULL can be done in first snippet by char *strings[5] = {NULL}. This is not possible in second snippet as strings[i] is an array, not a pointer. Though, char strings[5][20] = { {'\0'} }; will make all strings empty.
0

If you know the maximum size of each line, and if you know the maximum number of lines, you could simply define a two-dimensional array of chars, i.e. char arr[5][20+1]. Then you will have reserved space for up to 5 lines, each comprising up to 20 characters (+ null char). And you could also define a type alias representing such a line (if you like):

#define MaxLineLength 20
typedef char Line[MaxLineLength+1];

int main() {

    Line input = { 0 };
    scanf("%20s", input);

    Line a[5] = { 0 };
    strcpy(a[0], input);
    strcpy(a[1], "string1");

    return 0;
}

7 Comments

@BLUEPIXY: I cannot follow the link from my environment; Can you describe the problem with the code?
1) typedef char Line[MaxLineLengh+1]; : variably modified 'Line' at file scope It must be a constant in C.
2) Line input = {}; ISO C forbids empty initializer braces This is GCC extension (or C++ ?).
I guess you are using C++ as C.
@BLUEPIXY: Thanks for the comments. I'm using Apple LLVM with C99 [-std=C99], which accepted the code in my original post. But I usually have not set `-Wpedantic'. When doing so I get at least compiler warnings for the issues you posted. Again, thanks!
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.