what will the output?

Discussion in 'C' started by indian.21987, Aug 21, 2007.

  1. indian.21987

    indian.21987 New Member

    Joined:
    Aug 19, 2007
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Code:
    main() 
    { 
            char *p1=“name”; 
            char *p2; 
            p2=(char*)malloc(20); 
            memset (p2, 0, 20); 
            while(*p2++ = *p1++); 
            printf(“%s\n”,p2); 
    
    } 
    
    Also what is the use of malloc() function?
    EDIT: Please read the "Before you make a query" thread (see the upper right corner of this page).
     
    Last edited by a moderator: Aug 21, 2007
  2. seeguna

    seeguna New Member

    Joined:
    Jun 20, 2007
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Technical Consultant
    Location:
    Chennai
    What u exactly want in this program?


    Malloc() is used for dynamic memory allocation.........
    Memset() is used to Set buffers for the character........
     
  3. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Malloc will allocate 20 char of memory from the heap. Memset will set it to zeroes. "Name" will be copied there. Please note that you cannot use the type of quotes that you show. Use "".
     
  4. indian.21987

    indian.21987 New Member

    Joined:
    Aug 19, 2007
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    thanks a lot. Got the idea about malloc,memset fns.
    my question is,,

    what will be the output for the above program????
     
  5. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    The output will be Name, presuming you correct your quote characters. The line,
    Code:
    while(*p2++ = *p1++);
    
    will check to see if the assignment of *p1 to *p2 is non-zero. On the first operation if will be non-zero (it will be 'N'). It will then increment p1 and p2 to the next location and do it again. This will continue until it assigns the terminating 0, then it will exit. Thus, the contents pointed to by p1 are copied to the area pointed to by p2.

    This is the sort of thing you should compile and test. You would have learned two things: not to use balanced quote characters, and what the program would output.
     
  6. listendinesh

    listendinesh New Member

    Joined:
    Aug 3, 2007
    Messages:
    18
    Likes Received:
    0
    Trophy Points:
    0
    I have some doubts about output.malloc will allocate a chunk of 20 bytes to p2 and we set that buffer with 0. In the while loop we are incrementing both p1 and p2 for copying string. After while loop complete "name" whould be copied into buffer returned by malloc but p2 will point sixth byte of buffer which contain 0.So when printf statement will execute to print string pointed by p2 it would print nothing.
     
  7. seeguna

    seeguna New Member

    Joined:
    Jun 20, 2007
    Messages:
    31
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Technical Consultant
    Location:
    Chennai
    What Dinesh said is right.........
    This program doesn't print anything....
     
  8. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Actually, you are correct, my oversight. p2 will point to the byte AFTER the terminating 0, which is also a zero because of the memset. Thus, it will be a null string. Without the memset, it might print a bunch of junk.
     
  9. indian.21987

    indian.21987 New Member

    Joined:
    Aug 19, 2007
    Messages:
    8
    Likes Received:
    0
    Trophy Points:
    0
    Ahhh! Clearly unterstood.
    thank U all.........
     

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