为结构体指针分配空间

发布于 2024-08-21 12:52:41 字数 472 浏览 7 评论 0原文

在另一篇文章链接文本中,

我试图用结构做同样的事情,但我的 sizeof 运算符有问题,所以在整数情况下,我这样做了:

size_t totalMem = rows * sizeof(int *) + rows * cols * sizeof(int));

在结构情况下,我这样做了:

size_t totalMem = (rows * sizeof(struct TEST *)) + (rows * cols * sizeof(struct TEST));

但我收到错误: sizeof 对不完整类型的 struct TEST 的应用无效。

有什么想法吗?谢谢。

In another post link text

I am trying to do the same thing with a struct, but I have a problem with my sizeof operator, so in the integer case, I did this:

size_t totalMem = rows * sizeof(int *) + rows * cols * sizeof(int));

And in the struct case I did this:

size_t totalMem = (rows * sizeof(struct TEST *)) + (rows * cols * sizeof(struct TEST));

But I get the error: Invalid application of sizeof to incomplete type of struct TEST.

Any thoughts? Thanks.

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

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

发布评论

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

评论(3

妥活 2024-08-28 12:52:41

因为您正在定义结构,所以

 typedef struct { ... } TEST;

您应该始终将结构引用为 TEST,而不是 struct TEST,即使用

 size_t totalMem = (rows * sizeof(TEST*)) + (rows * cols * sizeof(TEST));

要使用 struct TEST 您需要实际命名结构,即

 struct TEST { ... };

您可以允许两者

 typedef struct TEST { ... } TEST;

Because you're defining the struct as

 typedef struct { ... } TEST;

you should always refer to the struct as TEST, not struct TEST, i.e. use

 size_t totalMem = (rows * sizeof(TEST*)) + (rows * cols * sizeof(TEST));

To use struct TEST you need to actually name the struct, i.e.

 struct TEST { ... };

You can allow both with

 typedef struct TEST { ... } TEST;
つ可否回来 2024-08-28 12:52:41
#include <stdio.h>
#include <stdlib.h>

#include "C2_Assignment_5_Exercise_2_Driver.h"

作为我的 size_t 声明的一部分。然后.h文件中的定义是:

#ifndef C2_ASSIGNMENT_5_EXERCISE_2_DRIVER_H
#define C2_ASSIGNMENT_5_EXERCISE_2_DRIVER_H

typedef struct
{
   char charObj;
   short shortObj;
   long longObj;
   double doubleObj;
} TEST;

#endif
#include <stdio.h>
#include <stdlib.h>

#include "C2_Assignment_5_Exercise_2_Driver.h"

as part of my size_t statement. And then the definition in the .h file is:

#ifndef C2_ASSIGNMENT_5_EXERCISE_2_DRIVER_H
#define C2_ASSIGNMENT_5_EXERCISE_2_DRIVER_H

typedef struct
{
   char charObj;
   short shortObj;
   long longObj;
   double doubleObj;
} TEST;

#endif
南烟 2024-08-28 12:52:41

typedef struct MYTEST
{
   char charObj;
   short shortObj;
   long longObj;
   double doubleObj;
} TEST;

size_t totalMem = (rows * sizeof(struct TEST *)) + (rows * cols * sizeof(struct TEST));

更改

size_t totalMem = (rows * sizeof(struct MYTEST *)) + (rows * cols * sizeof(struct MYTEST));

Change:

typedef struct MYTEST
{
   char charObj;
   short shortObj;
   long longObj;
   double doubleObj;
} TEST;

and

size_t totalMem = (rows * sizeof(struct TEST *)) + (rows * cols * sizeof(struct TEST));

to

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