Seite 1 von 1

structs, verschiedene Datein, Zugriff

Verfasst: Fr Jan 18, 2013 6:48 pm
von petterapamm
Hallo allerseits,
folgender Fehler tritt beim Compilieren meines Codes auf:
"Anfrage nach Element "xPos" in etwas, was keine Struktur oder Variante ist"

Code: Alles auswählen

if((movingObjects=getObjects())!=NULL) value++;
printf("bla: %d",movingObjects[0].xPos);	
Der Fehler entsteht in der zweiten Zeile. Diese steht in meiner main.c, die Funktion getObjects sowie die Deklaration des structs "struct object" stehen in einer object.c bzw. object.h die per include in main eingebunden sind.
object.c:

Code: Alles auswählen

	 ...
#include "objects.h"
 
struct object* createObject(char id, int xPos, int yPos, int healthPoints, int attack, char type){
    struct object* newObject = malloc(sizeof(struct object));
    newObject->id = id;
    newObject->xPos = xPos;
    newObject->yPos = yPos;
    newObject->healthPoints = healthPoints;
    newObject->type = type;
    newObject->attack = attack;
    return newObject;
}
 
struct object** getObjects(){
    struct object** objects = (struct object**) malloc(50*sizeof(struct object*));
    objects[0] = createObject(1,50,50,100,10,0);
    return objects;
}	
object.h:

Code: Alles auswählen

	struct object{ 
    char id;
    int xPos;
    int yPos;
    int healthPoints;
    int attack;
    char type;
};
 
struct object** getObjects();	
Deklaration von movingObjects in main.c:

Code: Alles auswählen

struct object** movingObjects;
Es scheint mir, als wenn das Dateienübergreifende das Problem ist. Die Funktionen createObject und getObjects habe ich bereits seperat in object.c getestet. Sie funktionieren einwandfrei, man kann auf die einzelnen structs im Array zugreifen.
Ich bitte um Hilfe, vielen Dank im Vorraus, mit freundlichen Grüßen,
petterapamm

Re: structs, verschiedene Datein, Zugriff

Verfasst: Fr Jan 18, 2013 8:55 pm
von Xin
Moin!

Code: Alles auswählen

printf("bla: %d",movingObjects[0].xPos);
Wenn wir das auseinander nehmen, können wir uns die Typen ansehen:
movingObjects => (struct object **)
movingObjects[0] => (struct object *)

Mit (struct object *) hast Du keine Struktur, sondern einen Zeiger:

Code: Alles auswählen

printf("bla: %d",movingObjects[0]->xPos);

Re: structs, verschiedene Datein, Zugriff

Verfasst: Sa Jan 19, 2013 12:48 pm
von petterapamm
Danke für deine Hilfe, macht Sinn und hat funktioniert!