How do I dynamically allocate an array of strings in C?

前端 未结 4 862
生来不讨喜
生来不讨喜 2020-12-05 08:49

If I have the number of items in a var called \"totalstrings\" and a var called \"string size\" that is the string size of each item, how do I dynamically allocate an array

4条回答
  •  情书的邮戳
    2020-12-05 09:23

    Well, first you might want to allocate space for "array", which would be an array of char * that is "totalstrings" long.

    What would then be the starting and final indexes in "array"? You know the first one is 0; what's the last one?

    Then, for each entry in "array", you could (if you wanted) allocate one area of memory that is "stringsize+1" (why +1, pray tell me?) long, putting the starting address of that area -- that string -- into the correct member of "array."

    That would be a good start, imo.

提交回复
热议问题