【发布时间】:2014-10-23 04:35:28
【问题描述】:
我有一个结构
typedef struct
{
UINT32 num_pairs;
UINT32 value;
}
SCSI_ENTRIES;
我需要动态实例化这个结构的对象。
for (int i = 0; i < 50; i++)
{
if ( port[i] )
{
port_valid_count += 1;
// Please tell me how to instantiate
// SCSI_ENTRIES objects dynamically,
// based on port_valid_count.
// Something like SCSI_ENTRIES entries[port_valid_count] ;
}
}
每次增加 port_valid_count 时,我都需要增加结构的对象。
【问题讨论】:
-
只用
realloc()分配更大的数组? -
Dynamically increase/decrease array size 的可能重复项。您也可以尝试在 C 中实现自己的向量(例如本文:Implementing a dynamic-vector array in C)。
-
谁能给我演示一下示例代码。我试过 realloc(entries , port_valid_count*sizeof(SCSI_ENTRIES));
-
@user1846251:阅读链接线程中的答案。其中一个有this link 到
realloc,上面@Jesus 提到过。或者这个答案:How to use realloc in C.
标签: c