How do I create an array of strings in C?

前端 未结 14 2514
野的像风
野的像风 2020-11-22 14:43

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

char (*a[2])[14];
a[0]=\"blah\";
a[1]=\"hmm\";

gcc gives me \"warning:

相关标签:
14条回答
  • 2020-11-22 15:18

    If you don't want to keep track of number of strings in array and want to iterate over them, just add NULL string in the end:

    char *strings[]={ "one", "two", "three", NULL };
    
    int i=0;
    while(strings[i]) {
      printf("%s\n", strings[i]);
      //do something
      i++;
    };
    
    0 讨论(0)
  • 2020-11-22 15:20

    I was missing somehow more dynamic array of strings, where amount of strings could be varied depending on run-time selection, but otherwise strings should be fixed.

    I've ended up of coding code snippet like this:

    #define INIT_STRING_ARRAY(...)          \
        {                                   \
            char* args[] = __VA_ARGS__;     \
            ev = args;                      \
            count = _countof(args);         \
        }
    
    void InitEnumIfAny(String& key, CMFCPropertyGridProperty* item)
    {
        USES_CONVERSION;
        char** ev = nullptr;
        int count = 0;
    
        if( key.Compare("horizontal_alignment") )
            INIT_STRING_ARRAY( { "top", "bottom" } )
    
        if (key.Compare("boolean"))
            INIT_STRING_ARRAY( { "yes", "no" } )
    
        if( ev == nullptr )
            return;
    
        for( int i = 0; i < count; i++)
            item->AddOption(A2T(ev[i]));
    
        item->AllowEdit(FALSE);
    }
    

    char** ev picks up pointer to array strings, and count picks up amount of strings using _countof function. (Similar to sizeof(arr) / sizeof(arr[0])).

    And there is extra Ansi to unicode conversion using A2T macro, but that might be optional for your case.

    0 讨论(0)
  • 2020-11-22 15:23

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

    const char *a[2];
    a[0] = "blah";
    a[1] = "hmm";
    

    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

    char a[2][14];
    strcpy(a[0], "blah");
    strcpy(a[1], "hmm");
    

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

    0 讨论(0)
  • 2020-11-22 15:23

    The string literals are const char *s.

    And your use of parenthesis is odd. You probably mean

    const char *a[2] = {"blah", "hmm"};
    

    which declares an array of two pointers to constant characters, and initializes them to point at two hardcoded string constants.

    0 讨论(0)
  • 2020-11-22 15:24

    Your code is creating an array of function pointers. Try

    char* a[size];
    

    or

    char a[size1][size2];
    

    instead.

    See wikibooks to arrays and pointers

    0 讨论(0)
  • 2020-11-22 15:26

    Ack! Constant strings:

    const char *strings[] = {"one","two","three"};
    

    If I remember correctly.

    Oh, and you want to use strcpy for assignment, not the = operator. strcpy_s is safer, but it's neither in C89 nor in C99 standards.

    char arr[MAX_NUMBER_STRINGS][MAX_STRING_SIZE]; 
    strcpy(arr[0], "blah");
    

    Update: Thomas says strlcpy is the way to go.

    0 讨论(0)
提交回复
热议问题