0

I wrote this C code from a pointers practice video and the problem I am having is that after integrating head as a local main variable over a global variable declared before main(), the program is no longer entering the for loop and I can't seem to figure out why. The interesting part is that I delete the head=NULL, the loop seems to be executing. Does anyone know where the problem with assigning NULL to head is?

Here is my code:

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

struct Node{
    int data;
    struct Node* next;
};

struct Node* Insert(struct Node* head, int data){
    struct Node* new_node = (struct Node*)malloc(sizeof(struct Node));    
    (*new_node).data = data;
    (*new_node).next = head;

    head = new_node;
}

void Print(struct Node* head){
    struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
    temp = head;

    while(temp!=NULL){
        printf("%d", (*temp).data);
        temp = (*temp).next;
    }

    printf("\n");
}

int main(){
    struct Node* head = NULL;

    int n, i, x;
    printf("%s", "How large do you want your array to be?\n");
    scanf("%d", &n);

    for(i; i<n; i++){
        printf("Enter a number:\n");
        scanf("%d",&x);
        head = Insert(head, x);
        Print(head);    
    }
    return 0;
}
klutt
  • 30,332
  • 17
  • 55
  • 95
callmeGuy
  • 944
  • 2
  • 11
  • 28
  • 1
    Not necessarily relevant, but why are you using `malloc`, storing the pointer in `temp`, then immediately overwriting `temp`? That's a memory leak. – Carcigenicate Jun 01 '19 at 21:35
  • 2
    Turn on compiler warnings and read them. Your code gave me two warnings. – klutt Jun 01 '19 at 21:39
  • @Carcigenicate, you are right, that was actually part of the problem – callmeGuy Jun 01 '19 at 21:48
  • @xing, oh, right. that is so annoying because my compiler does not say anything about htis – callmeGuy Jun 01 '19 at 21:48
  • @Carcigenicate, on a second thought, I don't think this is necessarily a memory leak, because it is a function variable, so as soon as the function terminates, variable temp is freed. – callmeGuy Jun 01 '19 at 22:00
  • @callmeGuy It's not `temp` that's leaked, it's the memory allocated by the call to `malloc` that's leaked. The problem is not the pointer, it's the thing it points to. – David Schwartz Jun 01 '19 at 22:09
  • @DavidSchwartz, oh, yes, got it. now I see – callmeGuy Jun 01 '19 at 22:13

1 Answers1

2

I can see a couple of problems with your code:

struct Node* temp = (struct Node*)malloc(sizeof(struct Node));
temp = head;

should be

struct Node* temp = head;

Then we have

for(i; i<n; i++)

that should be

for(i=0; i<n; i++)

This can be the cause of your problem, since this is undefined behavior.

Another thing that can cause trouble is that you don't do return head as a final statement in Insert.

Two of these could be easily discovered by turning on compiler warnings.

Also, don't cast malloc

klutt
  • 30,332
  • 17
  • 55
  • 95
  • that is it indeed, thanks. My compiler did not detect me not assigning i=0 and I've overlooked that, and yes, that memory leak from temp variable. – callmeGuy Jun 01 '19 at 21:51