I'm currently working on a lesson in the free edX.org C Programming course. I'm working through the final examples on linked list and playing around with the code below. This code is not complete yet, but what I'm baffled by is that when uncommenting the commented line in main, none of the printf statements give output to the console. Even the printfs before that line.
When I run it I'm simply entering 123 and then pressing the enter key.The printfs output fine as long as the line is commented out. I should also mention that when setting breakpoints around the printfs while it's commented out, it appears the program is continuing to execute through and past the breakpoints, but no output appears on the console.
Could anyone give insight into why this is happening? I'm running this in STM32CubeIDE on Fedora Linux with GCC toolchain. Please let me know if I could provide more detail. Thanks.
#include <stdio.h> #include <stdlib.h> struct digit { int num; struct digit *next; }; struct digit * createDigit(int); struct digit * append(struct digit * end, struct digit * newDigptr); void printNumber(struct digit *); void freeNumber(struct digit *start); struct digit * readNumber(); struct digit * searchNumber(struct digit * start, int number); struct digit * reverseNumber(struct digit* start); int main(void) { struct digit *start, *backwards; printf("Please enter a number: "); start = readNumber(); printf("a\n"); printNumber(start); printf("b\n"); //backwards = reverseNumber(start); printf("c\n"); freeNumber(start); return 0; } struct digit *createDigit(int dig) { struct digit *ptr; ptr = (struct digit *) malloc(sizeof(struct digit)); ptr->num = dig; ptr->next = NULL; return ptr; } struct digit * append(struct digit * end, struct digit * newDigptr) { end->next = newDigptr; return(end->next); } void printNumber(struct digit *start) { struct digit * ptr = start; while (ptr != NULL) { printf("%d", ptr->num); ptr = ptr->next; } printf("\n"); } void freeNumber(struct digit *start) { struct digit * ptr = start; struct digit * tmp; while (ptr != NULL) { tmp = ptr->next; free(ptr); ptr = tmp; } } struct digit * readNumber() { char c; int d; struct digit *start, *end, *newptr; start = NULL; scanf("%c", &c); while (c!='\n') { d = c - 48; newptr = createDigit(d); if (start == NULL) { start = newptr; end = start; } else { end = append(end, newptr); } scanf("%c", &c); } return start; } struct digit * searchNumber(struct digit * start, int number) { struct digit * ptr = start; while (ptr != NULL && ptr->num != number) { ptr = ptr->next; } return(ptr); } struct digit* reverseNumber(struct digit* start) { // iterate the original list and for each element make it the end in the new list struct digit *next, *newListStart; newListStart = next = start; if (newListStart == NULL) printf("yay"); else printf("nay"); while (next != NULL) { next = next->next; newListStart->next = createDigit(newListStart->num); newListStart = createDigit(next->num); } return newListStart; }
while(next != NULL) { next = next->next;what guarantees do you now have thatnextis not NULL? Your program is most likely crashing, and indeed that's what I see if I run it in a sandbox. But for you developing this, you should 100% be attaching your debugger and stepping through or intercepting the inevitable breakpoint when it does crash.newListStart->next = createDigit(newListStart->num); newListStart = createDigit(next->num);That doesn't make sense. The second statement will create a new list every iteration throwing away all previous nodes.