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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:37:38+00:00 2026-05-27T04:37:38+00:00

Attempting to modify a string literal causes undefined behavior: char * p = wikipedia;

  • 0

Attempting to modify a string literal causes undefined behavior:

char * p = "wikipedia"; 
p[0] = 'W'; // undefined behaviour

One way to prevent this is defining it as an array instead of a pointer:

char p[] = "wikipedia"; 
p[0] = 'W'; // ok

Why does char* cause undefined behaviour, while char[] doesn’t?

  • 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-27T04:37:39+00:00Added an answer on May 27, 2026 at 4:37 am

    Any attempt to modify a C string literal has undefined behaviour. A compiler may arrange for string literals to be stored in read-only memory (protected by the OS, not literally ROM unless you’re on an embedded system). But the language doesn’t require this; it’s up to you as a programmer to get it right.

    A sufficiently clever compiler could have warned you that you should have declared the pointer as:

    const char * p = "wikimedia";
    

    though the declaration without the const is legal in C (for the sake of not breaking old code). But with or without a compiler warning, the const is a very good idea.

    (In C++, the rules are different; C++ string literals, unlike C string literals, really are const.)

    When you initialize an array with a literal, the literal itself still exists in a potentially read-only region of your program image, but it is copied into the local array:

    char s[] = "wikimedia"; /* initializes the array with the bytes from the string */
    char t[] = { 'w', 'i', ... 'a', 0 };  /* same thing */
    

    Note that char u[] = *p does not work — arrays can only be initialized from a brace initializer, and char arrays additionally from a string literal.

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

Sidebar

Related Questions

I'm attempting the modify this Modx Snippet so that it will accept multiple values
To prevent a duplicate shopping cart being added I am attempting to modify my
I am essentially attempting to modify this stored procedure. Modified stored procedure: CREATE PROCEDURE
Attempting to use the data series from this example no longer passes the JSONLint
I'm attempting to modify an inherited project that has a convoluted process of displaying
I'm attempting to modify a MIPS simulator to display the contents of its registers
I've been attempting this for two days, and constantly running into dead ends. I've
This is basically a menu that doesn't refresh the page. I am attempting to
I've been attempting to modify the contents of a custom tree view that inherits
I'm attempting to modify a date field from my database before it gets outputted

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.