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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T02:08:35+00:00 2026-05-20T02:08:35+00:00

I got this weird behavior of my programm, that i cant figure out. My

  • 0

I got this weird behavior of my programm, that i cant figure out. My professor showed me a flaw in my programm, where i just copy a char pointer when i construct an object instead of making a new copy of the whole array, so you can fool around with it. He demonstrated this with similar code like this.

For the code:

char sweat[] ="Sweater";
warenkorb = new WareImKorb(new Textil (205366,4.2,sweat,40),2,warenkorb);
sweat[0] = '\0';

now if i instead make it:

char* sweat ="Sweater";

the program runs fine till i try sweat[0] = ‘\0’;
It simply crahes then.

However this works:
char cc[] =”Sweater”;
char* sweat = cc;

It is really bugging me, that i dont understand, why version 1 does not work.
Hope you guys can help me out, or else i will go crazy wondering about this.

  • 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-20T02:08:35+00:00Added an answer on May 20, 2026 at 2:08 am
    char* sweat ="Sweater";
    sweat[0] = '\0'
    

    Here sweat points to a CONSTANT data. “Sweater” is const literal data, residing somewhere in read-only memory, and sweat points to this data as such. It doesn’t make a copy of it. So when you do sweat[0]='\0', it tries to change first character of the CONSTANT data. Hence the error. By the way, a good compiler should give warning if you don’t write const in your declaration, as const char* sweater = "Sweater". See the warning here : http://www.ideone.com/P47vv

    But when you write char sweat[] = "Sweater", an array of char is created, copying the data from the CONSTANT data which is "Sweater"; that array’s element itself is modifiable!


    Lets see an interesting thing: since in the first case, it doesn’t make a copy of the const data, so no matter how many variables you declare (all pointing to the same data), the address would be same for all variables. See this:

    #include<cstdio>
    int main() {
            char* sweat  ="Sweater";        //notice the warning
            const char* another ="Sweater"; //no warning!
            std::printf("%p\n", sweat);     //print the address
            std::printf("%p\n", another);   //print the address
            return 0;
    }
    

    Output:

    0x8048610
    0x8048610
    

    Means, both printfs print the same address!

    See yourself here : http://www.ideone.com/VcyM6

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

Sidebar

Related Questions

Alright, I have some weird behavior and this question goes to the people out
I just had some weird behavior from a version of g++ for Windows that
I got this weird problem where System.Action cant be resolved when its not explicitly
I have a Rails 3.1 app where I just got some weird behavior. I
I got this weird error when I wanted to validate my in-memory xml Schema,
I've got a weird problem where if i do something like this in a
I've got some weird behavior and I can only assume is because of the
I'm trying to profile my renderer, and I'm seeing some weird profiling behavior that
I've got this weird problem with FileMaker Pro 12. I am using it for
I got this weird crash in my app, when i try to call this

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.