I've been trying to initialise a linked list in a for loop. Every iteration, I create a pointer to a node struct and point the last node in the list to it. However, something strange that happens is I keep getting a segmentation fault when trying to assign the value of the data field of the struct to 5 (second line in the for loop). Not sure why this is happening.
struct node{
  int data;
  node* next;
};
void CreateLinkedList(node* start, int numberOfNodes)
{
  int i = 0;
  node* tempo = start;
  for(i=0; i<numberOfNodes; i++){
      node* newNode;
      newNode->data = 5;
      newNode->next = NULL;
      while(tempo->next != NULL){
        tempo = tempo->next;
      }
      tempo->next = newNode;
  }
}
Something else I tried was using the "new" operator and it worked (again, not sure why):
void CreateLinkedList(node* start, int numberOfNodes)
{
  int i = 0;
  node* tempo = start;
  node* newNode;
  for(i=0; i<numberOfNodes; i++){
      newNode = new node;
      newNode->data = 5;
      newNode->next = NULL;
      while(tempo->next != NULL){
        tempo = tempo->next;
      }
      tempo->next = newNode;
  }
}
Any ideas?
PS: This is my very first post here!
 
     
     
    