c pointer to array of structs

后端 未结 4 2407
刺人心
刺人心 2020-12-05 05:13

I know this question has been asked a lot, but I\'m still unclear how to access the structs.

I want to make a global pointer to an array of structs:

         


        
4条回答
  •  死守一世寂寞
    2020-12-05 06:08

    The syntax you are looking for is somewhat cumbersome, but it looks like this:

    // Declare test_array_ptr as pointer to array of test_t
    test_t (*test_array_ptr)[];
    

    You can then use it like so:

    test_array_ptr = &array_t1;
    (*test_array_ptr)[0] = new_struct;
    

    To make the syntax easier to understand, you can use a typedef:

    // Declare test_array as typedef of "array of test_t"
    typedef test_t test_array[];
    ...
    // Declare test_array_ptr as pointer to test_array
    test_array *test_array_ptr = &array_t1;
    (*test_array_ptr)[0] = new_struct;
    

    The cdecl utility is useful for deciphering complex C declarations, especially when arrays and function pointers get involved.

提交回复
热议问题