Thread: Change one element of string pointed by char pointer

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by Ducky View Post
    Code:
    char * p = "Hello World";
    actually, on g++, this code will give compiler warnings about this conversion being deprecated.

    Code:
    char p[] = "Hello World";
    is fine though, as is

    Code:
    const char * p = "Hello World";

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by std10093 View Post
    String literals can not modify their data, but they can modify their pointer.

    On the other hand, if you had an array, then the opposite stands true. You can not modify the pointer, but you can modify the data
    I'd prefer to say that a pointer can change its string literal. I wouldn't really say that, it sounds awkward, but the point is that string literal should be pointed to, it doesn't own it's own pointer and the data itself doesn't really decay into a pointer. A string literal is actually "This" or "this" -- objects of type char[5] -- the data proper.

    char *p = "This";

    p can be reassigned, because it is a pointer. And that very syntax is only allowed as a convenience. A convenience which C++ copied from the beginning, I think, but nonetheless. Before C was standardized, you needed to do magic like this:

    char * p;
    char cs[] = "This";
    p = cs;

    I will even link a FAQ answer later that shows the difference between a pointer pointing to an array object and an array itself with pictures.

    Quote Originally Posted by std10093 View Post
    Example with string literal.
    <snip>

    While with the array
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char str[15] = "a string";
        
        printf("I can modify the data...\n");
        *(str+1) = 'W';
        
        printf("But not the pointer...!\n");
        str++;
        
        printf("%s\n", str);
        
        return 0;
    }
    On linux I got output:
    Code:
    linux05:/home/users/std10093>gcc -Wall px.c -o px
    px.c: In function 'main':
    px.c:11: error: lvalue required as increment operand
    and on windows
    Code:
    main.c: In function `main':
    main.c:11: error: wrong type argument to increment
    make[2]: *** [build/Debug/Cygwin-Windows/main.o] Error 1
    make[1]: *** [.build-conf] Error 2
    make: *** [.build-impl] Error 2
    Hope that helps!
    This code that you posted is actually guaranteed not to work.
    Arrays cannot be reassigned, because that's illegal. If arrays were pointers, then it would be legal. str++; is:

    str = str + 1;

    which means that this requires array assignment. With a pointer, this operation is fine, though it's not guaranteed to point to a dereferenceable location. In other words, you can do this:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void) {
       char foo[] = "My sample string.";
    
       for (char *bar = foo; *bar != '\0'; bar++) {
          *bar = toupper(*bar);
       }
       printf("%s\n", foo);
       
       return 0;
    }
    But you cannot increment foo here, because foo is an array type. Try, and you will get that error. foo is the wrong type to increment.

    Try reading this FAQ answer.
    Last edited by whiteflags; 02-15-2013 at 01:01 AM.

  3. #3
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Quote Originally Posted by whiteflags View Post
    I'd prefer to say that a pointer can change its string literal. I wouldn't really say that, it sounds awkward, but the point is that string literal should be pointed to, it doesn't own it's own pointer and the data itself doesn't really decay into a pointer. A string literal is actually "This" or "this" -- objects of type char[5] -- the data proper.
    The more you know something, the easier you can explain. I believe that you have a good knowledge, so rephrase what you said.
    Quote Originally Posted by whiteflags View Post
    p can be reassigned, because it is a pointer.
    Ι believe I said that too.
    Quote Originally Posted by whiteflags View Post
    I will even link a FAQ answer later that shows the difference between a pointer pointing to an array object and an array itself with pictures.
    That is what I am trying to say... but thanks for the FAQ, since I didn't know about this page.
    Quote Originally Posted by whiteflags View Post
    This code that you posted is actually guaranteed not to work.
    Arrays cannot be reassigned, because that's illegal. If arrays were pointers, then it would be legal. str++; is:

    str = str + 1;

    which means that this requires array assignment. With a pointer, this operation is fine, though it's not guaranteed to point to a dereferenceable location. In other words, you can do this:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    
    int main(void) {
       char foo[] = "My sample string.";
    
       for (char *bar = foo; *bar != '\0'; bar++) {
          *bar = toupper(*bar);
       }
       printf("%s\n", foo);
       
       return 0;
    }
    But you cannot increment foo here, because foo is an array type. Try, and you will get that error. foo is the wrong type to increment.
    Exactly! The code I posted was for demonstration of which you can do and what you can't! I thought this was clear by the printfs

    However, I like your example, so I am going to augment my upload on my page with your example and the faq link. Of course you will get the credits

    EDIT: Here it is.
    Last edited by std10093; 02-15-2013 at 06:52 AM.
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by std10093 View Post
    The more you know something, the easier you can explain. I believe that you have a good knowledge, so rephrase what you said.
    I hope you are doing this because you didn't understand me somehow.

    String literals have a type. It's array of char. And while you said "String literals can not modify their data, but they can modify their pointer," my main problem with this is that they don't have a pointer like you implied. The pointer and the string literal are actually separate things.

    Ι believe I said that too.

    That is what I am trying to say... but thanks for the FAQ, since I didn't know about this page.

    Exactly! The code I posted was for demonstration of which you can do and what you can't! I thought this was clear by the printfs

    However, I like your example, so I am going to augment my upload on my page with your example and the faq link. Of course you will get the credits
    Please. I give 0 ........s about credit here.

    If you were trying to say what I said, I felt I needed to rephrase your post anyway. You kept referring to arrays as pointers. Arrays are not pointers. And I thought it was important enough to say that arrays in general can't be reassigned, and it's not just a string literal issue.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 01-15-2012, 06:09 AM
  2. Adreess of pointed structure element
    By limp in forum C Programming
    Replies: 1
    Last Post: 06-12-2009, 05:54 AM
  3. How change value pointed to by shared_ptr?
    By 6tr6tr in forum C++ Programming
    Replies: 1
    Last Post: 04-28-2008, 03:45 PM
  4. Problem with passing back pointer pointed to string
    By whichet in forum C Programming
    Replies: 9
    Last Post: 11-21-2007, 07:55 AM
  5. Replies: 5
    Last Post: 10-29-2001, 10:16 AM