C - 如何将结构保存到内存的 malloc 部分?

发布于 2024-09-18 23:14:13 字数 450 浏览 7 评论 0原文

我的问题很基本,但已经有一段时间了。我正在读取文本文件并将文本中的数字保存到结构“记录”中。将文本读取到记录缓冲区后,我想将其放置在内存区域中。

typedef struct
{
 int line_status[64];
 float line_data[64], relativetime;
 unsigned long blkhdr_ticks;
} Record;

Record *storage; 
storage = (Record*)malloc(nRange*sizeof(Record)); 
Record buffer;

其中 nRange 是一些随机数,缓冲区是带有值的记录,尽管我没有列出将这些值分配给缓冲区的代码。我认为语法是这样的:

&storage = buffer;

但我知道这是不对的。任何帮助将不胜感激。

My question's pretty basic, but it's been a while. I'm reading in a text file and saving numbers in the text to a struct 'Record'. After I read text to my Record buffer, I want to place it in an area of memory.

typedef struct
{
 int line_status[64];
 float line_data[64], relativetime;
 unsigned long blkhdr_ticks;
} Record;

Record *storage; 
storage = (Record*)malloc(nRange*sizeof(Record)); 
Record buffer;

Where nRange is some random number, and buffer is a Record with values, though I haven't listed my code that assigns these to the buffer. I thought the syntax was something like:

&storage = buffer;

But I know that's not right. Any help would be greatly appreciated.

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

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

发布评论

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

评论(3

桃气十足 2024-09-25 23:14:13

您还可以将存储视为数组。

storage[0] = buffer;
storage[1] = anotherBuffer;
...
storage[nRange-1] = lastBuffer;

You can also treat storage as an array.

storage[0] = buffer;
storage[1] = anotherBuffer;
...
storage[nRange-1] = lastBuffer;
◇流星雨 2024-09-25 23:14:13

您应该可以说 *storage = buffer;storage[0] = buffer;

You should be able to say *storage = buffer; or storage[0] = buffer;.

抱猫软卧 2024-09-25 23:14:13

由于 storage 也可以被视为 nRange 记录的数组(我猜这确实是您的意图),您可以简单地执行以下操作:

 storage[0] = buffer;
 storage[someOtherIndex] = buffer;

Since storage can also be regarded as an array of nRange records (I guess that really is your intention) you can simply do:

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