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 8849027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T12:32:40+00:00 2026-06-14T12:32:40+00:00

I am attempting to compare a user inputted search term with a number of

  • 0

I am attempting to compare a user inputted search term with a number of names in a linked list. I know for sure that it is segfaulting at strcmp but none of the solutions for segfaults at strcmp seem to be the problem.

Here is my code! I am still very new to StackOverflow & C so I apologize in advance for any dumb mistakes I make in the posting of this or in my actual programming. ><

struct node{
char* name;
struct node* next;
};

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main(){
char reader;
char srchbuff[1001];
char name[10] = "Justin";
char* srch;
int i;

struct node *head;
struct node *cur;

head = malloc(sizeof(struct node));
head->name = name;
head->next = 0;

for(i=0; i<1000; i++){
   scanf("%c", &reader);
   srchbuff[i] = reader;
}


srchbuff[i] = '\0';
srch = malloc(sizeof(char)*i);
strcpy(srch, srchbuff);

cur = head;

while( (cur != NULL) && (strcmp(cur->name, srch)) != 0){
    cur = cur->next;
}
}

There are other nodes allocated in a separate function, that works fine, and the information is also allocated in a separate function (it also works fine), and my struct is in my header file so it’s all happy and recognized.

I have also tested with gdb and printf statements to make sure the strcmp is where I’m segfaulting, and it definitely is. Thanks in advance for any suggestions 🙂

  • 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-14T12:32:42+00:00Added an answer on June 14, 2026 at 12:32 pm

    In the line

    srchbuff[i] = '\0';
    

    you write one byte beyond the end of srchbuff.

    The memory srch points to, is not initialized. So anything might happen.

    cur is also not initialized. This means cur points anywhere and so does cur->name.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to use a linked list implementation to modify another linked list. Here's
Attempting to make a NSObject called 'Person' that will hold the login details for
I'm attempting to sum daily purchase amounts for a given user. @dates is an
I'm attempting to compare two tables of equal length with a function, since I
I am attempting to write a Perl script that parses a log where on
I am attempting to filter a list based on a predicate Func. I use
This is a learning exercise. I'm attempting to augment memcpy by notifying the user
I'm attempting to write a basic dna sequencer. In that, given two sequences of
I am attempting to use a module called interface.py which defines a list of
I am attempting to make a program that will input numbers into an array

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.