1

I'm having a problem debugging a linked lists program. It just crashes after the first few lines I figured it was probably a scanf problem and double checked but I still can't get it to run. It crashes in the middle of a function that creates a new node. Here is the code of the function and the main.

std* CreateNode()
{       
    std *newnd;
    char nm[20];
    double g;
    printf("\nCreating node\n");
    printf("\nEnter the student's name:\n");
    scanf("%s", &nm);
    printf ("\nEnter the student's GPA:\n");
    scanf("%lf", &g);
    strcpy( (newnd->name), nm);
    newnd->GPA = g;
    newnd->next = NULL;
    return newnd;
}

int main()
{
    list_head = (std*) malloc( sizeof(std) );
    list_tail=(std*) malloc( sizeof(std) );
    list_tail=(std*) malloc( sizeof(std) );

    list_head=CreateNode();
    A=CreateNode();
    B=CreateNode();
    C=CreateNode();
    PrintList(list_head);
    InsertBeg(A);
    InsertEnd(B);
    InsertMiddle(C);
    PrintList(list_head);
    SearchStudent();
    DeleteMiddle();
    DeleteEnd();
    DeleteBeg();
    PrintList(list_head);
    return 0;
}

When I run the program it stops executing right after I enter the gpa.

Any help would be very welcome I've tried everything I can think of. Thanks! :)

Telemachus
  • 19,459
  • 7
  • 57
  • 79
FaY
  • 35
  • 6

2 Answers2

2

You declare

std* newnd;

however you never allocate memory for it before you try to access it's members.

std* newnd = malloc( sizeof *newnd  );
clcto
  • 9,530
  • 20
  • 42
  • Better: `std* newnd = (std*)malloc(sizeof(newnd));` – zentrunix Mar 19 '14 at 16:01
  • @JoséX. Don't cast the result of malloc. http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc – clcto Mar 19 '14 at 16:02
  • sizeof is an operator not a function. no need for parens – Steve Cox Mar 19 '14 at 16:05
  • @SteveCox That is incorrect. In some cases you need parentheses. A good habit is to always include them. – this Mar 19 '14 at 16:06
  • you need parens only to resolve operator precidence, like all other operators. – Steve Cox Mar 19 '14 at 16:07
  • @self according to the page I linked, you only need them when it is a type – clcto Mar 19 '14 at 16:07
  • @clcto For consistency it is better to always go with the parentheses. Omitting them can cause an error, but always including them cannot. – this Mar 19 '14 at 16:08
  • @clcto: you're right, I'm confusing languages here, in C the cast is not needed. – zentrunix Mar 19 '14 at 16:11
  • @self including the parens for the expression variant is equivalent to including them for any of the other 8 unary operators. e.g. ++(var) – Steve Cox Mar 19 '14 at 16:17
  • @SteveCox That comparison is invalid since sizeof operator has a mode where the parenthesis are mandatory regardless of the actual code. – this Mar 19 '14 at 17:35
  • Enough of this. It is a coding-style. Just follow the same style as your team and everything will be ok. – clcto Mar 19 '14 at 17:51
0

in you program,

std *newnd;

is a pointer where did you allocate memory to it? you used the variable without allocating memory to it in

 strcpy( (newnd->name), nm);
 newnd->GPA = g;  
 newnd->next = NULL;

this results in program to crash. so allocate memory before using a variable.

LearningC
  • 3,182
  • 1
  • 12
  • 19