#include <stdio.h>
#include <malloc.h>
struct node
{
    int data;
    struct node *next;
};
struct queue
{
    struct node *front;
    struct node *rear;
};
struct queue *q;
void create_queue(struct queue *);
int main()
{
    create_queue(q);
    ptr = (struct node*)malloc(sizeof(struct node));
    ptr->data = 42;
    q->front = ptr; <-- here it fails with Sementaion fault
    return 0;
}
void create_queue(struct queue *q)
{
    q = (struct queue*)malloc(sizeof(struct queue));
    q->rear = NULL;
    q->front = NULL;
}
And this fails on trying to assign ptr to queue front, but if i move the line q = (struct queue*)malloc(sizeof(struct queue)); to the main function before create_queue(q) everything works well. What the reason the Segmentation fault error depends on the memory allocation position in the code.
 
     
     
    