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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:50:06+00:00 2026-06-09T22:50:06+00:00

There is something that has been bugging me for a while and I need

  • 0

There is something that has been bugging me for a while and I need an anwer for it,

char *p = "hello world";
p="wazzup";
p="Hey";

Here I declare an pointer to point to a string (or in other words I made a string by using a pointer)

I have had some strange results with this that i normally wouldnt have gotten if I used an char array string

cout <<p<< endl; //"Hey" Gets printer
cout <<p+8<< endl; // I kept adding numbers till "wazzup" got printed
cout <<p+29<< endl; // No matter how much I increment, I cant print "Hello World"

So my question is:

When I change the value that a char pointer is pointing to. Does it

  • overwrite the original Data like it would do with char array;

  • or it creates a new string right before it in the memory and points to it;

  • or does it add the new string at the begining of the old one(including null);

  • or does it create a new string in a new place in the memory and I was able to print “wazzup” only by chance

  • 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-09T22:50:08+00:00Added an answer on June 9, 2026 at 10:50 pm

    It does none of the options above. Changing the value of a pointer merely changes to the address in memory it points to. In each case of the assignments to p, it is set to point to the first character of a (different) string literal – which is stored in memory.

    The behaviour of using a pointer that points beyond the end a string literal such as

    cout <<p+8<< endl

    is undefined. This is why using pointers is fraught with danger.

    The behaviour you are seeing is implementation dependant: The compiler stores the strings literals adjacent in memory, so running off the end of one runs into another. Your program might equally have crashed when compiled with a different compiler.

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

Sidebar

Related Questions

I've been using JSF for a while but there's something that has always confused
This is something that has been bugging me for a while as it is
This is something that has been bugging me for a while.. I'm building of
So this has been something that has been bugging me for a while. How
This is something that has been pulling at me for a while. Consider a
This is a question that has been bugging me for a while and I
this is not a real example, it is something that has been bugging me
Something that has been bugging me since I read an answer on another stackoverflow
Hey all. Something which has been bugging me quite a bit recently is this
There is something that has been on my mind for a long time now.

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.