1

I'm trying to insert two random strings in each node, but when I print the list the output is not correct. What can it be? I'm not good at memory allocation, so if anything is wrong please explain me. I also tried to see if a string was overwriting another, but it doesn't seem to be the case.

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

struct node{
    int times;
    char name[100];
    char number[100];  
    struct node* next;
};

typedef struct node* node;

void mklist(node* n){
    *n=(node)malloc(sizeof(node*));
    (*n)->times=0;
    strcpy((*n)->name,"null");
    strcpy((*n)->number,"null");
    (*n)->next=(node)NULL;
}

void listins_beg(node* n,char name[],char num[],int tim){
    node a;
    a=(node)malloc(sizeof(node));
    if(a==NULL) exit(1);
    a->times=tim;
    strcpy(a->number,num);
    strcpy(a->name,name);
    a->next=(node)(*n);
    (*n)=a;
}

void printlist(node n){
    node x;
    x=n;
    if(x->next==NULL) printf("EMPTY LIST");
    else{
        do{
            printf("%s - %s\n",x->name,x->number);
            x=x->next;
        }while(x->next!=NULL);
    }
}

void freelist(node* n){
    node x;
    for(;x->next!=NULL;(*n)=(*n)->next){
        x=(*n);
        free(x);
    }
}

int main(void){
    node n;
    mklist(&n);
    listins_beg(&n,"Hermanouhuhuuteu","4523-2248",300);
    listins_beg(&n,"Luhu","4523-4887",299);
    listins_beg(&n,"Lulamolute","4523-4687",512);
    printlist(n);
    freelist(&n);
    return 0;
}
Rafael Santos
  • 39
  • 1
  • 6
  • 4
    `typedef struct node* node;` No, please, just *no.* This obfuscates your code *horribly.* It's best not to typedef pointers at all, so that semantics are immediately visible. But this is just asking for trouble. – Angew is no longer proud of SO May 25 '18 at 16:40
  • I tried using `typedef struct node node`, but it gave me a lot of errors and headaches :/ – Rafael Santos May 25 '18 at 16:45
  • 1
    Use more different identifiers and try again. – Yunnosch May 25 '18 at 16:50
  • It's quite hard to wrap one's head around pointers, at beginner and even intermediate programmer levels. Don't make it even harder by hiding the fact that pointers are involved. It's quite natural to assume `X` is a non-pointer type and `X*` is a pointer (for any `X`). You're making this harder for yourself by having to think about what is a pointer and what is not without the `*`s to help you. – Angew is no longer proud of SO May 25 '18 at 17:02

2 Answers2

1

You did typedef struct node* node in your code.But in your functions makelist and listins_beg you have used

 *n=(node)malloc(sizeof(node*));
 a=(node)malloc(sizeof(node));

Now here *n is a pointer to struct node but it is allocating memory only 8 byte or 4 byte depending on your machine as sizeof(node*) will return 8 or 4 because node* is a pointer to pointer to a node,same things happens while allocating memory for a.It should be rather like this

 *n=(node)malloc(sizeof(struct node)); //in makelist
 a=(node)malloc(sizeof(struct node));  //in listins_beg 
krpra
  • 466
  • 1
  • 4
  • 19
0

First, as angew points out you need to get rid of

  typedef node* node;

You need to review the basics of how structures work. For example in main you declare;

    node n;

Then in mklist(..) you try to allocate the struct. But your declaration already allocated it. If you want to allocate structures, declare pointers then allocate the struct and set the pointer to point to the new memory just allocated;

  node *p;
  p = malloc(sizeof(node));
john elemans
  • 2,578
  • 2
  • 15
  • 26