0

I'm looking over some old code and I am having trouble getting to the print statement after the scanf function. Everytime I run this code I have to break it using control c. What am I missing about Linked Lists that could help me solve this issue? For context I haven't taken a class in C in ages and I would like to review how to properly use data structures like Linked Lists but for some reason this problem I did ages ago is eluding me. What should I do?

#include <stdio.h>
#include <stdlib.h>

void trav_and_print(void);

typedef struct linked_list
{
   int data;
   struct linked_list *next;
}   element;
int a;
int* pA = &a;
typedef element * elementptr;
  elementptr first = NULL,
            last = NULL,
            current;
            

int main()
{
   /* Create a linked list with one element            */
   /* NOTE: the first element is always a special case */
   
   first = (elementptr) malloc(sizeof(element));
   last = first;
   last -> data = 5;
   last -> next = NULL;

   /* Add another element to the end of the list */

   last -> next = (elementptr) malloc(sizeof(element));
   last = last -> next;
   last -> data = 12;
   last -> next = NULL;
   printf("%d \n",last->data);

   last -> next = (elementptr) malloc(sizeof(element));
   last = last -> next;
   printf("Enter a number: ");
   scanf("%d\n",pA);
   fflush(stdin); 
   last -> next = (elementptr) malloc(sizeof(element));
   last = last -> next;
   last -> data = a;
   last -> next = NULL;
   printf("Number added is %d \n",last -> data);

   trav_and_print();
   free(first);
   free(last);

   return 0;
}

void trav_and_print(void)
{

   current = first;
   while (current!=NULL)
   {
      printf("The data value is %d\n",current -> data);
      current = current -> next;
   }
}

  • Does this answer your question? [Using "\n" in scanf() in C](https://stackoverflow.com/questions/15443483/using-n-in-scanf-in-c) – kaylum Aug 16 '20 at 05:25
  • 1
    Remove the `\n` from the `scanf` Also, the second last `malloc` element does not have any data value set. – kaylum Aug 16 '20 at 05:26
  • 1
    The C specification explicitly say that passing an input-only stream (like `stdin`) to `fflush` leads to *undefined behavior*. Even if your environment have added it as a non-portable extension, please try to avoid using it. – Some programmer dude Aug 16 '20 at 05:35
  • Aside from the `scanf` and `fflush` bugs, you're allocating four elements (four calls to `malloc`). You're only setting the `data` values in three of them, and when you're done, you're only freeing two of them. – Tom Karzes Aug 16 '20 at 05:48

1 Answers1

0

you have several issues that need to be fixed. i will list them all. the answer to your question is (quoted from the answer on this question white-space-in-a-scanf-format-string please go to the link and keep reading the whole answer, this is only the first part of it:

A whitespace character in a scanf format causes it to explicitly read and ignore as many whitespace characters as it can. So with scanf("%d ", ..., after reading a number, it will continue to read characters, discarding all whitespace until it sees a non-whitespace character on the input. That non-whitespace character will be left as the next character to be read by an input function...

in main you have called malloc several times, but you did not call the free() for each one of the allocated buffers. so you program encounters memory leaks !

malloc can fails in allocating the memory, so we must check the return value for each malloc call, see below:

first = (elementptr) malloc(sizeof(element));
if(NULL == first)
{
    /*do error handling*/
}

in the scope of function main you are referencing all the global variables you have declared. global variables is extremely dangerous (especially for multi threaded applications) do your best to prevent globals usage.

I do not recommend you to use typedef in this form , typedef element * elementptr; ! this can misleads the users and you esppecialy when the code is large enough. usually this is done for pointers to pointers or higher!

fflush(stdin); , flushing stdin is UB. this is taken from the ANSIC standard:

#include<stdio.h>
int fflush(FILE*stream);   

Description: If stream points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined

If you want to implement the linked list data structure (like all the ADTs {vector,stack...}), generate header file and API and separate the API and the implementation details.

Adam
  • 2,820
  • 1
  • 13
  • 33