2

I've been trying to add a new node into a linked list of profiles (for ex. facebook profiles), and I'm getting a runtime error while launching. This is what I got :

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

typedef struct friend {
    char *name;
    int age;
    char gender;
    struct friend* next;
} friend;

void node_add(friend* new);

int main(int argc, char *argv[]) {
    friend amit;
    friend *new;

    amit.name = "amit";
    amit.age = 16;
    amit.gender = 'm';

    node_add(new);
    new->name = "amit";

    printf ("name: %s\n", new->name);
    system("PAUSE");    

    return 0;
}

void node_add(friend* new) {
    new = (friend* )malloc(sizeof(friend));
    friend* head = new;
    new -> next = head;
}

I'm trying now to create a delete node function. I tried to find which node does the user wants to delete, and then delete it by doing

delete -> next = delete -> next -> next

The problem is, I need to get for the first node in the list. Here is what I wrote:

void node_delete(friend* delete) {
    friend *temp;
    char name[256];
    int i = 0, j = 0;

    printf ("Please enter the friend's name you want to delete: \n");
    fgets (name, 256, stdin);
    fgets (name, 256, stdin);

    while (0 == (strcmp(temp -> next -> name, delete -> next -> name))) {
        temp = friend -> next;
    }
    temp -> next = temp -> next -> next;
    free (delete);
}
7
  • When I try to run the program, I'm getting a run time error... If this isnt what you ment for, then I'm not sure how to see the exact error... Commented Apr 27, 2012 at 9:59
  • 1
    Why do you set the 'next' pointer to the node itself? Commented Apr 27, 2012 at 10:01
  • I guess you are trying to ad node at start of list. Commented Apr 27, 2012 at 10:16
  • Sorry for the misunderstanding, I tried to add a node to the start of the list... Commented Apr 27, 2012 at 10:59
  • I added a function I wrote to delete a node, though I had a problem.. I tried to reach the first node in the list(the head), and couldent do it... Commented Apr 27, 2012 at 11:53

5 Answers 5

1

Edit:

It seems my test was a mite too quick, because there is in fact a pretty serious problem with this code, but it's subtle:

In main() you are never actually pointing new at anything. It's just a garbled pointer out into memory space, which might sometimes work, and most of the time, is just terrible.

friend *new; // here's your problem; change this to:
friend *new = malloc(sizeof(friend));

Also, never cast the results of malloc.

Reedit:

How a very simple linked list implementation might look:

typedef struct _node node;
struct _node {
  void *payload;
  node *next;
};

node *create_node () {
  node *retval = malloc(sizeof(node));

  retval->payload = NULL;
  retval->next = NULL;

  return retval;
}

node *add_node (node *target) {
  if (target->next)
    return;

  node *next = create_node();
  node->next = next;
}

node *node_search (node *haystack, void *needle) {
  while (haystack) {
    if (!compare(needle, haystack->payload)) {
      return haystack;
    } else {
      haystack = haystack->next;
    }
  }

  return NULL;
}

Implementation of deletion and insertion are left as an exercise to the reader.

Sign up to request clarification or add additional context in comments.

2 Comments

@AmitSegal: Then you are going to need to post the exact error that you are getting.
@AmitSegal: Are you saying that the specific error you are getting is "Runtime error"?
1

Of course, you alloc memory and assign it to local variable. If you want to change pointer, pass pointer with one more asterix. And, by the way, do not name anything like friend or new. Its keywords in C++, and it create not needed problems.

Comments

1

You should have friend *head global.

And in the

void node_add(friend* new) 
{
    new = (friend* )malloc(sizeof(friend));
    new->next = head;
    head = new; 
} 

Comments

0

You should use a double-pointer.

void node_add(friend **new) {
    *new = malloc(sizeof(friend));
    /* etc */
}

2 Comments

This is not required, as the node is added to new, and not returned by reference.
Oh, what do you know. He is returning it by reference. I don't think that's intentional, tho'.
-1

The issue is in the following line: amit.name = "amit";

You should be a malloc and doing a strcpy()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.