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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:22:40+00:00 2026-05-14T21:22:40+00:00

Upon compiling and running this small program to reverse a string, I get a

  • 0

Upon compiling and running this small program to reverse a string, I get a Segmentation Fault before any output occurs. Forgive me if this is an obvious question, I’m still very new to C.

#include <stdio.h>

int reverse(char string[], int length); 

int main() {
char string[] = "reversed";

  printf("String at start of main = %s", string);
  reverse(string, sizeof(string));
  printf("%s\n", string);

return 0;

}

// Reverse string 
int reverse(char string[], int length) {
 int i;
 char reversed[] = {};
 int temp;

 for(i = 0; i < length; ++i) {
 temp = string[i];
 reversed[length - i] = temp;

 }
 return 0; 
}
  • 1 1 Answer
  • 1 View
  • 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-14T21:22:41+00:00Added an answer on May 14, 2026 at 9:22 pm

    Because of this:

    First you create an array with zero elements:

    char reversed[] = {};
    

    And later you attempt to write to that array beyond its bounds:

    reversed[length - i] = temp;
    

    Update:

    The above means that you need to allocate memory whose size is only known at runtime (it’s length). The usual C-style way of doing this is… by pushing the burden of memory allocation to your caller:

    int reverse(const char* string, char* destination, int length);
    

    This function would write to a buffer provided by the caller, who now also must ensure that:

    1. The buffer is large enough
    2. The memory for buffer gets freed when it should be
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

made a short java program in Bluej upon compiling it and trying to get
Hey Everyone, How do I fix the Compiler Error upon compiling on return ((string)(this[TargetDir]));
Hi upon compiling this code in Visual studio 2008 i get the following error
I stumbled upon this code: static void Main() { typeof(string).GetField(Empty).SetValue(null, evil);//from DailyWTF Console.WriteLine(String.Empty);//check //how
Upon compiling, I am getting the following error: C:\UDK\UDK-2010-03\Development\Src\FixIt\Classes\ZInteraction.uc(41) : Error, Unrecognized member 'OpenMenu'
I'm having an interesting error upon moving over to g++ compiling on RedHat after
I recently stumbled upon this in a project I'm working on. In package A
Today I stumpled upon the shadowCopyBinAssemblies option in the hostingEnvironment tag. Appearently this attribute
I am trying to install libsmtp on Ubuntu 12.04, and upon compiling i am
I encountered my first Segmentation Fault today (newbie programmer). After reading up on what

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.