为 size_t n 采用赋值运算符的 calloc 有何作用?
我只是检查了一些 leetcode 提交内容,并遇到了 2sum 解决方案的这个分配:
int* twoSum(int* nums, int numsSize, int target, int* returnSize){
int* res = calloc((*returnSize = 2), sizeof(int));
...
}
这是否是说“res 是一个指向存储 2 个 int 类型的整数内存块的指针,初始化为 0”?所以相当于:
int* res = calloc(2, sizeof(int));
或者是别的什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它并不完全是“为 size_t 采用赋值运算符”。相反,它获取 size_t 的赋值操作的结果。
例如
(a = 2)
将值 2 放入 a 中并 返回指定的值。所以你也可以这样做:
这里,
b
将得到操作(a = 2)
的结果,即2。这就是你的情况发生的情况。传递给
calloc
的第一个参数是赋值(*returnSize = 2)
的结果,即 2。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:
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.