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

  • Home
  • SEARCH
  • 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 9201229
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:57:19+00:00 2026-06-17T22:57:19+00:00

I’ve been stuck on a segmentation fault of a long time. I declared a

  • 0

I’ve been stuck on a segmentation fault of a long time. I declared a struct with a pointer to a string. I wrote two functions, create and remove to manipulate values. The struct is as follows:

#include "filename.h"  
//*in filename.h:* typedef struct linkNode linkNode_t;

struct linkNode{
    struct linkNode *next;
    char *value;
};

The create function will first allocate memory for the node, then allocate memory for the value, and then copy the input value into the value field:

linkNode_t* create(char* stuff){
    linkNode_t *ptr=malloc(sizeof(linkNode_t));
    if(ptr==NULL){
        printf("malloc failure");
        return NULL;
    }
    char* tempvalu=malloc(sizeof(char)*strlen(stuff)+1);
    if(tempvalu==NULL){
        printf("malloc failure");
        return NULL;
    }
    strcpy(tempvalu,stuff);
    ptr->next=NULL;
    ptr->value=tempvalu;
    return ptr;
}

A function is used to insert a node into the linked list:

linkNode_t* insertLast(linkNode_t* start, linkNode_t* newNode){
    linkNode_t* current=start;
    while(current->next!=NULL){
        current=current->next;
    }
//now current points to the last element in the linked list
    current->next=newNode;
    return start;
}

The part causing me problem is as follows:

linkNode_t* removebyValue(linkNode_t* start, char* valu){
/**removes the first instance of a node with a certain value. Return *start after     removing.
    if linked list becomes empty, return NULL*/

    linkNode_t *current=start;
    linkNode_t *previous=start;
    while(current!=NULL){
        if(strcmp(valu,current->value)==0) {//found the node to delete
            if(current==start){//removing the head
                linkNode_t* retvalue= current->next;
                free(current->value);
                free(current);
                return retvalue;
            }
            else{   //removing other elements in the linked list
                previous->next=current->next;
                free(current->value);
                free(current);
                return start;
            }
        }
        else{
            previous=current;
            current=current->next;
        }
    }
    return start;
}

In the Main I created a linked list of two elements,1 and 2, and tried to free element 1 when segmentation fault occured.

int main(){
    linkNode_t *pt1=create("1");
    pt1=insertLast(pt1,create("2"));
    removebyValue(pt1,"1"); //Causes seg fault. If I replace "1" by "2" nothing happens 

Can someone give some suggestions on this? Thanks in advance

EDIT: I put all the code that could be related since someone said the sections I put on didn’t have an error

  • 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-17T22:57:20+00:00Added an answer on June 17, 2026 at 10:57 pm

    I think you’re over-thinking the removal of a node while maintaining the start pointer properly. Consider a hopefully simpler approach.

    typedef struct node_t 
    {
        struct node_t* next;
        char* value;
    } node_t;
    
    node_t* remove(node_t *start, const char* valu)
    {
        node_t* current=start;
        node_t* prev=NULL;
    
        while(current && strcmp(current->value, valu))
        {
            prev = current;
            current = current->next;
        }
    
        if (current)
        {
            if (prev) // we're not deleting start node
                prev->next = current->next;
    
            else      // we *are* deleting start node
                start = current->next;
    
            // now the node is unlinked. remove it.
            free(current->value);
            free(current);
        }
        return start;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I've tracked down a weird MySQL problem to the two different ways I was
I have been unable to fix a problem with Java Unicode and encoding. The
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string

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.