c array of strings

In C/C++, a string is a 1-D array of characters and an array of string in C is a 2D array of characters. This comes quite handy in C++. There are 3 ways in which an Array of Strings in C or C++ can be created.

    Using 2D array(Both C and C++): This method is useful for shuffling, comparing and accessing characters randomly.
    Syntax:

Example:

Drawbacks:

  • Both the number of Strings and Size of String are fixed.
  • A 2D array is allocated, whose second dimension is equal to maximum sized string which causes wastage of space.
  • Using string Keyword (Only in C++): In this method, size of the string is not fixed, hence space is saved.
    Syntax:

    Example:

    Drawback: The array is of fixed size.
    Using Vectors(Only C++): STL Container Vector can be used to dynamically allocate array.

    Syntax:

    Example:

    Conclusion: Out of all the three methods, Vector seems to be the best way for creating an array of Strings in C++.

    This article is contributed by Kartik Ahuja. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

    Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

    In this section we will see how to define an array of strings in C++. As we know that in C, there was no strings. We have to create strings using character array. So to make some array of strings, we have to make a 2-dimentional array of characters. Each rows are holding different strings in that matrix.

    In C++ there is a class called string. Using this class object we can store string type data, and use them very efficiently. We can create array of objects so we can easily create array of strings.

    After that we will also see how to make string type vector object and use them as an array.

    Содержание
    1. Example
    2. Output
    3. 14 Answers 14

    Example

    Output

    Now let us see how to create string array using vectors. The vector is available in C++ standard library. It uses dynamically allocated array.

    I am trying to create an array of strings in C. If I use this code:

    gcc gives me «warning: assignment from incompatible pointer type». What is the correct way to do this?

    edit: I am curious why this should give a compiler warning since if I do printf(a[1]); , it correctly prints «hmm».

    14 Answers 14

    If you don’t want to change the strings, then you could simply do

    When you do it like this you will allocate an array of two pointers to const char . These pointers will then be set to the addresses of the static strings «blah» and «hmm» .

    If you do want to be able to change the actual string content, the you have to do something like

    This will allocate two consecutive arrays of 14 char s each, after which the content of the static strings will be copied into them.

    There are several ways to create an array of strings in C. If all the strings are going to be the same length (or at least have the same maximum length), you simply declare a 2-d array of char and assign as necessary:

    You can add a list of initializers as well:

    This assumes the size and number of strings in the initializer match up with your array dimensions. In this case, the contents of each string literal (which is itself a zero-terminated array of char) are copied to the memory allocated to strs. The problem with this approach is the possibility of internal fragmentation; if you have 99 strings that are 5 characters or less, but 1 string that’s 20 characters long, 99 strings are going to have at least 15 unused characters; that’s a waste of space.

    Instead of using a 2-d array of char, you can store a 1-d array of pointers to char:

    Note that in this case, you’ve only allocated memory to hold the pointers to the strings; the memory for the strings themselves must be allocated elsewhere (either as static arrays or by using malloc() or calloc() ). You can use the initializer list like the earlier example:

    Instead of copying the contents of the string constants, you’re simply storing the pointers to them. Note that string constants may not be writable; you can reassign the pointer, like so:

    But you may not be able to change the string’s contents; i.e.,

    may not be allowed.

    You can use malloc() to dynamically allocate the buffer for each string and copy to that buffer:

    Declares a as a 2-element array of pointers to 14-element arrays of char.

  • Оцените статью