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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:05:25+00:00 2026-05-23T08:05:25+00:00

Possible Duplicate: Why does simple C code receive segmentation fault? I have code below

  • 0

Possible Duplicate:
Why does simple C code receive segmentation fault?

I have code below which will remove trailing spaces from a string but i don’t what
going in this code so as it gives segmentation fault problem??

 void main(void);

char* rtrim(char*);

void main(void)
 { 
 char* trail_str = "This string has trailing spaces in it.               ";


 printf("Before calling rtrim(), trail_str is '%s'\n", trail_str);

 printf("and has a length of %d.\n", strlen(trail_str));



 rtrim(trail_str);



 printf("After calling rtrim(), trail_str is '%s'\n", trail_str);

 printf("and has a length of %d.\n", strlen(trail_str));

 }


  char* rtrim(char* str)
  {
   int n = strlen(str) - 1;    

    while (n>0)            
   {
       if (*(str+n) != ' ')    
      {

           *(str+n+1) = '\0'; 



           break;             
      }

      else

      n--;
 }

 return str;      

 }
  • 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-23T08:05:26+00:00Added an answer on May 23, 2026 at 8:05 am

    trail_str points to a constant area in the memory and thus cannot be changed in *(str+n+1) = '\0'

    when initializing

    char* trail_str = "This string has trailing spaces in it.               ";
    

    you actualy generate a constant string: "This string has trailing spaces in it. " and tell trail_str to point to it.

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

Sidebar

Related Questions

Possible Duplicate: Why does simple C code receive segmentation fault? Hey Everyone, I'm sure
Possible Duplicates: Why does simple C code receive segmentation fault? Modifying C string constants?
Possible Duplicate: How does StackOverflow generate its SEO-friendly URLs? I made a simple form
Possible Duplicate: PHP: Script for generating Crossword game? Does anyone know any PHP-based simple
Possible Duplicate: How does the Google Did you mean? Algorithm work? Suppose you have
Possible Duplicate: Does SQL Server 2005 have an equivalent to MySql’s ENUM data type?
Possible Duplicate: Javascript === vs == : Does it matter which “equal” operator I
Possible Duplicate: Simple C scanf does not work? Why does scanf(%c, &letter); not working.
Possible Duplicate: Does an abstact classes have a VTABLE? Does a vtable gets created
Possible Duplicate: Does anyone know of a good C# API for Subversion? I'm designing

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.