How do I create an array of strings in C?

前端 未结 14 2674
野的像风
野的像风 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条回答
  •  旧时难觅i
    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.

提交回复
热议问题