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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T08:08:21+00:00 2026-06-15T08:08:21+00:00

Please some one tell me why is this function call resulting in a segmentation

  • 0

Please some one tell me why is this function call resulting in a segmentation fault

Look at the second section of code the first section is free from errors i already debugged it

Scroll down to second code snippet

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include"line.h"
main(int argc,char **argv,...)
{
  FILE *fp;
  char ch,**str=0;
  int length=0,maxlen=0,line=0;

  if(argc==2)
  fp=fopen(argv[1],"r");

  else
  {
   printf("Expexted Input Was:\"./linesort\" \"filename\"\n");
   return;
  }

  if(fp==NULL)
  {
   printf("Source file not found ,Please Check If It Really Exists in Expected path\n");
   return;
  }

   printf("%ld:",ftell(fp));

  while((ch=fgetc(fp))!=EOF)
  {
     length=0;
     while(ch!='\n')
     {
     printf("%c",ch);
     ch=fgetc(fp);
     length++;
     }

    printf("len:%d\n",length);
    if(maxlen<length)
    maxlen=length;
    line++;
  }

printf("maxlen:%d\n",maxlen);
printf("No.of lines:%d\n",line);
fseek(fp,0,0);
str=(char **)malloc(line*sizeof(char *));
printf("__%ld____\n",str);

for(length=0;length<line;length++)
{
 printf("%d\t",length);
 str[length]=malloc((maxlen+1)*sizeof(char));
 printf("__%ld____\n",str[length]);
 fgets(str[length],maxlen+1,fp);
 puts(str[length]);
}

fclose(fp);

while(1)
{
   printf(" \n'a':for alphabet wise \t'c':for character wise\t 'e':to exit:\t");
   ch=getchar();
   getchar();

///////This section causing segfault and the following code is continuation to previus code

Look at alpha(str,maxlen,line);

   switch(ch)
   {
      case 'a':  alpha(str,maxlen,line);///////////Causes Segmentation Fault?Why?
                 break;
    //case 'c':chara(str);
    //    break;
    case 'e':exit(0);
    default :;
   }
   }
 }

////alpha.c(The called function Is in this file)dont mind if this section has a logical error

   #include"line.h"
   void alpha(char **p,int maxlen,int line)
    {
     int i=0;
     char *buffer;
     printf("in alpha");
     buffer = (char *)malloc((maxlen+1)*sizeof(char));
     while(i<line)
     {
      if(strcmp(p[i],p[i+1])==+1)
      {
        strcpy(buffer,p[i+1]);
         strcpy(p[i+1],p[i]);
         strcpy(p[i],buffer);
        }
      i++;
     }
      printf("%s\t",p[3]);
     }

///////My header file(line.h) contains these declarations

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void alpha(char **p,int maxlen,int line);
void chara(char **p,int maxlen,int line);
  • 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-15T08:08:22+00:00Added an answer on June 15, 2026 at 8:08 am

    In Function alpha():

    while(i < (line - 1))
    {
        if(strcmp(p[i],p[i+1])==+1)
        {
            strcpy(buffer,p[i+1]);
            strcpy(p[i+1],p[i]);
            strcpy(p[i],buffer);
        }
        i++;
    }
    

    Last Line is not matched with any Next Line…

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

Sidebar

Related Questions

can some one please tell me how can i make the send function read
Can someone please tell me whats wrong with this code. I'm not getting complete
Please can some one briefly tell me with example what does the means of
Can some one please tell me why exactly classes were introduced in C++. Are
Some one please tell me how to handle RunTimeExceptions in grails version1.1 .I have
can some one please tell me where is the mistake from my side, to
Can some one please tell me why I get odd results rurning the following
Can someone please tell me where is the error in this code?, I use
Can someone please tell me what this means: 07-04 09:54:38.048: I/DetailActivity(15496): Title that is
Could someone please tell me why this <%= destroy_password_url @user.password_reset_token %> generates http://localhost:3000/api/destroy_password.4G5EoRVYMUAtiIKqOerKsw routes.rb

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.