verständinsproblem addressausgabe dynamische Liste?
Hallo,
ich habe eine Liste in C erstellt, dies scheint wohl zu funktionieren, allerdings habe ich ein Problem mit der Ausgabe der Adresse der einzelnen Elemente in der Liste
Zunächst rufe ich für alle Files die Funktion "appenFileName_v" auf.
zum schluss rufe ich dann noch einmal die Funktion: "printList_v" auf um zu überprüfen ob alle elemente vorhanden sind
struct fileList_ts
{
char* name;
struct fileList_ts* next;
};
static struct fileList_ts* fileList_ps = NULL;
static void appendFileName_v(char* value)
{
struct fileList_ts *newElement_ps;
if (fileList_ps == NULL) // check if there is already an element in the list
{
if((fileList_ps = malloc(sizeof(struct fileList_ts))) == NULL)
{
ESP_LOGE(TAG, "no free memory for starting list");
}
else
{
ESP_LOGI(TAG, "address fileList_ps: %p", &fileList_ps);
fileList_ps->name = (char *)malloc(strlen(value) + 1);
strcpy(fileList_ps->name, value);
fileList_ps->next = NULL;
}
}
else
{
newElement_ps = fileList_ps; // point to first element
while(newElement_ps->next != NULL) // go to last element
{
newElement_ps = newElement_ps->next;
}
if((newElement_ps->next = malloc(sizeof(struct fileList_ts))) == NULL) // reserve memory for the new element
{
ESP_LOGE(TAG, "no free memory for the new element");
}
else
{
newElement_ps = newElement_ps->next; // point to new memory
newElement_ps->name = (char *)malloc(strlen(value) + 1);
ESP_LOGI(TAG, "address newElement: %p", &newElement_ps);
strcpy(newElement_ps->name, value);
newElement_ps->next = NULL;
}
}
}
static void printList_v(void)
{
struct fileList_ts *pointer = fileList_ps;
while(pointer != NULL) {
ESP_LOGI(TAG, "printList: %p %s", &pointer, pointer->name);
pointer = pointer->next;
}
}
Die Ausgabe ist dann wie folg:
I (634) SD_CARD: address fileList_ps: 0x3ffb2fbc SYSTEM~1
I (634) SD_CARD: address newElement: 0x3ffb8f10 HELLO.TXT
I (634) SD_CARD: address newElement: 0x3ffb8f10 FOO.TXT
I (634) SD_CARD: address newElement: 0x3ffb8f10 DREIECK.BMP
I (644) SD_CARD: address newElement: 0x3ffb8f10 DREIECK.PNG
I (644) SD_CARD: address newElement: 0x3ffb8f10 KREUZ.BMP
I (664) SD_CARD: address newElement: 0x3ffb8f10 TEST.BMP
I (664) SD_CARD: address newElement: 0x3ffb8f10 TEST2.BMP
I (664) SD_CARD: printList: 0x3ffb8f10 SYSTEM~1
I (664) SD_CARD: printList: 0x3ffb8f10 HELLO.TXT
I (674) SD_CARD: printList: 0x3ffb8f10 FOO.TXT
I (674) SD_CARD: printList: 0x3ffb8f10 DREIECK.BMP
I (674) SD_CARD: printList: 0x3ffb8f10 DREIECK.PNG
I (694) SD_CARD: printList: 0x3ffb8f10 KREUZ.BMP
I (694) SD_CARD: printList: 0x3ffb8f10 TEST.BMP
I (694) SD_CARD: printList: 0x3ffb8f10 TEST2.BMP
Die Namen sind also alle in der Liste drinn. Nur wundert es mich, dass die Adresse überall die selbe ist.