別のリンク リストを含むリンク リストがあり、それらにデータを統合したいのですが、できませんでした。
これが私のコードです:
構造宣言:
typedef struct BigStructure {
UINT x;
UINT y;
struct SmallStructure* smallStructure;
struct BigStructure* next;
} BigStructure;
typedef struct SmallStructure {
UINT x;
UINT y;
struct SmallStructure* next;
} SmallStructure;
構造操作関数:
BigStructure* addLinkedListElement(BigStructure* linkedList)
{
if(linkedList-> next == NULL)
{
return NULL;
}
BigStructure* newLinkedList = malloc(sizeof(linkedList));
newLinkedList->next = linkedList;
return newLinkedList;
}
BigStructure* removeLinkedListElement(BigStructure* linkedList)
{
//If the list is empty, we return NULL
if(linkedList == NULL)
return NULL;
//If the list contains one element
if(linkedList->next == NULL)
{
free(linkedList);
return NULL;
}
//if the list contains at least 2 elements
BigStructure* tmp = linkedList;
BigStructure* ptmp = linkedList;
/* Tant qu'on n'est pas au dernier élément */
while(tmp->next != NULL)
{
//ptmp stores the address of tmp
ptmp = tmp;
//We move tmp (but pmpt keeps the old value of tmp)
tmp = tmp->next;
}
ptmp->next = NULL;
free(tmp);
return linkedList;
}
BigStructure* getLinkedListElement(BigStructure* linkedList, int id)
{
int i = 0;
for(i=0; i<id && linkedList != NULL; i++)
{
linkedList = linkedList->next;
}
if(linkedList == NULL)
{
return NULL;
}
else
{
return linkedList;
}
}
上記のコードを使用して SmallStructure 変数にアクセスしようとしましたが、大きな数値 (アドレスのように見えます) が返されました。
BigStructure* bigStructure = NULL;
void addBigStructure(UINT x, UINT y) {
if(bigStructureNb == 1)
{
bigStructure->x = x;
bigStructure->y = y;
}
else
{
BigStructure* newBigStructure;
newBigStructure = (BigStructure*)addLinkedListElement((BigStructure*)&bigStructure);
newBigStructure->x = x;
newBigStructure->y = y;
}
}
void addSmallStucture(UINT x, UINT y) {
if(smallStructuresNb == 1)
{
bigStructure->startTrigger = malloc(sizeof(BigStructure*));
bigStructure->startTrigger->x = x;
bigStructure->startTrigger->y = y;
}
else
{
BigStructure* tmpBigStructure = NULL;
tmpBigStructure = (BigStructure*)getLinkedListElement(&bigStructure, rowID); //Table row ID
g_print("%d", tmpBigStructure->id); //Here I get a false value !!!!
//Here I want to set the value of the tmpBigStructure->smallStructure->x/y
}
}