0

I first created a linear linked list using initlist, and then used listlength to return to 0, but there was a segmentation fault when debugging. Why? the code is here

#include<stdio.h>
#include<stdlib.h>
typedef struct LNode{
    int data;
    struct LNode *next;
}LNode,*LinkList;
int InitList(LinkList L);
int ListLength(LinkList L);
int main(void)
{
    LinkList L;
    InitList(L);
    printf("%d",ListLength(L));
    return 0;
}
int InitList(LinkList L){
    L=(LinkList)malloc(sizeof(LNode));
    if(!L)return 0;
    (L->next)=NULL;
    return 1;
}
int ListLength(LinkList L)
{
    int length=0;
    if((L->next)==NULL) return 0;
    struct LNode *x=(L->next );
    while(x){
        length++;
        x=x->next ;
    }
    return length;
}

the segmentation fault was thrown

if((L->next)==NULL) return 0;
Weng
  • 1
  • 1
    Welcome to SO. In C all parameters are passed by value, i.e. a copy is provided to the called function. Whatever you do to that parameter inside the function, will not have any effect outside that function. In your case, `L` will not be updated by `InitList`. This means you have an uninitialized pointer `L` that you pass to `ListLength` where it is dereferenced causing the problem. You should also add checks for `NULL` before dereferencing a pointer. (This will not help in this case) – Gerhardh Oct 30 '20 at 09:03
  • This may help too: https://stackoverflow.com/questions/766893/how-do-i-modify-a-pointer-that-has-been-passed-into-a-function-in-c – Jabberwocky Oct 30 '20 at 09:08
  • 2
    Unrelated, whomever told you hiding pointer types in typedef aliases (e.g `LinkList`) was a good habit couldn't be more wrong. There are two places where it is considerable: blackbox handle-type APIs, and callback function pointer types. This code exhibits *neither* of those attributes. – WhozCraig Oct 30 '20 at 09:09
  • 1
    Classic error. C uses pass by value. In other words: `IniyList` will not update `L` in `main` – Support Ukraine Oct 30 '20 at 09:10
  • If you define structure like `typedef struct LNode{ int data; struct LNode *next;}LNode, LinkList;` and use in main like `LinkList *L;` and n method declation use `int InitList(LinkList *L)` it should work – Pandey Amit Oct 30 '20 at 09:19

1 Answers1

-3

Even the first function InitList is incorrect and does not make a sense.

First of all it deals with a copy of the value of the pointer L declared in main used as an argument. So changing the copy within the function does not influence on the original pointer L. It stays uninitialized with an indeterminate value.

What you need is just to set the pointer L in main to NULL. This will be the initialization of the list.

LinkList L = NULL;

That is your approach when the list always has a dummy node that is pointed to by the pointer L that always is not equal to NULL is too complicated and only confuses readers of your code.

Correspondingly you will need to change the present check (the condition in the if statement) within the function ListLength to a check that the passed pointer is not equal to NULL because now the list does not have a dummy node.

Pay attention to that it is not a good idea to use a typedef like this

typedef struct LNode{
    int data;
    struct LNode *next;
}LNode,*LinkList;

For example the function ListLength does not change the list itself. So its parameter should be declared at least like

int ListLength( const LNode *L );

But if you will write

int ListLength( const LinkList L);

then it means a different declaration that is equivalent to

int ListLength( LNode * const L );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • His `ListLength` function would still crash. – kfx Oct 30 '20 at 09:11
  • @kfx You are wrong. He uses an approach when his list always has a dummy node that is not equal to NULL. – Vlad from Moscow Oct 30 '20 at 09:15
  • Can I change to int InitList(LinkList &L)? – Weng Oct 30 '20 at 09:19
  • @Weng As I wrote the function does not make a sense. There is no need to create a dummy node. What you need is to write in main LinkList L = NULL; That is enough. – Vlad from Moscow Oct 30 '20 at 09:21
  • You are correct to point out the problem of `InitList`, but I'm lost as why you think setting L to NULL is enough. I invite you to run and compile the code. – kfx Oct 30 '20 at 09:23
  • @kfx I wrote that it is enough to set the pointer to NULL as the initialization of the list instead of calling the function and creating a dummy node. – Vlad from Moscow Oct 30 '20 at 09:26