简体   繁体   English

在C函数中通过指针传递结构数组以更新指针引用

[英]Pass array of struct by pointer in C function to update pointer reference

C++ allows pass by reference, but not in C. C++ 允许按引用传递,但在 C 中不允许。

Questions is, how can C function be used to update the reference to array of struct passed by pointer.问题是,如何使用 C 函数来更新对指针传递的结构数组的引用。

My question can be better explained with code.我的问题可以用代码更好地解释。

enum ch_numbers {
        CH_00,
        CH_01,
        CH_02,
        CH_03,
        CH_04,
        CH_05,
    };

    enum sen_numbers {
        SEN_0,
        SEN_1,
        SEN_2,
        SEN_3,
        SEN_4,
    };

    struct ch_sen_map {
        enum ch_numbers ch_num;
        enum sen_numbers sen_num;
    };

    struct ch_sen_map map1[] = {
        {CH_02, SEN_3},
        {CH_03, SEN_1},
        {CH_01, SEN_4},
    };

    struct ch_sen_map map2[] = {
        {CH_01, SEN_1},
        {CH_02, SEN_3},
        {CH_04, SEN_2},
    };

    void UpdateMap(struct ch_sen_map * map)
    {
        map = map1;
    }

    struct ch_sen_map * GetMap(void)
    {
        return map1;
    }

    void main()
    {
        int i;
        struct ch_sen_map *pMap;
        int mapsize = sizeof(map1)/sizeof(struct ch_sen_map);

        printf("\n Expected map1 (straight forward):");
        for (i=0; i<mapsize; i++) {
            printf("\n %d : %d", map1[i].ch_num, map1[i].sen_num);
        }

        pMap = map2;

        printf("\n Expected map2 :");
        for (i=0; i<mapsize; i++) {
            printf("\n %d : %d", pMap[i].ch_num, pMap[i].sen_num);
        }

        UpdateMap(pMap);  // Not working as I expect

        printf("\n Expected map1 :");
        for (i=0; i<mapsize; i++) {
            printf("\n %d : %d", pMap[i].ch_num, pMap[i].sen_num);
        }

        pMap = GetMap();

        printf("\n Expected map1 :");
        for (i=0; i<mapsize; i++) {
            printf("\n %d : %d", pMap[i].ch_num, pMap[i].sen_num);
        }

    }

And, the output is:而且,输出是:

 Expected map1 (straight forward):
 2 : 3
 3 : 1
 1 : 4
 Expected map2 :
 1 : 1
 2 : 3
 4 : 2
 Expected map1 :  < -- this is what I want to fix. 
 1 : 1   <-- These are map2 values, Not Map1 :( 
 2 : 3
 4 : 2
 Expected map1 :
 2 : 3
 3 : 1
 1 : 4

I basically want to rewrite/fix UpdateMap function such that it work like GetMap, except no return.我基本上想重写/修复 UpdateMap 函数,使其像 GetMap 一样工作,除了没有返回。 [I might still not be making sense]. [我可能仍然没有意义]。

I want the caller function define the map pointer *pMap (in this case, main).我希望调用函数定义映射指针 *pMap(在本例中为 main)。 When I pass this map pointer to a function (UpdateMap), the UpdateMap function would update the pointer value, to either point to map1 or map2.当我将此映射指针传递给函数 (UpdateMap) 时,UpdateMap 函数将更新指针值,以指向 map1 或 map2。 And more importantly, update the map pointer reference such that next when main function utilize map pointer, it points to the map updated by UpdateMap function.更重要的是,更新映射指针引用,以便接下来当主函数使用映射指针时,它指向由 UpdateMap 函数更新的映射。

In the above code, UpdateMap gets *pMap as function parameter, and its value can be modified to use within that function only.在上面的代码中,UpdateMap 获取 *pMap 作为函数参数,并且可以修改其值以仅在该函数中使用。

In your UpdateMap function:在您的UpdateMap函数中:

void UpdateMap(struct ch_sen_map * map)
{
    map = map1;
}

You change the value of map , which is local to the function, so changes to it aren't seen outside of the function.您更改了函数局部的map值,因此在函数外部看不到对其的更改。 You need to accept a pointer to a struct ch_sen_map * , ie a struct ch_sen_map ** , and dereference that pointer to change what it points to.您需要接受一个指向struct ch_sen_map *的指针,即struct ch_sen_map ** ,并取消引用该指针以更改它指向的内容。

So change your function to:因此,将您的功能更改为:

void UpdateMap(struct ch_sen_map **map)
{
    *map = map1;
}

And call it like this:并这样称呼它:

UpdateMap(&pMap);  

If you want a pointer to be updated, you need to pass a pointer to that pointer.如果要更新指针,则需要传递指向该指针的指针。

void UpdateMap(struct ch_sen_map ** map_ptr)
{
    *map_ptr = map1;
}

int main(void)
{

    struct ch_sen_map *pMap;

    UpdateMap(&pMap);
    // ...
}

By the way, main must always return int .顺便说一句, main必须始终返回int

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM