'How to remove segmentation error from this code?

This is my code in c++ of queue using array

#include <iostream>
#include <cstdlib>
using namespace std;

struct queue
{
    int size;
    int f;
    int r;
    int *arr;
};

int isFull(struct queue *q)
{
    if (q->r == q->size - 1)
    {
        return 1;
    }
    else
        return 0;
}

int isEmpty(struct queue *q)
{
    if (q->r == q->f)
    {
        return 1;
    }
    else
        return 0;
}

void enqueue(struct queue *q, int val)
{
    if (isFull(q))
    {
        cout << "Queue is full, cannot enqueue\n";
    }
    else
    {
        q->r++;
        q->arr[q->r] = val;
    }
}

int dequeue(struct queue *q)
{
    int a = -1;
    if (isEmpty(q))
    {
        cout << "Queue is empty, cannot dequeue\n";
    }
    else
    {
        q->f++;
        a = q->arr[q->f];
    }
    return a;
}

int main()
{
    struct queue *q;
    q->size = 100;
    q->f = q->r = -1;
    q->arr = (int *)malloc(q->size * sizeof(int));

    enqueue(q, 5);
    enqueue(q, 65);
    enqueue(q, 49);
    enqueue(q, 26);

    cout << "The dequeued element is " << dequeue(q) << "\n";
    return 0;
}

I have tried to implement queue using array but I am a getting segmentation error as runtime error

I tried the same concept in c language and it worked fine for me but not in c++

I have also tried different IDE but the result is the same

Please help me solve this



Solution 1:[1]

Replace:
struct queue *q;
to

//struct;
queue* q = new queue;

struct queue is implicitly declared queue type.
On the sidelines: check (f > r) in dequeue.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 zpc