Why is returnSize needs to be assigned ?


  • 0
    T
    int* singleNumber(int* nums, int numsSize, int* returnSize) {
        int i, *ret = calloc(*returnSize, sizeof(int));
        for (i = 0; i < numsSize; i++) {
            ret[0] ^= nums[i];
        }
        for (i = 0; i < numsSize; i++) {
             if (nums[i] & ret[0] & -ret[0]) {
                ret[1] ^= nums[i];
            }
        }
        ret[0] ^= ret[1];
        return ret;
    }
    

    This code doesn't work, only if I write it like :
    int i, *ret = calloc(*returnSize = 2, sizeof(int));

    why I need to assign returnSize here?


  • 0
    C

    returnSize is a pointer save the size of your return array.
    you should make it points to 2 before you use it in the alloc function.


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.