C-从动态内存访问结构

发布于 2024-09-18 23:31:11 字数 398 浏览 10 评论 0原文

我正在用 struct Record 编写一个程序。当我从循环中的文本中读取记录时,我将它们分配给缓冲区,然后将缓冲区保存到数组中。 nRange 只是正在读取的记录总数。

Record *storage; 
storage = (Record*)malloc(nRange*sizeof(Record)); 
Record buffer;
storage[i] = buffer;

我想访问 storage[i] 以检查记录是否保存到内存中,但我不太明白语法。我正在尝试类似的事情:

printf("%d \n", &storage[i].x);

但我认为这只是给我存储[i]处记录的x值的地址。如果有人能给我确切的语法,我将不胜感激。

I'm writing a program with struct Record. As I read in records from text in a loop, I assign them to buffer before saving buffer into the array. nRange is just the total number of records being read.

Record *storage; 
storage = (Record*)malloc(nRange*sizeof(Record)); 
Record buffer;
storage[i] = buffer;

I want to access storage[i] in order to check that the record is being saved to memory, but I can't quite get the syntax. I was trying something like:

printf("%d \n", &storage[i].x);

But I think this is just giving me the address of the x value of the Record at storage[i]. If anyone could give me the exact syntax I'd greatly appreciate it.

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

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

发布评论

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

评论(1

沉睡月亮 2024-09-25 23:31:11

你想太多了。您只需编写 storage[i],就像分配它时一样。

printf("%d \n", storage[i].x);

You're overthinking things. You just write storage[i], just like when you assigned it.

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