简体   繁体   中英

Given a pointer to an array of structs how would i split it into two arrays of the same size

So essentially if i have a function that is supposed to split an struct array into two halves and do stuff with it, how would i make two separate arrays and pass them into another function. This is the following code that I have:

int function(struct Values *v, int lengthOfArray)
{
    int midpoint = lengthOfArray /2;

    // no clue how to make these
    struct Values firstHalf*;
    struct Values secondHalf*;

    int i = helperFunction(firstHalf, midpoint);
    int j = helperFunction(secondHalf, lengthOfArray - midpoint);
    // remaining code omitted
}

int helperFunction(struct Values v*, int lengthOfArray)
{
    // code omitted
}

You don't need to split the array per se. Just pass in the start of each array to the helper function. The helper function is also given the length so together with the start address that completely defines the array bounds.

firstHalf = v; secondHalf = &v[midpoint];

or

firstHalf = v; secondHalf = v + midpoint;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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