C - How can I save structs to a malloc'd section of memory?
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;
sto开发者_开发百科rage = (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.
You can also treat storage as an array.
storage[0] = buffer;
storage[1] = anotherBuffer;
...
storage[nRange-1] = lastBuffer;
You should be able to say *storage = buffer;
or storage[0] = 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;
精彩评论