Thread: Changing value referred to by a character pointer

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

    Changing value referred to by a character pointer

    I am trying to change the value referred to by a character pointer to a character type variable after initializing it to a string literal.
    Code:
    #include <stdio.h>
    void func7() {
        char c = 'L';
        char *p = "APPLES";
        p = &c;
        printf("%s\n", p);
    }
    In the output, the character 'L', which should be the only character printed, is followed by some random characters, which I think are garbage values.
    I thought it was because the printf function kept printing characters until it encountered '\0' character, so I added this line
    Code:
    *(p + 1) = '\0';
    but its giving run time error, so I used string function
    Code:
    strcat(p, "\0");
    but even after this I am getting the same output.
    Is this because %s is causing some error, or my character pointer is not pointing to a string or something else?

  2. #2
    Tweaking master Aslaville's Avatar
    Join Date
    Sep 2012
    Location
    Rogueport
    Posts
    528
    You are making on fundametal mistake - trying to modify a string literal (in C).

    You cannot modify a string literal.

    When you write 'char a = "blahblah"' it get written to the executable so what you are trying to do is to modify the executable and that will of course give you undefined behaviour.

    if you want to modify the string, use:

    Code:
    char a[] = "string";

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by amit_s95 View Post
    In the output, the character 'L', which should be the only character printed,
    No... Since your p points to 1 char instead of nul-terminated char array - the behavior of any library function expecting C-string (nul-terminated char array) will be undefined.
    So there is no "should be" in this situation. Anything could happen.


    Quote Originally Posted by amit_s95 View Post
    I thought it was because the printf function kept printing characters until it encountered '\0' character, so I added this line
    Code:
    *(p + 1) = '\0';
    p+1 doe not points to any memory you own, so you live here on the borrowed time.

    Quote Originally Posted by amit_s95 View Post
    but its giving run time error,
    As expected

    Quote Originally Posted by amit_s95 View Post
    so I used string function
    Code:
    strcat(p, "\0");
    Once again - do not call library function that expects C-String and pass to it something else entirely

    Quote Originally Posted by amit_s95 View Post
    but even after this I am getting the same output.
    Well you got lucky.

    So if you want to handle 'L' as string - make it a string "L"
    If you want to append to it something - make sure you have enough memory reserved to put your additional data
    Code:
    char c[10] = "L";
    char* p = c;
    printf("1st: %s\n", p);
    strcat(p,"ove");
    printf("2nd: %s\n", p);
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    May 2015
    Location
    Lucknow, U.P., India
    Posts
    9
    Quote Originally Posted by Aslaville View Post
    You are making on fundametal mistake - trying to modify a string literal (in C).

    You cannot modify a string literal.

    When you write 'char a = "blahblah"' it get written to the executable so what you are trying to do is to modify the executable and that will of course give you undefined behaviour.

    if you want to modify the string, use:

    Code:
    char a[] = "string";
    but when i use
    Code:
    printf("%c\n", *p);
    in place of
    Code:
    printf("%s\n", p);
    then I get the expected output.
    So doesn't it mean that i was getting the error because printf() was expecting a string and not because
    Code:
    p = &c;
    is modifying the string?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You immediately assign to p, so the point about modifying the string literal is not applicable: the string literal is a red herring since you could well have left p uninitialised or initialised to a null pointer without making a real difference. See vart's post #3 instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 01-07-2013, 10:53 AM
  2. Changing character case
    By egomaster69 in forum C Programming
    Replies: 2
    Last Post: 11-27-2004, 05:17 PM
  3. A pointer to a character pointer array... can't pass
    By Lynux-Penguin in forum C Programming
    Replies: 9
    Last Post: 10-12-2003, 10:53 PM
  4. Changing one character in a char[]
    By codegirl in forum C++ Programming
    Replies: 4
    Last Post: 09-15-2003, 03:40 PM
  5. Changing the character output.
    By InFeStEd-ArCh0n in forum C++ Programming
    Replies: 7
    Last Post: 05-03-2002, 12:39 PM

Tags for this Thread