Thread: Puts() does not work for a string (pointer)

  1. #1
    Registered User
    Join Date
    Dec 2014
    Location
    Romania, city Arad
    Posts
    1

    Puts() does not work for a string (pointer)

    puts(pd) does not give any output in the below code. Also, the first puts(ps) does give an output, but the second puts(ps) after "while" does not give any output. Could please somebody explain? Thanks.

    Code:
    #include <stdio.h>
    
    
    int main()
    {
        char source[50]="Alice has apples.";
        char destination[60];
        char *pd, *ps;
        ps=source;
        puts(source);
        puts(ps);
        pd=destination;
        while (*ps!='\0')
            *pd++=*ps++;
        *pd='\0';
        puts(ps); //it does not work!!! Why?
        puts(source);
        puts(pd); //it does not work!!! Why?
        puts(destination);
        return 0;
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    What are your pointers pointing to after the "while()" loop is completed?

    Trace it out on paper if you need help visualizing the answer.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to Use Puts and Gets in C++
    By rajatahirqaiser in forum C++ Programming
    Replies: 1
    Last Post: 06-03-2013, 10:18 AM
  2. puts() function doesn't return string
    By kappilrinesh in forum C Programming
    Replies: 6
    Last Post: 02-07-2012, 12:56 PM
  3. gets and puts does'nt work!!
    By behzad_shabani in forum C Programming
    Replies: 46
    Last Post: 06-16-2008, 10:18 AM
  4. puts( );
    By xddxogm3 in forum C Programming
    Replies: 9
    Last Post: 03-28-2005, 12:42 PM
  5. Getting pointer to work with function
    By Vertex34 in forum C Programming
    Replies: 7
    Last Post: 09-24-2004, 01:18 PM