C-从动态内存访问结构
我正在用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你想太多了。您只需编写
storage[i]
,就像分配它时一样。You're overthinking things. You just write
storage[i]
, just like when you assigned it.