C - 如何将结构保存到内存的 malloc 部分?
我的问题很基本,但已经有一段时间了。我正在读取文本文件并将文本中的数字保存到结构“记录”中。将文本读取到记录缓冲区后,我想将其放置在内存区域中。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还可以将存储视为数组。
You can also treat storage as an array.
您应该可以说
*storage = buffer;
或storage[0] = buffer;
。You should be able to say
*storage = buffer;
orstorage[0] = buffer;
.由于
storage
也可以被视为 nRange 记录的数组(我猜这确实是您的意图),您可以简单地执行以下操作:Since
storage
can also be regarded as an array of nRange records (I guess that really is your intention) you can simply do: