Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8892797
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:05:51+00:00 2026-06-14T23:05:51+00:00

I am having trouble with this code. I am new to C and as

  • 0

I am having trouble with this code. I am new to C and as far as I can tell I am using the malloc operation correctly.

#include "fifo.h"
#include <stdlib.h>

/* add a new element to a fifo */
void Enqueue( fifo* queue, int customerId)
{
   //allocate memory for the element being added
   //initialize fifo_element
   fifo_element *temp;
   temp = (fifo_element*)malloc(sizeof(fifo_element));
   temp->customerId = customerId;
   temp->prev = NULL;
   temp->next = NULL;

   //if the queue is empty, add the element to the start
   if(&queue->head == NULL){
      queue->head = queue->tail = temp;
      return;
   }
   else{
      queue->tail->next = temp;
      temp->prev = queue->tail;
      queue->tail = temp;
      return;      
   }   
}

I am unable to perform this operation without getting a segmentation fault:

queue->tail->next = temp;

I can’t seem to come up with a solution or a work around to not use this line of code. Can anyone help explain why this line of code will not work? Thanks in advance.

Also, here is the fifo and fifo_element structure:

struct fifo_element
{
   int customerId;
   fifo_element *next;
   fifo_element *prev;
};

struct fifo
{
   fifo_element *head;
   fifo_element *tail;
};

and here is my call when Enqueuing:

Enqueue( &f, i ); //f is of type fifo
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T23:05:52+00:00Added an answer on June 14, 2026 at 11:05 pm
    if(&queue->head == NULL){
    

    In this line you check the address of the element head in your fifo. This is probably not what you want. Instead, you want to check whether the value of your pointer is valid:

    if(queue->head == NULL){
    

    Also keep in mind that you have to initiate the fifo with the correct values:

    fifo f;
    f.head = 0;
    f.tail = 0;
    Enqueue( &f, 1 );
    

    And you should check whether malloc actually returns a valid address:

    temp = (fifo_element*)malloc(sizeof(fifo_element));
    if(temp == NULL){
         /* insufficient memory, print error message, return error, etc */
    } else {
         /* your code */
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble copying text using zeroclipboard. This is my html code: <dl
I'm having trouble with this bit of code. I'm new to C#, but from
I am still new to mocking and I am having trouble with this code:
I'm a beginner in asp, and I am having trouble with this code. I
I am having trouble getting my datasource linked to my repeater through this code
I am having trouble figuring out where my issue with this code is. My
I am having trouble moving this JLabel across this JPanel? I put the code
I am having trouble getting this invisibilityOfElementLocated() method to work. Here is the code
I found this snippet of code I am having trouble trying to find a
I'm having trouble figuring out how to implement the code from this answer. Android:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.