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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:13:26+00:00 2026-06-14T13:13:26+00:00

I was going through some documentation which states that First Case char * p_var=Sack;

  • 0

I was going through some documentation which states that

First Case

char * p_var="Sack";     

will create a constant string literal.

And hence code like

  p_var[1]="u";             

will fail because of that property.


Second Case

Also mentioned is that this is possible only for character literals and not for other data types through pointers. So code like

float *p="3.14"; 

will fail, resulting in a compiler error.

But when i try it out i don’t get compiler errors ,accessing it though gives me 0.000000f(using gcc on Ubuntu).

So regarding the above, i have three queries:

  1. Why are string literals created in First Case read-only?

  2. Why are only string literals allowed to be created and not other constants like float through pointers?

3. Why is Second Case not giving me compiler errors?

Update

Please discard the 3rd question and second case. I tested it by adding quotes.

Thanks

  • 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-14T13:13:27+00:00Added an answer on June 14, 2026 at 1:13 pm

    The premise is wrong: pointers don’t create any string literals, neither read-only nor writeable.

    What does create a read-only string literal is the literal itself: "foo" is a read-only string literal. And if you assign it to a pointer, then that pointer points to a read-only memory location.

    With that, let’s turn to your questions:

    Why are string literals created in First Case read-only?

    The real question is: why not? In most cases, you won’t want to change the value of a string literal later on so the default assumption makes sense. Furthermore, you can create writeable strings in C via other means.

    Why are only string literals allowed to be created and not other constants like float?

    Again, wrong assumption. You can create other constants:

    float f = 1.23f;
    

    Here, the 1.23f literal is read-only. You can also assign it to a constant variable:

    const float f = 1.23f; 
    

    Why is Second Case not giving me compiler errors?

    Because the compiler cannot check in general whether your pointer points to read-only memory or to writeable memory. Consider this:

    char* p = "Hello";
    char str[] = "world"; // `str` is a writeable string!
    
    p = &str[0];
    
    p[1] = 'x';
    

    Here, p[1] = 'x' is entirely legal – if we hadn’t re-assigned p beforehand, it would have been illegal. Checking this cannot be generally done at compile-time.

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

Sidebar

Related Questions

Going through some documentation on modifying CGImageRef data, I came across a strange example
I was going through some code that I downloaded off the internet ( Got
I've been going through some good (seeming) resources for Rails tutorials, and will dutifully
I'm going through some old C#.NET code in an ASP.NET application making sure that
I was going through some tutorial and documentation about zend framework, most of things
I am going through some documentation about adding the application image for the app,
I'm going through the Intel processor documentation and writing some basic assembly code at
Going through some of my older Delphi projects and upgrading them to D2009, as
While going through some tutorials, I have encountered lines such as this: ((IDisposable)foo).Dispose(); Ignore
I am going through some code and at the beginning of the script we

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.