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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:44:09+00:00 2026-06-15T04:44:09+00:00

Possible Duplicate: Why do I get a segmentation fault when writing to a string?

  • 0

Possible Duplicate:
Why do I get a segmentation fault when writing to a string?
What is the difference between char a[] = “string”; and char *p = “string”;

Can anyone point out the problem in following program:

#include <stdio.h>

int main()
{
    char *c = "Hello World!! !!";
    char c2 = 'X';
    while(c)
    {
        if(*c == ' ')
        {
            printf("%s",c);
            *c = c2;
        }
        c++;
    }
    return 0;
}

It crashes at *c = c2; with following error :

Thread [1] (Suspended : Signal : EXC_BAD_ACCESS:Could not access memory)    
    main() at mainclass.cpp:64 0x100000d74  

I used GCC as compiler on MAC OSX and Eclipse as IDE.

  • 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-15T04:44:11+00:00Added an answer on June 15, 2026 at 4:44 am

    In your code, c points to a string literal. The line *c = c2 tries to modify the string literal, which is undefined behaviour. One possible manifestation of the undefined behaviour is that string literals are put into read-only memory and you get a SIGSEGV or similar.

    If you change the code as follows, it should work:

    char arr[] = "Hello World!! !!";
    char* c = arr;
    

    Now c points into an array. Unlike string literals, it is permissible to modify the contents of an array.

    Lastly, your while-loop is incorrectly conditioned on the value a pointer, not the value it points to. You’re looking for a null-terminator (‘\0’). It should look like this:

    while (*c)
    {
        ...
        c++;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why do I get a segmentation fault when writing to a string?
Possible Duplicate: Why do I get a segmentation fault when writing to a string?
Possible Duplicate: Why do I get a segmentation fault when writing to a string?
Possible Duplicate: Why do I get a segmentation fault when writing to a string?
Possible Duplicate: Why do I get a segmentation fault when writing to a string?
Possible Duplicate: Why do I get a segmentation fault when writing to a string?
Possible Duplicate: Why is this C code causing a segmentation fault? char* string =
Possible Duplicate: Segmentation Fault - C #include<stdio.h> #include<string.h> int main() { char *p; printf(enter
Possible Duplicate: Difference between char *str=“STRING” and char str[] = “STRING”? I wrote the
Possible Duplicate: Why don’t I get a segmentation fault when I write beyond the

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.