1

I manipulated my code to be able to use pred_p but have run into problems since. My code stops at line "pred_p->next_p = temp_p;" and gives me the message "Thread 1: EXC_BAD_ACCESS (code=1, address=0x8).Not sure where to go from here.

struct list_node_s {
    int    data;
    struct list_node_s* next_p;
};

struct list_node_s* Insert(struct list_node_s* head_p, int val);
void Print(struct list_node_s* head_p);
char Get_command(void);
int  Get_value(void);

/*-----------------------------------------------------------------*/
int main(void) {
    char command;
    int  value;
    struct list_node_s* head_p = NULL;
    /* start with empty list */

    command = Get_command();
    while (command != 'q' && command != 'Q') {
        switch (command) {
            case 'i':
            case 'I':
                value = Get_value();
                head_p = Insert(head_p, value);
                break;
            case 'p':
            case 'P':
                Print(head_p);
                break;
            default:
                printf("There is no %c command\n", command);
                printf("Please try again\n");
        }
        command = Get_command();
    }

    return 0;
}  /* main */


/*-----------------------------------------------------------------*/
struct list_node_s* Insert(struct list_node_s* head_p, int val) {
    struct list_node_s* curr_p = head_p;
    struct list_node_s* pred_p = NULL;
    struct list_node_s* temp_p;

    while (curr_p != NULL) {
        if (curr_p->data >= val)
            break;
        pred_p = curr_p;
        curr_p = curr_p->next_p;
    }

    // Create new node
    temp_p = malloc(sizeof(struct list_node_s));
    temp_p->data = val;
    temp_p->next_p = curr_p;
    if (pred_p = NULL) {
        head_p = temp_p;
    }
    else {
        pred_p->next_p = temp_p;
    }

    return head_p;
}  /* Insert */
3
  • Is your code multi-threaded? It might be due to a concurrent access Commented Sep 19, 2014 at 21:35
  • You may find this alternative an interesting piece of code to walk through. Commented Sep 19, 2014 at 21:42
  • There are lots of problems with your code, here is one: the line if (pred_p = NULL) { is assigning NULL to pred_p rather than comparing pred_p to NULL. (a common mistake and the reason the literal value should be written first, so the compiler would catch the problem. Commented Sep 22, 2014 at 7:16

1 Answer 1

2
 if (pred_p = NULL)

This should be

if (pred_p == NULL)

You technically repeated yourself, as one =, simply assigned NULL to pred_p again

Also

You need to allocate memory to pred_p using pred_p=malloc(sizeof struct list_node_s).

The above would only have worked as it is if head_p wasn't NULL, meaning curr_p wouldn't be NULL and in turn pred_p but then you would never have noticed the pitfall.

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

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.