Copying arrays of structs in C

后端 未结 6 971
后悔当初
后悔当初 2021-02-06 12:03

It\'s been a long since I don\'t use C language, and this is driving me crazy. I have an array of structs, and I need to create a function which will copy one array to another (

6条回答
  •  一个人的身影
    2021-02-06 12:41

    The compiler has no information about the size of the array after passing them as pointer into a function. So you often need a third parameter: The size of the arrays to copy.

    A solution (without any error checking) could be:

    void copySolution(struct group* a, struct group* b, size_t size) {
        memcpy(a, b, size * sizeof(*a));
    } 
    

提交回复
热议问题