Fehlersuche: assignment from incompatible pointer type
-
Hey,
ich baue gerade ein Programm für eine "lineare Liste" zusammen und hänge nun schon eine Weile an diesem Fehler und raff's einfach nicht. Der aufs wesentliche reduzierte (!) Quelltext sieht so aus:
typedef struct { float x, y; struct complex *next; } complex; complex *in(complex *p) { complex *j; j=(complex*)malloc(sizeof(complex)); printf("? <Re> <Im>: "); scanf("%f%f", &j->x, &j->y); j->next=p; return j; } int main() { complex *p=0, *h; h=in(p); }
Den genannten Fehler bekomme ich für Zeile 13. Hilft mir wer auf die Sprünge? Danke
-
typedef struct complex ...
Den Typ "struct complex" hast du nämlich nirgendwo definiert.
-
Danke - aber verstehe ich nicht ganz (was habe ich nicht definiert?). Mit typedef struct definiere ich doch einen Datentyp complex!?
EDIT: Ah hab's geschnallt! Danke!
-
flowschi schrieb:
Danke - aber verstehe ich nicht ganz (was habe ich nicht definiert?). Mit typedef struct definiere ich doch einen Datentyp complex!?
du hast einen 'typedef complex', aber keine 'struct complex'. so muss das:
typedef struct complex { float x, y; struct complex *next; } complex;
ach, und mach den type casts vorm malloc weg.
-
Auch dir ein Danke
Das Ding läuft jetzt, habe aber noch eine Frage dazu. Bevor ich einen neuen Thread wegen dieser "Kleinigkeit" auf mache, frage ich erstmal hier. Verstehe nicht warum ich Zeile 55 brauche, da es für mich so aussieht als ob p und h nach Ende der while-Schleife eh gleich sind. Wenn ich Zeile 55 aber weglasse, stürzt das Ding bei der Ausgabe ab. Was hab ich da übersehen?
/*CompList.c*/ #include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> typedef struct complex { float x, y; struct complex *next; } complex; float betrag(float x, float y) { return fabs(x)<fabs(y)? fabs(y)*sqrt(1+(x/y)*(x/y)) : x==0? 0: fabs(x)*sqrt(1+(y/x)*(y/x)); } complex *in(complex *p) { complex *j; j=malloc(sizeof(complex)); printf("? <Re> <Im>: "); scanf("%f%f", &j->x, &j->y); j->next=p; return j; } complex* out(complex* h) { printf("\n\n! Re Im: %f %f", h->x, h->y); printf("\n! Betrag: %f", betrag(h->x, h->y)); return 0; } int main() { /*Initialisierung*/ complex *p=0, *h; unsigned char c='j'; printf("Demo: Lineare Liste komplexer Zahlen\n"); /*Eingabe*/ while(c=='j') { h=in(p); p=h; printf("? Weiter mit \"j\": "); scanf("%1s", &c); } /*Ausgabe*/ printf("\nAusgabe"); h=p; while(h) { out(h); h=h->next; } return 0; }
-
Öhm -.-
.. ich glaub ich hab vergessen einmal zu compilen. Meine Frage hat sich erledigtSorry..