1

when I'm trying to add a new node to my linked list the compiler stop working and fails to add the node , I can't find out the problem is it in the logic or in the syntax

struct Record* CreateNode() {
    struct Record* PointerToRecord ;
    PointerToRecord = (struct Record*) malloc(sizeof(struct Record*));
    if (PointerToRecord) {
        PointerToRecord->C = FillDataOfContacts();
        PointerToRecord->Next = NULL;
        PointerToRecord->Prev = NULL;
    }
    return PointerToRecord  ;
}

struct Record* AddNode() {
    if (Head == NULL && Tile == NULL) {
        Head = Tile = CreateNode();
    } else {
        struct Record* Pointer ;
        Pointer = CreateNode();
        Tile->Next = Pointer ;
        Pointer->Prev = Tile ;
        Pointer->Next = NULL;
        Tile = Pointer ;
    }
}
Stephen Docy
  • 4,738
  • 7
  • 18
  • 31
AlameerAshraf
  • 872
  • 1
  • 13
  • 23
  • you have to show error messages, saying 'the compiler stops working' is not useful – pm100 Nov 18 '16 at 17:43
  • that is what actually happened ! code blocks stop working it is accidently closed ! – AlameerAshraf Nov 18 '16 at 17:45
  • @AlameerAshraf Please add a [Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve), as well as reading about [how to debug small programs](http://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Random Davis Nov 18 '16 at 17:47
  • Please post a [MCVE], that means your complete code so we can possibly reproduce your problem on out computers. – Jabberwocky Nov 18 '16 at 17:47

1 Answers1

3

In your struct Record* CreateNode() function, you have:

PointerToRecord = (struct Record*) malloc(sizeof(struct Record*));

Here you are not assigning enough memory to store a struct record instead you are assigning memory to store a struct record*

try assigning this way:

PointerToRecord = (struct Record*) malloc(sizeof(struct Record));

Additionally, you need not cast the return value of malloc : Here's why (click)

so you can allocate your pointer in the following manner

PointerToRecord = malloc(sizeof(struct Record));
Community
  • 1
  • 1
Cherubim
  • 5,287
  • 3
  • 20
  • 37