Output and explanation req...

Discussion in 'C++' started by rahulmaximus, Aug 12, 2011.

  1. rahulmaximus

    rahulmaximus New Member

    Joined:
    May 31, 2011
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    The following 'C' code prints:
    void f(char *x) {
    x++;
    *x = ‘a’;
    }
    int main() {
    char *str = "hello";
    f(str);
    printf("%s", str);
    }
    a) hello
    b) allo
    c) hallo
    d) empty string
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    None of the answers is correct because
    Code:
    char *str="hello";
    
    defines the string "hello" in a part of memory that cannot be modified by the program. What the program SHOULD do is this:
    Code:
    char str[32];
    strcpy(str,"Hello");
    
    and then the output would be "Hallo".
     
  3. rahulmaximus

    rahulmaximus New Member

    Joined:
    May 31, 2011
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    thanks but i got what u meant....but could answer this one then....

    inside the func when i call printf 2nd time i get a total vague address value which does nt follow the character pointer arithmetic...it has no similarity to the first printf address.....
    Code:
    #include<iostream>
    #include<stdio.h>
    
    using namespace std;
    
    void func(char ** x)
    {    cout<<"\nThis is inside the small function :)\n\n";
         printf("The value whic is stored in x initially:%u\n",*x);
         *x++;
         printf("The value whic is stored in x afeterwards:%u\n",*x);
         
         **x='e';
    }
    
    int main()
    {
         char *a="Rahul";
         printf("The address stored in a is :%u\n",a);
         printf("\nThe address stored in the next element is :%u\n",a+1);
         func(&a);
         cout<<a;
         system("pause");
    }
     
    Last edited by a moderator: Aug 14, 2011
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    What is the exact output that you get when you run this program?
     
  5. rahulmaximus

    rahulmaximus New Member

    Joined:
    May 31, 2011
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    The address stored in a is :4199312

    The address stored in the next element is :4199313

    This is inside the small function :)

    The value whic is stored in x initially:4199312
    The value whic is stored in x afeterwards:2293680



    ****the prob******
    i was expecting the last output to be 4199313....as its a character pointer....so the address should have increased by +1
     
  6. rahulmaximus

    rahulmaximus New Member

    Joined:
    May 31, 2011
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    But as u can see i got 2293680...which i din understand at all

    but whereas in the main function u get 4199313...

    but why is there an address anomaly in the main function and inside the func function...though in both we point to the same 2nd element ...atleast i think so


    And i checked u were right we could not alter the content of a constant string thanks for tht :)...but it seems so vague
     
  7. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    *x is of what type? And what exactly happens when you ++ one of those?

    You're not doing exactly the same thing; a+1 is not the same as *x++. And that is the cause of the different behaviour.
     
  8. rahulmaximus

    rahulmaximus New Member

    Joined:
    May 31, 2011
    Messages:
    11
    Likes Received:
    0
    Trophy Points:
    0
    well **x is a pointer to a pointer...

    so *x should contain the address of the element....
    and yah i tried with a++ also same results...

    i tried the code in dev c++.................
     
  9. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    No, if **x is a pointer to a pointer, then *x is not what the pointer to the pointer double-points at; that would be **x. *x points to the POINTER and is of type POINTER, which is 4 bytes on a 32-bit system. So *x++ increases x by 4, not by 1, and does not point to the R. That's why you get a "garbage" value.
     
  10. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    No, hang on, I got that wrong. If x is of type char** then *x will be of type char*.

    Running through the code in the debugger, replacing the code to avoid undefined behaviour as follows:
    Code:
         // char *a="Rahul";
    	 char str[32];
    	 strcpy(str,"Rahul");
    	 char *a=str;
    
    Before *x++, x contains 5175140. After that statement it contains 5175132 (I'm using Visual Studio 2010 on 64-bit Windows). The output displayed is:
    The value whic is stored in x initially:5175140
    The value whic is stored in x afeterwards:3435973836

    and 3435973836=0xcccccccc, which is the value the Visual Studio debugger loads into certain places to help you spot broken pointer manipulation. Note however that 0xcccccccc is not the value in x but the value pointed at by x.

    Did you know that *x++ is equivalent to *(x++)? Replacing *x++ with *(x++) gets the same behaviour. However replacing it with (*x)++ gets what I suspect to be the behaviour you want:

    Code:
    void func(char ** x)
    {    cout<<"\nThis is inside the small function :)\n\n";
         printf("The value whic is stored in x initially:%u\n",*x);
         (*x)++;
         printf("The value whic is stored in x afeterwards:%u\n",*x);
         
         **x='e';
    }
    
    int main()
    {
         // char *a="Rahul";
    	 char str[32];
    	 strcpy(str,"Rahul");
    	 char *a=str;
         printf("The address stored in a is :%u\n",a);
         printf("\nThe address stored in the next element is :%u\n",a+1);
         func(&a);
    	 printf("str after func is '%s'\n",str);
         //cout<<a;
         //system("pause");
    }
    
    Output:
    Code:
    The address stored in a is :2948836
    
    The address stored in the next element is :2948837
    
    This is inside the small function :)
    
    The value whic is stored in x initially:2948836
    The value whic is stored in x afeterwards:2948837
    str after func is 'Rehul'
    
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice