2

I have this problem when I input. The program will freeze and a pop out window will open and says " .exe has stopped working. "

It is just a simple insert and display fuction of a singly linked list. I tried everything. I rewrote the code and find another way of inserting. I tried different compiler.. It works on turbo C but I am using devC++.

  • Is this a compiler error?

Here is the code:

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include<windows.h>
#include <string.h>

typedef struct process
{
    int pNum;
    struct process *next;
}node;


node *create_node(node x)
{
    node *temp;
    temp=(node *)malloc(sizeof(node));

    if(temp==NULL)
    {
        return 0;
    }
    else
    {

        temp->pNum=x.pNum;
        temp->next=NULL;


    }
    return temp;
}


node *insert_node(node *head,node *last,node x)
{
    node *temp;

    temp=create_node(x);

    if(head==NULL && head==last)
    {
        head=temp;
        last=temp;
        head->next=NULL;
        last->next=NULL;
    }
    else
    {
        last->next=temp;
        last=temp;
        last->next=NULL;

    }

    return head;
}

int main()
{
    node *head=NULL,*last=NULL,*temp,input;
    int i,x,y,num;

    printf("INPUT NUMBER: ");
    scanf("%d",&num);


    x=0;
    y=6;

    for(i=0;i<num;i++)
    {

        gotoxy(39,y);
        scanf("%d",&input.pNum);

        head=insert_node(head, last, input);

        y++;
    }
    getch();
    return 0;
}

I think I have found out what line it stopped working. On the function insert_node

The line last->next=temp;

It seems I can't find what I had done wrong.

guwop69
  • 119
  • 1
  • 13
  • 1
    possible duplicate of [pointer to pointers for singly linked list in C](http://stackoverflow.com/questions/18083593/pointer-to-pointers-for-singly-linked-list-in-c) – Rachit Ahuja Jul 29 '15 at 13:13
  • For now, just an integer. – guwop69 Jul 29 '15 at 13:14
  • When you call `insert_node(head, last, input)`, you expect `last` to be modified, but it isn't. – Jabberwocky Jul 29 '15 at 13:15
  • @RachitAhuja pointer to pointer? Maybe its because of my function returning a pointer? I have tried another way with just void. Still did not work for me. – guwop69 Jul 29 '15 at 13:16
  • The reason why I say it is maybe a compiler error because I have used this code many times using Turbo C only. But now, I am using devC++. – guwop69 Jul 29 '15 at 13:28
  • When a sub function is to change the contents of a pointer, where that pointer is a passed in parameter, then the function needs to be called with function( & pointer ) and the prototype needs to be function( type ** pointer ). in this case, both 'head' and 'last' need to be ' ** ' remembering that C passes by value, so the code is only changing the 'copy' of the original pointer, By passing 'pointer to pointer' the code in the function can de-reference the pointer to change the actual pointer in the callers' code – user3629249 Jul 29 '15 at 13:57

2 Answers2

2

In your code, after the first entry head pointer will point to new value. Because of you are assigning the return value of that calling function. But last value will not be affect. Because of your are calling that as a pass by value. At next time head == last will be fail.

It will go to else block, and you are accessing the

 last->next=temp;

It is like accessing the null pointer this is the reason. If you need to avoid this you need to call last as pass by reference.

Karthikeyan.R.S
  • 3,991
  • 1
  • 19
  • 31
  • I will try. The reason why I say it is maybe a compiler error because I have used this code many times using Turbo C only. But now, I am using devC++. – guwop69 Jul 29 '15 at 13:28
2

You need this:

node *insert_node(node *head, node **last, node x)
{
    node *temp;

    temp=create_node(x);

    if(head==NULL && head== *last)
    {
        head=temp;
        *last=temp;
        head->next=NULL;
        (*last)->next=NULL;
    }
    else
    {
        (*last)->next=temp;
        (*last)=temp;
        (*last)->next=NULL;

    }

    return head;
}

Call like this:

head=insert_node(head, &last, input);

Your function needs to modify the last pointer. In C values including pointers are passed by value. So if you modify the function argument inside the function, the argument passed to the function won't be modified. That is what happens in your program.

In the modified we don't pas simpty the lastpointer but we pass a pointer to the last pointer which will allow us to modify the last pointer of the main function.

Simple Example:

int func1(int x)
{
  x = 10;
  return 2;
}

int func2(int *x)
{
  *x = 10;
  return 2;
}

...
int x = 3;
printf ("%d\n", func1(x));
printf ("%d\n", x);
printf ("%d\n", func2(&x));
printf ("%d\n", x);

Will print:

2
3
2
10
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
  • At the first input, the window will popup. ".exe stopped working" – guwop69 Jul 29 '15 at 13:26
  • The reason why I say it is maybe a compiler error because I have used this code many times using Turbo C only. But now, I am using devC++. – guwop69 Jul 29 '15 at 13:29
  • It works fine here. I'm sure it's not a compiler error. Even Turbo C should compile simple code like this correctly. If forgot to mention how you have to call the function, I just edited my answer. – Jabberwocky Jul 29 '15 at 13:30
  • It works. Now I dont understand, I used the wrong code many times on my projects. It works perfectly. – guwop69 Jul 29 '15 at 13:34
  • You probably got struck by _undefined behaviour_. Feel free to accept the answer. – Jabberwocky Jul 29 '15 at 13:35