'I keep getting segmentation fault: 11

In C, I keep getting the error segmentation fault: 11. I am trying to do priority queues to count the frequency of each character In a file. Here is my Code...

This is the code to all of my priority queue, functions, I believe that my problem is in printQueue that is giving me set fault 11, but I am not sure.

//pq.h

#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>


#ifndef PQ_H
#define PQ_H

struct pair //struct to store frequency and value 
{
    int frequency;
    char value;
};

struct Qnode
{
    struct pair nodeValue;
    struct Qnode *next;
};

struct Qnode *popQueue(struct Qnode **front)
{
    struct Qnode *pop = *front;

    *front = (*front) -> next;

    free(pop);
}

void pushQueue(struct Qnode **front, struct Qnode *newQnode)
{
    struct Qnode *n1 = *front;
    struct Qnode *n2 = newQnode;
    
    if (!(*front))
    {
        (*front) = newQnode;
    }
    else{

        if((*front)->nodeValue.frequency < newQnode->nodeValue.frequency)
        {
            n2->next = *front;
            *front = n2;
        }
        else
        {
            while(n1->next != NULL && (n1->nodeValue).frequency < (newQnode->nodeValue).frequency)
            {
                n1 = n1->next;
            }

            n2->next = n1->next;
            n1->next = n2;
        }
    }
    
    //newQnode->next = front;
   // front = newQnode;

}

struct Qnode *createQnode(struct pair Pairs)
{
    struct Qnode *p = malloc(sizeof(struct Qnode));

    p->next=NULL;
    p->nodeValue = Pairs;

    return p;
}

void printQueue(struct Qnode *front)
{
    struct Qnode *nodePtr = front;
    while (nodePtr != NULL)
    {
        printf("%c: %d\n", (nodePtr->nodeValue).value,(nodePtr->nodeValue).frequency);
        nodePtr = nodePtr->next;
    }
}

struct Qnode *isEmpty(struct Qnode **front)
{
    return *front;
}

#endif

This is the code for my main function where I use command line to take in a text file and read through the data, then I call create createQnode(), pusQueue(), popQueue, printQueue.

//frequency.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>

#include "pq.h"

int main(int argc, char *argv[]) //command line takes in the file of text
{
    struct pair table[128]; //set to 128 because these are the main characters
    

    int fd; // file descriptor for opening file
    char buffer[1]; // buffer for reading through files bytes

    fd = open(argv[1], O_RDONLY); // open a file in read mode
    
    for(int j = 0; j < 128; j++)//for loop to initialize the array of pair (struct)
    {
        table[j].value = j; // table with index j sets the struct char value to equal the index
        table[j].frequency = 0; // then the table will initialize the frequency to be 0
    }

    while((read(fd, buffer, 1)) > 0) // read each character and count frequency
    {
          int k = buffer[0]; //index k is equal to buffer[0] with integer mask becasue each letter has a ASCII number.
          table[k].frequency++; //using the struct pair table with index k to count the frequency of each character in text file
    }

    struct Qnode *pq;

    printf("Pushing Queue\n");

    for (int i = 0; i < 128; i++)
    {
        struct Qnode *newnode = createQnode(table[i]);
        pushQueue(&pq, newnode);
    }


    for (int i = 0; i < 128; i++)
    {
         printQueue(&pq);
        
        popQueue(&pq);
    }



    close(fd); // close the file

    
    
    return 0; //end of code
} 


Sources

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

Source: Stack Overflow

Solution Source