Thread: Can we change the value stored at the address stored in a character pointer?

  1. #1
    Registered User
    Join Date
    May 2015
    Location
    Lucknow, U.P., India
    Posts
    9

    Can we change the value stored at the address stored in a character pointer?

    I compiled the following code successfully but its not running :

    Code:
    void func2() {
        char c = 'b';
        char *p = "Hello";
        *p = c;
        printf("%c\n", *p);
    }
    I changed the value pointer p refers to, so shouldn't the address stored in it change automatically as well or the character stored at the address, i.e., 'H' be replaced by 'b'? Why is the code not running?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > Why is the code not running?
    Because on all modern machines, string constants like "Hello" are stored in read-only memory. You can't change them.

    You need something like
    char p[] = "Hello";
    to store the string (actually a copy of a string constant is made in the array p), and that is something you can change.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    Quote Originally Posted by amit_s95 View Post
    I compiled the following code successfully but its not running :

    Code:
    void func2() {
        char c = 'b';
        char []p = "Hello";
        *p = c;
        printf("%c\n", *p);
    }
    I changed the value pointer p refers to, so shouldn't the address stored in it change automatically as well or the character stored at the address, i.e., 'H' be replaced by 'b'? Why is the code not running?
    You cannot modify a string literal, if you want to modify the string, explicity use an array instead.

    Code:
    char hello[] = "Hello";

  4. #4
    Registered User
    Join Date
    May 2015
    Location
    Lucknow, U.P., India
    Posts
    9
    Thanks for replying

    Quote Originally Posted by Salem View Post
    >
    Because on all modern machines, string constants like "Hello" are stored in read-only memory. You can't change them.
    Wow, I didn't know that, thanks for sharing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Accessing values stored by a pointer into an array
    By Saad Rafey in forum C Programming
    Replies: 4
    Last Post: 03-08-2013, 06:25 AM
  2. Dereferencing pointer stored in boost::variant
    By h3ro in forum C++ Programming
    Replies: 1
    Last Post: 01-11-2011, 01:21 PM
  3. Addresses and extracting the value stored at an address
    By donoe_85 in forum C Programming
    Replies: 4
    Last Post: 04-08-2009, 03:38 AM
  4. Increment the address stored in a pointer???
    By patricio2626 in forum C++ Programming
    Replies: 9
    Last Post: 04-04-2007, 06:36 PM
  5. How is data stored? (or something like that)
    By Hunter2 in forum C++ Programming
    Replies: 13
    Last Post: 12-14-2002, 06:18 PM

Tags for this Thread