[問題] 靜態陣列可調整大小?
如果這是蠢問題就請噓爆我@@
開發平台(Platform): (Ex: VC++, GCC, Linux, ...)
GCC
問題(Question):
有一個巢狀結構如下述,請問那個records的陣列大小是可以調整的嗎@@?
程式碼(Code):(請善用置底文網頁, 記得排版)
typedef struct{
time_t t;
double d;
}Value;
typedef struct{
int NumberOfRecords;
struct _Record{
char Name[64];
Value value;
}records[1];
}Message;
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 114.42.207.127
※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1416930324.A.E67.html
→
11/25 23:59, , 1F
11/25 23:59, 1F
→
11/26 00:04, , 2F
11/26 00:04, 2F
→
11/26 00:05, , 3F
11/26 00:05, 3F
→
11/26 00:06, , 4F
11/26 00:06, 4F
所以我可以
Message msg;
msg.records = (struct _Record)* malloc (5 * sizeof(struct _Record));
...
free(msg.records);
類似這樣? 怎麼感覺都怪怪的@@
→
11/26 00:10, , 5F
11/26 00:10, 5F
→
11/26 00:30, , 6F
11/26 00:30, 6F
→
11/26 00:31, , 7F
11/26 00:31, 7F
→
11/26 00:32, , 8F
11/26 00:32, 8F
我的表達能力很差, 因為這個結構拿來用 Message msg; 那我只能
msg.records[0].d = 1.0;
time(&msg.records[0].t); 之類的存一筆資料
我在納悶如我我要存NumberOfRecords筆的資料會怎麼做
→
11/26 01:50, , 9F
11/26 01:50, 9F
→
11/26 01:51, , 10F
11/26 01:51, 10F
→
11/26 01:52, , 11F
11/26 01:52, 11F
就是現在Message這個結構裡面只會有一筆record,
納悶有什麼辦法在不動records[1]這邊, //像假如改成records[5]就可以存5筆
但是可以達到類似宣告records[5]的效果 //或是改成 *records 就可以 malloc?
※ 編輯: lionheart60 (114.42.207.127), 11/26/2014 02:09:15
→
11/26 02:12, , 12F
11/26 02:12, 12F
→
11/26 02:12, , 13F
11/26 02:12, 13F
討論串 (同標題文章)