0

I am too bad now. My list is not working! I know that there is an issue of just coping my ptr into function, not actually using real one, but I can't understand how can I make this work as I want.

PS. I see also that if I make head as global value, it will be ok. But I want to get function, which I can call it specific list.

Here is function of adding element into a blamk list. I can't make even this function work. I tried to play with double pointers, but now I am here to ask some help.

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

struct node
{
    int data;
    struct node *next;
};

void add( int num,struct node * head )
{
    struct node *temp;
    temp=(struct node *)malloc(sizeof(struct node));
    temp->data=num;
    if (head== NULL)
    {
    head=temp;
    head->next=NULL;
    }
    else
    {
    temp->next=head;
    head=temp;
    }
}

int  main()
{
    struct node *head;
    head=NULL;
    add(20,head);
    if(head==NULL) printf("List is Empty\n");

    return 0;
}

UPD: MY own playing with double pointers:

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

struct node
{
    int data;
    struct node *next;
};

void add( int num, struct node **head )
{
    struct node **temp;
    temp=(struct node **)malloc(sizeof(struct node*));
    (*temp)->data=num;
    if (*head== NULL)
    {
    *head=*temp;
    (*head)->next=NULL;
    }
        else
{
(*temp)->next=head;
*head=temp;
}

}

int  main()
{
    struct node *head;
    head=NULL;
    add(20,&head);
    if(head==NULL) printf("List is Empty\n");

    return 0;
}
  • 3
    [casts are bad for malloc - see this](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Ed Heal Dec 11 '16 at 14:54
  • 5
    The use of `head` as a function parameter hides it from the other function. You should have played with double pointers some more. – Jongware Dec 11 '16 at 14:56
  • Possible duplicate of [Passing by reference in C](http://stackoverflow.com/questions/2229498/passing-by-reference-in-c) – Siguza Dec 11 '16 at 14:57
  • Inside `add` you change the value of `head`. Try to find a way how to pass the new value of `head` up the caller. – alk Dec 11 '16 at 15:01
  • 1
    What happens to `temp` is `head` is not null - kinda disappears - memory leak – Ed Heal Dec 11 '16 at 15:01
  • *head=temp;* in the add function doesn't do what you think is doing. The *head* pointer you are passing in the add function is actually a copy of you header. So *head=temp;* doesn't change the original but the copy one. You need to send the address of your header – γηράσκω δ' αεί πολλά διδασκόμε Dec 11 '16 at 15:01
  • `head=temp;` do not do what you intended, it do not actually swap pointer for main head – Logman Dec 11 '16 at 15:01
  • I know all these things. I just can't make double pointers work. Sad, but true. –  Dec 11 '16 at 15:11
  • 1
    Draw a diagram - I find this helps. Pointers being an arrow and the variable being a box – Ed Heal Dec 11 '16 at 15:14

3 Answers3

0

How do you expect it will work if you don't pass the address of head to the function?

Correct code:
add(20,&head)

And you also have to change your function signature with this:
void add(int num, struct node **head)

Also, to refer to your struct node pointer, again in the add function you'll want to change head with *head.

Please pay attention: **head points to *head, so everytime you want to apply changes to your pointer (not double pointer) head, you have to tell it to the compiler by using *head.

UrbiJr
  • 133
  • 1
  • 2
  • 20
0

How are you expecting it to work? It wont work unless you pass the pointer head itself, here you are just creating a copy of it. You can do this as well.

head = add(20,head);

Instead of just

add(20,head);

Add a

return head;

before ending the function

And don't forget to change the return type of your function like this

struct node* add( int num,struct node *head)

The updated code looks like this

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

struct node
{
    int data;
    struct node *next;
};

struct node* add( int num,struct node *head)
{
    struct node *temp;
    temp=malloc(sizeof(struct node));
    temp->data=num;
    if (head==NULL)
    {
        head=temp;
        head->next=NULL;
    }
    else
    {
        // please change your logic
    }
    return head;
}

int  main()
{
    struct node *head;
    head=NULL;
    head = add(20,head);
    if(head==NULL) printf("List is Empty\n");

    return 0;
}

Cheers!

sameera sy
  • 1,708
  • 13
  • 19
  • well, it is great idea. But I have played with double pointers too much. So, I want make them work too. and what is bad with logic in else? And I know that my code should not work! I just gave an example for u to play :) –  Dec 11 '16 at 15:41
  • @Dida Haha, I didn't know you did it purposefully. – sameera sy Dec 11 '16 at 15:46
0

Change

struct node **temp;
temp=(struct node **)malloc(sizeof(struct node*));
(*temp)->data=num;
if (*head== NULL)
{
    *head=*temp;
    (*head)->next=NULL;
}

To

struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
(temp)->data=num;
if (*head== NULL)
{
    *head=temp;
    (*head)->next=NULL;
}

The reason you are getting a segmentation fault, is because in malloc, you allocated sizeof(struct node*), which is basically enough size for a pointer and doesn't make any sense to do so.

Also, change the logic of the else in add, if you are planning to add in more nodes.

eyalm
  • 3,366
  • 19
  • 21
mahesh Rao
  • 375
  • 1
  • 4
  • 16