for some reason, my code does create a random linked dynamic data structure, but whenever i try and terminate it it constatntly crashes....please help.
Code:
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int sentinel = 0;
typedef struct list{
char c;
struct list *listPtr;
} List;
void populate (List);
int main(){
srand(time(NULL));
List list1;
populate(list1);
printf("%d\n", sizeof(List));
free(&list1);
system("PAUSE");
return 0;
}
void populate(List list){
List *currentPtr = &list;
for (sentinel = 0; sentinel <= 10; sentinel++){
List *newNode = (List *) malloc(sizeof(List));
currentPtr->listPtr = newNode;
newNode = ++currentPtr;
newNode->c = rand() % 26 + 65;
newNode->listPtr = NULL;
printf("%c->", newNode->c);
}
printf("NULL\n\n");
}
i have noticed that if i remove the while loop and just generate 1 node, the thing executes and terminates fine. could it be that i cant use malloc in a while loop? if so, please explain....