下面的代码中有什么错误,应该如何更正

What is the error in the code below and how should it be corrected?

本文关键字:何更正 错误 什么 代码      更新时间:2023-10-16

下面代码中的错误是什么?应该如何更正?

my_struct_t *bar;
/* ... do stuff, including setting bar to point to a defined my_struct_t object ... */
memset(bar, 0, sizeof(bar));

memset的最后一个参数不对。

sizeof(bar)是指针的大小。

你需要一个物体的大小。使用sizeof(*bar)

memset(bar, 0, sizeof(*bar));