使用 memset 初始化指针元素的二维数组

发布于 2024-07-10 13:12:17 字数 289 浏览 4 评论 0原文

我有一个这些结构定义

typedef struct my_s {
   int x;
   int y;
} my_T;

typedef struct your_s {
    my_T * x;
} your_T;

your_T array[MAX_COL][MAX_ROW];

要将数组的指针初始化为 NULL,我可以这样做吗:

memset (array, 0, sizeof(array))

这对我来说看起来不正确。

I have a these structures definitions

typedef struct my_s {
   int x;
   int y;
} my_T;

typedef struct your_s {
    my_T * x;
} your_T;

your_T array[MAX_COL][MAX_ROW];

To initialize the array's pointer to NULL, can I do:

memset (array, 0, sizeof(array))

this does not look right to me.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心碎无痕… 2024-07-17 13:12:17

最简单的是

your_T array[MAX_COL][MAX_ROW] = {{{0}}};

easiest is

your_T array[MAX_COL][MAX_ROW] = {{{0}}};
剑心龙吟 2024-07-17 13:12:17
typedef struct my_s {
   int x;
   int y;
} my_T;

typedef struct your_s {
    my_T * x;
} your_T;

your_T array[MAX_COL][MAX_ROW];

您无法使用 memset 将 your_T 的指针初始化为 null,因为没有指定空指针的位模式全部由空位组成。 但是您可以像这样创建数组:

your_T array[MAX_COL][MAX_ROW] = {{}};

元素将被默认初始化,这意味着对于指针,该指针将包含空指针值。 如果您的数组是全局的,您甚至不必关心。 那么默认情况下就会发生这种情况。

typedef struct my_s {
   int x;
   int y;
} my_T;

typedef struct your_s {
    my_T * x;
} your_T;

your_T array[MAX_COL][MAX_ROW];

You cannot initialize the pointers of your_T to null using memset, because it is not specified that a null pointer has its bit pattern all consisting of null bits. But you can create your array like this:

your_T array[MAX_COL][MAX_ROW] = {{}};

The elements will be default initialized, which means for a pointer that the pointer will contain a null pointer value. If your array is global, you don't even have to care. That will happen by default then.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文