0

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; } 
4
  • 2
    "Please let me know if I could provide more detail. Thanks." It would be better if you try to provide less while still having complete code that causes the problem. What can you take out while still observing this behaviour (never mind the actual functionality of the program)? Commented Dec 15, 2021 at 2:58
  • Thought experiment time... Loop beginning with: while(next != NULL) { next = next->next; what guarantees do you now have that next is 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. Commented Dec 15, 2021 at 3:05
  • 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. Commented Dec 15, 2021 at 3:11
  • If your IDE is collecting the program's output via a pipe or temporary file (not a pseudo-terminal), then your program would by default fully buffer its output. This would mean that the printf's would not generate any actual output until a lot of data had been printed (probably more than your program prints) or until it exits normally. So if it crashes then none of it ever gets written out. I'm not quite sure if that's the issue here (it'd be hard for an IDE to do this when running an interactive program) but it could be something along those lines. Commented Dec 15, 2021 at 3:50

1 Answer 1

1

The issue is your newListStart = createDigit(next->num). Trying to access num results in a dereference of a null pointer (you cannot access *next's fields since it isn't pointing to anywhere).

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.