mogbo
Banned
Hallo,
wiedermal eine Frage die ich nicht wirklich ergoogelt bekomme:
Siehe Comments in der 1. Schleife
Ich verstehe nicht wirklich wie ich Speicherpositionen innerhalb eines structs ausgeben kann oder ist oben alles richtig und ich habe einen allgemeinen Denkfehler?
Output:
wiedermal eine Frage die ich nicht wirklich ergoogelt bekomme:
Code:
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int zahl;
struct node *next;
} node_t;
node_t *ptr = NULL;
int
main(void)
{
short i = 0, b = 3;
ptr = malloc(sizeof(node_t));
node_t *current = ptr;
ptr->zahl = i;
i++;
/* Struct fuell-Schleife */
while (i <= b) {
ptr->next = malloc(sizeof(node_t));
ptr->next->zahl = i;
/*
* Gebe ich hier wirklich die Position von ptr->next->zahl aus?
*/
printf("Position von ptr->next->zahl: %p\n", &(ptr->next->zahl));
printf("Position von ptr->next->zahl: %p\n\n", ptr->next);
// Ende der Frage 1
/*
* Wie gebe ich die Position von "next" aus?
*/
printf("Position von ptr->next: %p\n", &(ptr->next));
ptr = ptr->next;
printf("Position von ptr->next: %p\n\n", &ptr);
// Ende der Frage 2
i++;
}
ptr = NULL;
printf("\nOutput:\n\n");
/* Struct output-Schleife */
while (current != NULL) {
printf("%i\n", current->zahl);
current = current->next;
}
free(ptr);
return 0;
}
Ich verstehe nicht wirklich wie ich Speicherpositionen innerhalb eines structs ausgeben kann oder ist oben alles richtig und ich habe einen allgemeinen Denkfehler?
Output:
Code:
Position von ptr->next->zahl: 0x1660f9b349f0
Position von ptr->next->zahl: 0x1660f9b349f0
Position von ptr->next: 0x16619afeace8
Position von ptr->next: 0x165ec1301058
Position von ptr->next->zahl: 0x166133268c70
Position von ptr->next->zahl: 0x166133268c70
Position von ptr->next: 0x1660f9b349f8
Position von ptr->next: 0x165ec1301058
Position von ptr->next->zahl: 0x1661269803b0
Position von ptr->next->zahl: 0x1661269803b0
Position von ptr->next: 0x166133268c78
Position von ptr->next: 0x165ec1301058
Output:
0
1
2
3