Search code examples
ccalloc

What does calloc that takes an assignment operator for size_t n do?


I am just checking out some leetcode submissions and came across this assignment for the 2sum soution:

int* twoSum(int* nums, int numsSize, int target, int* returnSize){
    int* res = calloc((*returnSize = 2), sizeof(int));
    ...
}

Is this saying "res is a pointer to an integer block of memory storing 2 int types initialised to 0" ? So the equivalent of:

int* res = calloc(2, sizeof(int));

Or is it something else?


Solution

  • It isn't "taking an assignment operator for size_t" exactly. Rather, it's taking the result of the assignment operation for size_t.

    For example

    (a = 2)

    Puts the value 2 in a and returns that assigned value. So you could also do something like:

    int a, b;
    b = (a = 2);
    

    Here, b would get the result of the operation (a = 2), which is 2.

    That's what's happening in your case. The first argument passed to calloc is the result of the assignment (*returnSize = 2), which is 2.