constant string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jerico
    New Member
    • Sep 2006
    • 39

    constant string

    Hi.I have some confusion regarding a constant string and constant pointer to a string.

    char *str="hello";
    Here I think "hello" is constant so we cant modify it.Then what about
    const char *str="hello" and
    char *const str="hello"?

    Please explain it with examples.thanks .

    Jerico
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    have a look at
    http://duramecho.com/ComputerInforma tion/WhyHowCppConst. html

    Comment

    • jerico
      New Member
      • Sep 2006
      • 39

      #3
      I would like to learn it from you.In my previous post,you explained it very thoroughly.Plea se.

      Jerico

      Comment

      • horace1
        Recognized Expert Top Contributor
        • Nov 2006
        • 1510

        #4
        hope this helps!
        Code:
        // simple examples of const - invalid statements are commented out
        #include <stdio.h>
        
        int main()
        {
            char *s = "hello S";                     // "hello S" is a constant, s is pointed at it   
            char a[30] = "hello A";                  // define an array, initialise it with characters "hello A"
            printf("s = %s a = %s\n", s, a);
            //*(s+2) = 'X';                          // error! change third element of char* pointed to by s 
                                                     //   would give run time segmentation error
            a[2] = 'X';                              // change third element of array a 
            printf("s = %s a = %s\n", s, a);
            s=a;                                     // point s at array a
            *(s+2) = 'Z';                            // change third element of char* pointed to by , i.e. a[2]
            printf("s = %s a = %s\n", s, a);
            
            // define pointer s1 to a constant char*, we cannot change the char* s1 points at
            const char *s1 = "hello";                // pointer to a constant  char* 
            //*s1='X';                               // compile time error! - assignment of read-only location
            s1=a;                                    // but we can point s1 at array a
            //*s1='X';                               // compile time error! - although s1 points at a (which can be written)
                                                     //    compiler still gives assignment of read-only location
            
            // define pointer s2 a constant pointer to char*
            char *const s2 = a;                      // constant pointer to char *, initialised to point to a
            *s2='Q';                                 // change a[0]
            printf("s2 = %s a = %s\n", s2, a);
            //s2=a;                                  // compile time error! - assignment of read-only location
                                                     //  cannot change what s2 points at
        
            // define pointer s3 to a constant pointer to constant char*
            const char *const s3 = a;                // constant pointer to const char *, initialised to point to a
            printf("s3 = %s a = %s\n", s3, a);
        
        getchar();
        }
        try compiling and running it. then one by one uncomment some of the lines which should cause errors and see what you get.

        Comment

        Working...