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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T05:31:57+00:00 2026-05-31T05:31:57+00:00

I am getting segmentation fault for the code below. To compile I typed gcc

  • 0

I am getting segmentation fault for the code below. To compile I typed gcc -std=c99 -g alphacode.c. This is a problem I am solving from here, and I am not sure what the problem is.

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

int catchar(char a, char b) {

  char a1[2];
  a1[1] = a;
  a1[0] = b;

  return atoi(a1);
}

int processNumber(char *num) {
 int size = strlen(num);
 int p[size];

 if (num[size-1] != 0)
   p[size-1] = 1;
 else
   p[size-1] = 0;

 int i;
 for (i = size-2; i>=0; i--)
    {

      if (catchar(num[i], num[i-1]) > 26 ||
      catchar(num[i] , num[i-1]) <1 || num[i] == 0) 
    p[i] = p[i-1];    
      else
    p[i] = p[i-1] + p[i-2];

    }

 return p[0];

}


int main() {

    int bytes_read;
    int nbytes = 5000;
    char *number;
    bytes_read = getline (&number, &nbytes, stdin);
    while (bytes_read != -1) {

      int out = processNumber(number);
      printf("%d\n", out);
      bytes_read = getline (&number, &nbytes, stdin);

    }
    return 0;
}
  • 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-05-31T05:31:57+00:00Added an answer on May 31, 2026 at 5:31 am
    int catchar(char a, char b) {
    
      char a1[2];
      a1[1] = a;
      a1[0] = b;
    
      return atoi(a1);
    }
    

    atoi() expects a string, and a string must have a '\0' terminator, without it – atoi() will just keep looking until it find '\0', and you might get garbage or segfault – if you are out of your allocated memory.

    You should declare your array of size 3, and put '\0' at index 2.

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

Sidebar

Related Questions

I am getting segmentation fault in this code but i cant figure out why.
I'm getting a segmentation fault trying to compile the following code. My question is,
I am getting a Segmentation Fault error when running my program in GCC. This
I am getting a segmentation fault error when I run this code. I don't
I'm getting a segmentation fault from SDL_BlitSurface in C++. here's the code mainScreen->draw( 0,
I am getting a segmentation fault while running this code. I can't work out
I am getting a segmentation fault with the following code after adding structs to
Possible Duplicate: getting segmentation fault in a small c program Here's my code: char
This is an extended question from How can i remove this Segmentation fault in
Given the code below, I get a segmentation fault if I run it with

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.