YohanRoth
YohanRoth

Reputation: 3263

How to perform static initialization of an array of strings in C

I am trying to assign string values to 2d array in C. As of now I have the following code but it does not work:

char array[9][6];
    array[0][] = "[0x0a]";
    array[1][] = "[0x09]";
    array[2][] = "[0x0b]";
    array[3][] = "[0x08]";
    array[4][] = "[0x0d]";
    array[5][] = "[0x0c]";
    array[6][] = "[0x07]";
    array[7][] = "[0x5c]";
    array[8][] = "[0x22]";

How would I do it? Thanks

Upvotes: 0

Views: 168

Answers (3)

Vlad from Moscow
Vlad from Moscow

Reputation: 311068

Arrays have no assignment operator. Either set values when an array is defined, for example:

char array[9][6] =
{
    "[0x0a]", "[0x09]", "[0x0b]", "[0x08]", "[0x0d]", 
    "[0x0c]", "[0x07]", "[0x5c]", "[0x22]"
};

Or by using the standard function strcpy:

char array[9][7];
    strcpy( array[0], "[0x0a]" );
    strcpy( array[1], "[0x09]" );
    strcpy( array[2], "[0x0b]" );
    strcpy( array[3], "[0x08]" );
    strcpy( array[4], "[0x0d]" );
    strcpy( array[5], "[0x0c]" );
    strcpy( array[6], "[0x07]" );
    strcpy( array[7], "[0x5c]" );
    strcpy( array[8], "[0x22]" );

In the last case the size of an element of the array shall be equal to 7 that to include the terminating zero or the size can be equal to 6 but you have to use function strncpy instead of strcpy

Take into account that such an initialization of the array showed above is valid only in C. In C++ you have to specify the size of an element equal to 7 that to include the terminating zero of string literals.

Upvotes: 1

Cloud
Cloud

Reputation: 19333

There are two ways to do this. Either via static initialization using string literals like you are above:

Code Listing


#include <stdio.h>

int main(void) {
   int i;
   char array[9][7] = {
      "[0x0a]",
      "[0x09]",
      "[0x0b]",
      "[0x08]",
      "[0x0d]",
      "[0x0c]",
      "[0x07]",
      "[0x5c]",
      "[0x22]"
   };
   for (i=0; i<9; i++) {
      printf("%d: %s\n", i, array[i]);
   }

   return 0;
}

Or, using an array of pointers like so:

Code Listing


#include <stdio.h>

int main(void) {
   int i;
   const char* array[9] = {
      "[0x0a]",
      "[0x09]",
      "[0x0b]",
      "[0x08]",
      "[0x0d]",
      "[0x0c]",
      "[0x07]",
      "[0x5c]",
      "[0x22]"
   };
   for (i=0; i<9; i++) {
      printf("%d: %s\n", i, array[i]);
   }

   return 0;
}

Sample Run


0: [0x0a]
1: [0x09]
2: [0x0b]
3: [0x08]
4: [0x0d]
5: [0x0c]
6: [0x07]
7: [0x5c]
8: [0x22]

Note that in the first example, all strings are the same length (you have a bug, they should be 7-chars long to accommodate the NULL / '\0' that terminates C-style strings), and all strings are read-write. In the second example, the strings don't need to be the same length, they can be different. They are also read-only, as are any string literals which are used in this fashion.

Recommended Reading


  1. Difference between declared string and allocated string, Accessed 2014-09-03, <https://stackoverflow.com/questions/16021454/difference-between-declared-string-and-allocated-string>

Upvotes: 1

inspector-g
inspector-g

Reputation: 4176

If you don't want to assign them statically, then use the strcpy() function:

#include <strings.h>

int main(int argc, char const* argv[])
{
    char array[9][7];
    strcpy(array[0], "[0x0a]");
    strcpy(array[1], "[0x09]");
    strcpy(array[2], "[0x0b]");
    strcpy(array[3], "[0x08]");
    strcpy(array[4], "[0x0d]");
    strcpy(array[5], "[0x0c]");
    strcpy(array[6], "[0x07]");
    strcpy(array[7], "[0x5c]");
    strcpy(array[8], "[0x22]");

    return 0;
}

Static assignment would look like this:

int main(int argc, char const* argv[])
{
    char array[9][7] = {
        "[0x0a]",
        "[0x09]",
        "[0x0b]",
        "[0x08]",
        "[0x0d]",
        "[0x0c]",
        "[0x07]",
        "[0x5c]",
        "[0x22]"
    };

    return 0;
}

(Also, the second dimension of your array should be 7, not 6.)

Upvotes: 4

Related Questions