我对几个问题感到很困惑。
让我说我有这个......
void insert_data(OPAQUE *hOpaque, int key, int data);
//cast to the known type..
使用此函数传递值...
<config>
<modules>
<Wolfsellers_Saeconnector>
<version>0.0.1</version>
</Wolfsellers_Saeconnector>
</modules>
<global>
<models>
<wolfsellers_saeconnector>
<resourceModel>wolfsellers_saeconnector_resource</resourceModel>
<class>Wolfsellers_Saeconnector_Model</class>
</wolfsellers_saeconnector>
<wolfsellers_saeconnector_resource>
<entities>
<saewolf>
<table>sae_wolf</table>
</saewolf>
<saeprodwolf>
<table>sae_prod_wolf</table>
</saeprodwolf>
<saewolfpath>
<table>sae_path_wolf</table>
</saewolfpath>
<saewolfconf>
<table>sae_wolf_conf</table>
</saewolfconf>
</entities>
</wolfsellers_saeconnector_resource>
</models>
<resources>
<wolfsellers_saeconnector_setup>
<setup>
<module>Wolfsellers_Saeconnector</module>
<class>Mage_Core_Model_Resource_Setup</class>
</setup>
<connection>
<use>core_setup</use>
</connection>
</wolfsellers_saeconnector_setup>
</resources>
<helpers>
<saeconnector>
<class>Wolfsellers_Saeconnector_Helper</class>
</saeconnector>
</helpers>
<blocks>
<wolfsellers_saeconnector>
<class>Wolfsellers_Saeconnector_Block</class>
</wolfsellers_saeconnector>
</blocks>
</global>
<adminhtml>
<acl>
<resources>
<admin>
<children>
<system>
<children>
<config>
<children>
<saeconnector_options>
<title>Custom Configuration Section</title>
</saeconnector_options>
</children>
</config>
</children>
</system>
</children>
</admin>
</resources>
</acl>
</adminhtml>
<crontab>
<jobs>
<wolfsellers_saeconnector>
<run>
<model>wolfsellers_saeconnector/observer::setStatus</model>
</run>
</wolfsellers_saeconnector>
</jobs>
</crontab>
如何在每次迭代中创建多个数据结构。每个结构都获得一个新的值输入,所以......;
key = 52; data = 43;
这些值将出现在第一个对象中。现在..如果我给20个键和20个数据怎么办?然后我会调整大小以适应更多值的涌入以创建更多结构。我总共有20个结构。
对于我应该如何处理以及如何做到这一点略有困惑。
由于
答案 0 :(得分:1)
只是举个例子,你可以像
一样手动完成Data *new_data = malloc(old_size + 1 * sizeof *new_data);
if (new_data == NULL)
{
// Handle error
}
memcpy(new_data, old_data, old_size * sizeof *old_data);
free(old_data);
old_data = new_data;
++old_size;
你可以或
Data *new_data = realloc(old_data, old_size++ * sizeof *new_data);
if (new_data == NULL)
{
// Handle error
}
old_data = new_data;
不计算错误检查和处理,语句数量减少了60%。
您决定哪个更简单。 :)
答案 1 :(得分:1)
您可以使用malloc()来分配所需的结构:
Data *info;
可以用于malloc()类型为Data的结构,因此如果要分配20个数据结构:
// Inside the function that receives the pointer to Headm assume h
// be the poiner to the Head structure:
h -> info = malloc(sizeof(Data) * 20);
如果我们想增加到21个结构,或者根据需要动态重新分配结构,例如:
h -> info = realloc(h -> info, (sizeof(Data) * 21));
另一种选择可能是:
struct head
{
int size;
int capacity;
Data info;
};
struct data
{
int key;
int values;
}
Head **h = malloc(sizeof(Head *) * 20);
要分配数组20头结构指针,因此您不必使用malloc()Data结构。然后,您可以使用每个Head指针分配单个Head结构:
int s;
for (s = 0; s < 20; s++) {
if ((h[s] = malloc(sizeof(Head))) == NULL) {
perror("malloc()");
exit(EXIT_FAILURE);
}
}
我想有很多方法可以做到,取决于你的需求。
希望这有帮助。