0

I created a linked list program it works perfect with ints in c. but if change the parameter to char array, and try to do a strcpy it causes a core dump.

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

struct node {
    char mac[25];
    struct node * next;
};
typedef struct node *list;

int main(void) {

   lista c;

   c = creoLista();
   c = insert_start(c, "aa:bb:cc:dd:e1");
   c = insert_start(c, "aa:bb:cc:dd:e2");
   c = insert_start(c, "aa:bb:cc:dd:e3");

   showList(c);
   return 0;
}

list createList() {
   return NULL;
}

list insert_start(list l1, char val[]) {
    list n;
    n =(list )malloc(sizeof(list));
    strcpy(n->mac,val);
    printf("ADDED: %s en ADDRESS:%p NEXT ADDRESS: %p\n", n->mac,(void *)(&n), (void *) (&n->next));
    n -> next = l1;

    return n;
}

void showList(list l1) {
     while (l1 != NULL){
         printf("Value: %s Address: %p\n",l1 -> mac,(void *) (&l1 -> next) );
         l1 = l1 -> next;
    }
}

Any hint on what im doing wrong and why it works with ints and not a char array

thanks

S.Regusci
  • 63
  • 6
  • You have no check for a possible null pointer anywhere in your code. – Iharob Al Asimi Feb 16 '15 at 17:05
  • 1
    [Please don't cast the result of `malloc()`](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858). – Quentin Feb 16 '15 at 17:06
  • This `malloc(sizeof(list))` allocates space to hold a pointer. May be you need this `malloc(sizeof(struct node))` to allocate space to hold a node rather than a pointer to a node? – Igor S.K. Feb 16 '15 at 17:07
  • Wheres `creoLista()` defined? Also, you don't need to cast the printf either –  Feb 16 '15 at 17:10

3 Answers3

1

The problem is this allocation:

malloc(sizeof(list))

It shows the problem with making type-aliases of pointers as you here only allocate the size of a pointer and not the whole structure.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0
  1. Your allocation is wrong, because you made it confusing by typedefing a pointer, don't do that

    n = malloc(sizeof(*n));
    

    is less error prone.

  2. Check the return value of malloc() and there is no need for the cast, so

    n = malloc(sizeof(*n));
    if (n == NULL)
        return NULL;
    
  3. You are printing the n->next pointer's address before initializing it, change this

    printf("ADDED: %s en ADDRESS:%p NEXT ADDRESS: %p\n", n->mac,(void *)     (&n), (void *) (&n->next));
    n->next = l1;
    

    to

    n->next = l1;
    printf("ADDED: %s en ADDRESS:%p NEXT ADDRESS: %p\n", n->mac, (void *)n, (void *)n->next);
    
  4. You have no function prototypes, so your compiler is using implicit function declaration, which is bad, because it will assume that all the functions return int, so you need to add these before the definition of main()

    list createList();
    list insert_start(list l1, char val[]);
    void showList(list l1);
    

    specially the first 2 are very important, enable compiler warnings to prevent this.

This is your code fixed:

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

struct node {
    char mac[25];
    struct node * next;
};
typedef struct node *list;

list insert_start(list l1, char val[]);
void showList(list l1);

int main(void) {
    lista c;

    c = insert_start(NULL, "aa:bb:cc:dd:e1");
    c = insert_start(c, "aa:bb:cc:dd:e2");
    c = insert_start(c, "aa:bb:cc:dd:e3");

    showList(c);
    return 0;
}

list insert_start(list l1, char val[]) {
    list n;

    n = malloc(sizeof(*n));
    if (n == NULL)
        return NULL;
    strcpy(n->mac, val);
    n->next = l1;

    printf("ADDED: %s en ADDRESS:%p NEXT ADDRESS: %p\n", n->mac, (void *)n, (void *)n->next);
    return n;
}

void showList(list l1) {
     while (l1 != NULL) {
        printf("Value: %s Address: %p\n", l1->mac, (void *)l1->next);
        l1 = l1->next;
    }
}

and you need a freeList() function too.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
0

typedef struct node *list;

n =(list )malloc(sizeof(list));

list is pointer to struct node, malloc() should be passed the size of valid bytes to perform strcpy and list could be just 8 bytes if you are working on 64 but machine. Change malloc() allocation to size of pointer points to.

Sunil Bojanapally
  • 12,528
  • 4
  • 33
  • 46