Search code examples
arrayscpointerspass-by-pointer

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


C++ allows pass by reference, but not in C.

Questions is, how can C function be used to update the reference to array of struct passed by pointer.

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. [I might still not be making sense].

I want the caller function define the map pointer *pMap (in this case, 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. 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.

In the above code, UpdateMap gets *pMap as function parameter, and its value can be modified to use within that function only.


Solution

  • 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.