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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:04:56+00:00 2026-06-09T15:04:56+00:00

I keep hitting a segmentation fault when calling anything that reads from stdin. I

  • 0

I keep hitting a segmentation fault when calling anything that reads from stdin. I have no idea why. Calling getchar() or any other similar function from within a certain function causes my program to crash, but when I call it from another function, it works fine. Here’s the part that crashes:

int prompt()
{
  int i;
  int selection = -1;
  while (selection < 0 || selection > 9) {
    printf("Item:\n\n");
    for (i = 0 ; i < 10 ; i++) {
      printf("%d) %s\n", i, getItemName(i));
    }

    for (i = 0 ; i < 11 ; i++) {
      printf("\n");
    }

    printf("Select the number of the corresponding item: ");
    char input = getchar(); <--- dies here!
    if (input != EOF && input != '\n') flush();
    selection = atoi(input); <--- error here!
  } 

  return selection;
}

void flush() {
    char c = getchar();
    while (c != EOF && c != '\n')
        c = getchar();
}

UPDATE After a lot of exprimenting, I found out that the issue was with the code I marked out. (the atoi()). I was passing it a simple char, rather than a char*. I still don’t get why when I used a bunch of printfs, it would die at the line I specified, and not before the call to atoi().

  • 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-09T15:04:57+00:00Added an answer on June 9, 2026 at 3:04 pm

    If you compile and run it with a debugger, you will find that the problem is actually in your atoi call.

    char input = ...;
    ...
    selection = atoi(input);
    

    atoi takes a char *, so you are telling it to convert the string at address 0x00000030 (for '0') to a number, and that is an invalid address.

    in gdb:

    Select the number of the corresponding item: 0
    
    Program received signal EXC_BAD_ACCESS, Could not access memory.
    Reason: KERN_INVALID_ADDRESS at address: 0x0000000000000030
    0x00007fff8ab00c53 in strtol_l ()
    (gdb) bt
    #0  0x00007fff8ab00c53 in strtol_l ()
    #1  0x0000000100000dc5 in prompt () at test.c:45
    #2  0x0000000100000cb9 in main () at test.c:21
    

    Compiling with warnings would also have told you this:

    $ gcc -Wall -std=gnu99 test.c
    test.c: In function ‘prompt’:
    test.c:48: warning: passing argument 1 of ‘atoi’ makes pointer from integer without a cast
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been slowly learning iPhone development and seem to keep hitting walls where
There are some bots that just keep hitting my site over and over again.
I have a problem that I really need solved in any way (changing data
I have an issue I keep hitting the wall on and I need some
Migrating a project from ASP.NET 1.1 to ASP.NET 2.0 and I keep hitting this
I keep on having ACCESS DENIED after hitting my download button. I already have
I keep hitting road blocks with Django and have read about Pylons. Pylons seemed
Been investigating for a while now and keep hitting a brick wall. I am
I've been trying to create a dynamically named JSON property but I keep hitting
I'm using Glade-3 for my GUI design, but I keep hitting this problem. I

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.