The code bellow creates structures and links them using pointers,
prints the structures or quits and frees the allocated memory
for the structures
what happens is the following
i add the first structure... prints fine
i add the second structure... prints fine
i add the third... press print and the program crashes
Code:
//*FSpointer is the first structure in the list
void printfunc(typeLS *FSpointer){
typeLS *holder3 = FSpointer;
while(holder3 != NULL){
printf("name:%s float:%f\n",holder3->name,holder3->decimals);
holder3 = holder3->forward;
if(holder3 != NULL){
printf("not NULL\n");
}
}
printf("exited loop\n");
so far i know that the while loop can't stop if i add more than 2
structures, because for some reason structure 3 forward pointer
is not NULL even though there is no structure after it.
I can't grasp where the code logic is wrong, adding a second structure works fine, and adding a 3rd structure uses the same logic as adding the second structure but it doesn't work WTF?
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef struct LSexample{
char name[52];
float decimals;
struct LSexample *forward;
}typeLS;
typeLS *returnPointerToAllocatedMemory(typeLS *MainPointers){
char UserInput5[16];
fgets(UserInput5, 15,stdin);
typeLS *PointertoAllocatedMemory = malloc(sizeof(typeLS));
sscanf(UserInput5,"%s %f",PointertoAllocatedMemory->name,&PointertoAllocatedMemory->decimals );
printf("Name:%s float:%f\n",PointertoAllocatedMemory->name,PointertoAllocatedMemory->decimals);
if(MainPointers != NULL){//if there is somethign behind then set
// whats behind pointer to point at the newly created structure
MainPointers->forward = PointertoAllocatedMemory;//mainpointers is the previous structure
// if there isn't one it will be NULL
}
return PointertoAllocatedMemory;
}
void cleanUp(typeLS *FirstStructure1){
typeLS *freeThisAllocatedMemory = NULL;
typeLS *HoldPointerForward = NULL;
freeThisAllocatedMemory = FirstStructure1;
if(freeThisAllocatedMemory != NULL){
printf("Free name:%s float %f\n",freeThisAllocatedMemory->name,freeThisAllocatedMemory->decimals);
HoldPointerForward = freeThisAllocatedMemory->forward;
free(freeThisAllocatedMemory);
freeThisAllocatedMemory = HoldPointerForward;
}
}
void printfunc(typeLS *FSpointer){
typeLS *holder3 = FSpointer;
while(holder3 != NULL){
printf("name:%s float:%f\n",holder3->name,holder3->decimals);
holder3 = holder3->forward;
if(holder3 != NULL){
printf("not NULL\n");
}
}
printf("exited loop\n");
}
int main(){
typeLS *Firststructure = NULL;//this pointer will point at the first structure in the list
typeLS *holderr = NULL; //this pointer will point at structures temporarily
char UserInput[16];
char translation[16];
while(fgets(UserInput, 15,stdin)){
sscanf(UserInput,"%s",translation);
if(strncmp(translation, "add",3)==0){
printf("Ready to add...\n");
if(Firststructure == NULL){
Firststructure = returnPointerToAllocatedMemory(NULL);
holderr = Firststructure;
}else{
holderr = returnPointerToAllocatedMemory(holderr);
}
}else if(strncmp(translation, "print",5)==0){
printf("Printing...\n");
printfunc(Firststructure);
}else if(strncmp(translation, "quit",4)==0){
printf("Breaking...\n");
break;
}
}
cleanUp(Firststructure);
return 0;
}