pointer array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • 333471
    New Member
    • Jun 2014
    • 4

    pointer array

    Code:
    char *s="george";
    printf(" from s %s\n",s);
    
    s[0]='k';
    
    printf(" from new s %s\n",s);
    
    char p[]="john";
    printf("from p %s\n",p);
    
    p[2]='k';
    
    printf(" from new p %s\n",p);

    plss explain me how dis actually works inside the memory?
    1)s is just a pointer holding the adress of 'george' ryt?so if i do s[2]='k' y does it NOT change?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    s has the address of s[0]. The first g of george.

    s[2] is a calculation of s + 2 x sizeof(char). The s is not changed but the calculation results in an address of the o of george.


    There are two syntaxes for dealing with arrays. s[2] is the "array notation" where 2 refers to the 3rd element of the array (from the starting character go 2 characters to the right). The other notation is the "pointer notation" where you do a calculation s + 2 x sizeof(char) to get to the 3rd element.

    Comment

    • androidapp
      New Member
      • Jun 2014
      • 10

      #3
      I guess, you will not be able to change constant string via pointer.

      Comment

      • donbock
        Recognized Expert Top Contributor
        • Mar 2008
        • 2427

        #4
        Line 4 attempts to change "george" to "keorge" by changing s[0] to 'k'.
        Line 11 attempts to change "john" to "joke" by changing t[2] to 'k'.

        However, both s and t point at string literals. The C Standard says
        6.4.5 String literals
        ...
        A character string literal is a sequence of zero or more multibyte characters enclosed in double-quotes, as in "xyz".
        ...
        In translation phase 7, a byte or code of value zero is appended to each multibyte character sequence that results from a string literal or literals. The multibyte character sequence is then used to initialize an array of static storage duration and length just sufficient to contain the sequence.
        ...
        It is unspecified whether these arrays are distinct provided their elements have the appropriate values. If the program attempts to modify such an array, the behavior is undefined.
        That last paragraph warns that lines 4 and 11 provoke undefined behavior. Avoid undefined behavior.

        The following is an example of how to modify your code to avoid undefined behavior. This code only tries to modify char array s. Notice how I use const to make the string literals explicitly read-only.
        Code:
        const char *a = "george";
        const char b[] = "john";
        char s[99+1];
        
        strcpy(s,a);
        printf("a: %s\n, s);
        s[0] = 'k';
        printf("a: %s\n, s);
        
        printf("\n");
        
        strcpy(s,b);
        printf("b: %s\n, s);
        s[2] = 'k';
        printf("b: %s\n, s);

        Comment

        • donbock
          Recognized Expert Top Contributor
          • Mar 2008
          • 2427

          #5
          By the way, "It is unspecified whether these arrays are distinct provided their elements have the appropriate values" means that the compiler is allowed to reduce the number of duplicate string literals in a program.

          For example, in the following snippet it is permissible (but not required) for the pointer values of s1, s2 and s3 to be identical (that is, for all of them to point to the same string literal).
          Code:
          const char *s1 = "string";
          const char *s2 = "string";
          const char *s3 = "string";
          
          printf("s1 = %p\n", s1);
          printf("s2 = %p\n", s2);
          printf("s3 = %p\n", s3);

          Comment

          Working...