1

i am trying to read and print one value in a linked list , but my program does not give any output, i have tryed checking where the program is failing to execute , after the first scanf the code is not printing anything, what might be the reason for that? code is as followed:

#include<stdlib.h>
#include<stdio.h>
void display();

struct ll{
    int val;
    struct ll* address;
};
struct ll *new=NULL,*start=NULL,*present=NULL;


int main(void)
{
    int num;
    scanf("%d",&num);
    //reading ll
    new=(struct ll*) malloc(sizeof(struct ll));
    new->val=num;
    new->address=NULL;
    
    if(start==NULL)
    {
        start=new;
        present=new;
       
    }
    else
    {
        present->address=new;
        present=new;
        
    }
   //calling display func to display the contents of ll
    display();
    
   
}
void display()
{
    present=start;
  // displaying.
    while (present!=NULL)
    {
        printf("%d",present->val);
        present=present->address;
    }
    printf("%d",present->val);
}
  • What resources are you using to learn C? What do they say about using global variables? If they say it's okay, then it's not a good resource and you should look for other books, tutorials or classes. – Some programmer dude Jun 25 '22 at 07:19
  • As part of the problem, after the loop `while (present!=NULL)`, what is the value of `present`? Will it be a valid pointer? – Some programmer dude Jun 25 '22 at 07:21
  • 1
    What do you think `printf("%d", present->val);` will do when the loop immediately previous terminated because the condition `while (present!=NULL)` was no longer true? That dereference of `present` in your print, when you *know* it is `NULL`, is a recipe for *undefined behavior*. – WhozCraig Jun 25 '22 at 07:21
  • 1
    @BhaveshNaiduKulluru please [edit] and show a simple example of input and expected output – Jabberwocky Jun 25 '22 at 07:40
  • 1
    You dereference a null pointer. That leads to *undefined behavior* and will make your whole program *ill-formed* and invalid. So no, the code is *not* correct. – Some programmer dude Jun 25 '22 at 07:41
  • I suggest you find some tutorial that deals with linked lists, there are plenty of them. Or maybe just read your lecture notes/leaning material. – Jabberwocky Jun 25 '22 at 07:42
  • 1
    Also, using `printf` for debugging can lead to false positives, if you forget to *flush* the output. If you truly want to debug your program, use a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems). – Some programmer dude Jun 25 '22 at 07:42

1 Answers1

1

I have encoded my comments interspersed into your code. I have commented the last statement in function display(), to make it run properly. I have also commented the cast to malloc() (for the given reasons in the code) I have also made some aesthetic changes to make the code more readable. You can add spaces to improve readability of the code, as they don't change the compiler produced code, so please, use enough spaces to make your code more readable (I've done this also to show who it is more readable now):

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

void display(void);

struct ll {
    int        val;
    struct ll *address;
};

struct ll *new     = NULL,
          *start   = NULL,
          *present = NULL;


int main(void)
{
    int num;

    scanf("%d", &num);
    //reading ll

    /* Never cast the returned value of malloc()  This allows to
     * detect if you have properly #include'd the header file and
     * avoids other dificult to find errors.  malloc() returns a
     * void * pointer, so it will be automatically converted to
     * any other pointer type without risk. */
    new          = /* (struct ll*) */ malloc(sizeof(struct ll));
    new->val     = num;
    new->address = NULL;

    if(start == NULL)
    {
        start   = new;
        present = new;

    }
    else
    {
        /* this is never executed, as start == NULL at program
         * start. */
        present->address = new;
        present          = new;

    }
    //calling display func to display the contents of ll
    display();
    /* while it is not necessary for main() it is normal for a function 
     * that is defined to return an int value, to return something, so
     * I added the following statement: */
    return 0; 
}

void display(void)
{
    present = start;
  // displaying.
    while (present != NULL)
    {
        printf("%d",present->val);
        present = present->address;
    }
    /* as you have moved present in a while loop until the while
     * condition is false, at this point you must assume the
     * condition is false (so present == NULL) and you are trying to
     * dereference a NULL pointer below */
    /* printf("%d", present->val); */
}

Now your program will run and show the only value (I recommend you to put a \n character at the end of the printf() call, to put the printed data in a line by itself.

Luis Colorado
  • 10,974
  • 1
  • 16
  • 31