Ich habe eine Struktur:Wie referenziere ich einen Wert von einer Variablen aus einem Zeiger von einem Zeiger?
struct room;
...
/*some more structs */
typedef struct {
int state;
int id;
struct room* north;
struct room* south;
struct room* east;
struct room* west;
creature* creatures[10];
} room;
und eine Funktion, die den Inhalt druckt:
void printContentsOfRoom(room* r) {
printf("\nRoom %d:\n", r->id);
printf("\tState: %s\n", getState(r));
printf("\n\tNeighbors:\n");
if (r->north.id != -1)
printf("\t North: Room %d\n",((room*)r->north)->id);
if (r->east.id != -1)
printf("\t East: Room %d\n",((room*)r->east)->id);
if (r->south.id != -1)
printf("\t South: Room %d\n",((room*)r->south)->id);
if (r->west.id != -1)
printf("\t West: Room %d\n",((room*)r->west)->id);
printf("\n\tCreatures:\n");
for (int i = 0; i < 10; i++) {
creature* c = (creature*)r->creatures[i];
if (c) {
if (c == pc)
printf("\t %s\n",getType(c));
else
printf("\t %s %d\n",getType(c),c->id);
}
}
}
Ich versuche, es zu machen, so dass, wenn der Raum-ID auf -1 (Satz, während gesetzt das Programm läuft), die Informationen zu diesem Raum werden nicht gedruckt. Bei der Kompilierung bekomme ich den Fehler "Anfrage für Mitglieds-ID 'in etwas, das keine Struktur oder Union ist."
Außerdem habe ich versucht, die Einstellung der, wenn die Bedingung r-> nord-> id, und das gibt den Fehler „Dereferenzierung Zeiger auf einen unvollständigen Typ‚struct Raum‘“
'typedef struct {' -> 'typedef struct room {' dann 'r-> nord-> id' – BLUEPIXY
Verwenden Sie keine Güsse, wenn Sie das Problem nicht verstanden haben! – Olaf
'r-> west' ist ein Zeiger, richtig? '.' funktioniert nur für Strukturen und Vereinigungen, und ein Zeiger ist keine Struktur oder Union. So ist 'r-> west.anything' ungültig. Aber Sie könnten "(* r-> west) .anything" oder "r-> west-> irgendetwas" für kurz verwenden. – immibis