ganz fießer bug
-
Hallo Leute.
Da ist ein bug in meinen Programm wo eigentlich gar keiner sein kann. Kann mir da jemand erklären?
struct card* mix (struct card* stack)
{
struct card * help[3];
int h[2], i, j;
srand(time(NULL));
h[1]=rand()%10+5;
h[2]=rand()%10+25;
help[1]=stack;
for(i=1; i<h[1]; i++) help[1]=help[1]->next;
help[2]=help[1]->next;
help[3]=help[1]->next;
printf("h[2]=%d\n", h[2]); //---------------------------------------------------------------- hier läuft das Programm noch(wird noch ausgegeben)
printf("."); //---------------------------------------------------------------- vor dieser zeile hängt es sich in eine endlosschleife (wird nicht mehr ausgegeben)
for(i=1; i<h[2]; i++) help[2]=help[2]->next;
help[1]->next=help[2]->next;
help[2]->next=NULL;
printf(".");
for(j=1;j<11;j++)
{
h[1]=rand()%3+5; /*start of mix*/
printf(".");
while((h[2]-h[1])>=0)
{
help[1]=help[3];
for(i=1;i<h[1];i++) help[1]=help[1]->next;
help[2]=help[1]->next;
help[1]->next=stack;
stack=help[3];
help[3]=help[2];
help[2]=help[2];
h[1]=rand()%3+5;
}
while (help[2]->next!=NULL) help[2]=help[2]->next;
help[2]->next=stack;
stack=help[3];
}
printf("stack output: %i\n", stack->next->next->next->next->next->next->next->next->next->next->next->next->next->next->value);
return stack;
}
-
Hi!
Benutz bitte das Code-Statement
struct card* mix (struct card* stack) { struct card * help[3]; int h[2], i, j; srand(time(NULL)); h[1]=rand()%10+5; h[2]=rand()%10+25; help[1]=stack; for(i=1; i<h[1]; i++) help[1]=help[1]->next; help[2]=help[1]->next; help[3]=help[1]->next; printf("h[2]=%d\n", h[2]); //-- hier läuft das Programm noch(wird noch ausgegeben) printf("."); //-- vor dieser zeile hängt es sich in eine endlosschleife (wird nicht mehr ausgegeben) for(i=1; i<h[2]; i++) help[2]=help[2]->next; help[1]->next=help[2]->next; help[2]->next=NULL; printf("."); for(j=1;j<11;j++) { h[1]=rand()%3+5; /*start of mix*/ printf("."); while((h[2]-h[1])>=0) { help[1]=help[3]; for(i=1;i<h[1];i++) help[1]=help[1]->next; help[2]=help[1]->next; help[1]->next=stack; stack=help[3]; help[3]=help[2]; help[2]=help[2]; h[1]=rand()%3+5; } while (help[2]->next!=NULL) help[2]=help[2]->next; help[2]->next=stack; stack=help[3]; } printf("stack output: %i\n", stack->next->next->next->next->next->next->next->next->next->next->next->next->next->next->value); return stack; }
mfg bauerb
-
printf(".");
gibt nichts aus, da in C die Standardausgabe Zeilengepuffert ist.
Also musst du entweder ne neue Zeile ausgeben oder die Ausgabe flushen.Edit:
Achja... h[2] ist mindestens 25 und h[1] maximal 7... siehst du das problem beiwhile((h[2]-h[1])>=0)
-
#include<time.h> struct card { struct card* next; int value, symbol; }; struct card* create (struct card* stack) /*creats a stack of cards with a whole card deck */ { struct card* help; int i, j; for(j=1;j<5;j++) { for(i=1;i<14;i++) { help = (struct card*) malloc(sizeof(struct card)); help->next=stack; stack=help; help->value=i; help->symbol=j; } } return stack; } struct card* mix (struct card* stack) { struct card * help[3]; int h[2], i, j; srand(time(NULL)); h[1]=rand()%10+5; h[2]=rand()%10+25; help[1]=stack; for(i=1; i<h[1]; i++) help[1]=help[1]->next; help[2]=help[1]->next; help[3]=help[1]->next; printf("h[2]=%d\n", h[2]); printf("huhu\n"); for(i=1; i<h[2]; i++) help[2]=help[2]->next; help[1]->next=help[2]->next; help[2]->next=NULL; printf("huhu\n"); for(j=1;j<11;j++) { h[1]=rand()%3+5; /*start of mix*/ printf("huhu\n"); while((h[2]-h[1])>=0) { help[1]=help[3]; for(i=1;i<h[1];i++) help[1]=help[1]->next; help[2]=help[1]->next; help[1]->next=stack; stack=help[3]; help[3]=help[2]; help[2]=help[2]; h[1]=rand()%3+5; } while (help[2]->next!=NULL) help[2]=help[2]->next; help[2]->next=stack; stack=help[3]; } printf("stack output: %i\n", stack->next->next->next->next->next->next->next->next->next->next->next->next->next->next->value); return stack; }
Und meine Konsole sagt dazu:
philipp@hufi:~/karten$ gcc -o main main.c In file included from main.c:2: stack.h: In Funktion »create«: stack.h:17: Warnung: Unverträgliche implizite Deklaration der eingebauten Funktion »malloc« philipp@hufi:~/karten$ ./main succesfuly loaded h[2]=7
Danach macht sie gar nichts mehr. Sie hängt sich einfach auf
-
[ ]
du hast gelesen und verstanden was ich geschrieben hab.
-
gelesen schon aber ich glaube nicht verstanden. Kannst du es mir noch mal erklären?
-
Das sieht aber toll aus:
stack->next->next->next->next->next->next->next->next->next->next->next->next->next->next->value
da könnte man sicher ne schleife draus machenund warum h[2] bei einem array der größe 2?
-
DAS IST ES!!! DANKE!!!
Edit: aber wenn ich den Buffer korregiere macht es noch immer den gleichen unfug
#include<time.h> struct card { struct card* next; int value, symbol; }; struct card* create (struct card* stack) /*creats a stack of cards with a whole card deck */ { struct card* help; int i, j; for(j=1;j<5;j++) { for(i=1;i<14;i++) { help = (struct card*) malloc(sizeof(struct card)); help->next=stack; stack=help; help->value=i; help->symbol=j; } } return stack; } struct card* mix (struct card* stack) { struct card * help[3]; int h[2], i, j; srand(time(NULL)); h[0]=rand()%10+5; h[1]=rand()%10+25; help[0]=stack; for(i=1; i<=h[0]; i++) help[0]=help[0]->next; help[1]=help[0]->next; help[2]=help[0]->next; printf("h[1]=%d\n", h[1]); printf("huhu\n"); for(i=1; i<=h[1]; i++) help[1]=help[1]->next; help[0]->next=help[1]->next; help[1]->next=NULL; printf("huhu\n"); for(j=1;j<11;j++) { h[0]=rand()%3+5; /*start of mix*/ printf("huhu\n"); while((h[1]-h[0])>=0) { help[0]=help[2]; for(i=1;i<h[0];i++) help[0]=help[0]->next; help[1]=help[0]->next; help[0]->next=stack; stack=help[2]; help[2]=help[1]; help[1]=help[1]; h[0]=rand()%3+5; } while (help[1]->next!=NULL) help[1]=help[1]->next; help[1]->next=stack; stack=help[2]; } printf("stack output: %i\n", stack->next->next->next->next->next->next->next->next->next->next->next->next->next->next->value); return stack; }