#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    23
    Rep Power
    0

    Program using pointers


    I want to simulate the use of strcpy using pointers.

    The user enters word "x" and the program copies it into word "y" and prints it on the screen. Here's what I wrote so far I know something's not good in that for :(. Also please explain what each line does if you provide an answer thank you very much!

    Code:
    #include <stdio.h>
    int main()
    {
     char x[100];
     char y[100];
     int i;
     char *p;
     printf("Enter word x: ");
     scanf("%s", x);    
     
     for(p=x; *p; p++)
     p=y;
     
     printf("Word y is: %s",y);
     getch();
     
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,172
    Rep Power
    2222
    Uh, you wrote it, so you tell us what each line does. Then we can tell you whether you are right.

    And do please indent your code!

    For a moment, I will assume that you did actually write this (your request screams out to us that you stole^H^H^H^H^Hresearched it from somewhere and are trying to pass it off as your own; it has been tried here far too many times with the exact same request for us to explain what you had allegedly written yourself).

    This line,
    p=y;
    does not do what you want it to do. Consider creating two char pointers, one for each string, and use them. Also review how to dereference a pointer.

    And please indent your code!
    Last edited by dwise1_aol; July 23rd, 2013 at 10:10 AM.
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Location
    India
    Posts
    95
    Rep Power
    4
    p=y;
    just think what are you doing here....
    and u will get idea what to do.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Posts
    109
    Rep Power
    3
    When you write p=y, it copies Y into P (that's the syntax), but Y is empty !

    Comments on this post

    • jwdonahue disagrees : Actually y is always the address of y[0] in this case, therefore p = y sets p pointing to y[0].
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2012
    Posts
    29
    Rep Power
    0
    Try using strcpy instead of just coying directly , you had taken it as a char type not a int type

    Comments on this post

    • jwdonahue disagrees : That probably wasn't the OP's assignment and definitely doesn't seem help, given the OP clearly stated they wanted to it without strcpy. Also, strncpy would be much safer.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Loyal (3000 - 3499 posts)

    Join Date
    May 2004
    Posts
    3,417
    Rep Power
    887
    Well dwise1_aol gave you the best hint so far. How did you do on your assignment with this one? What does your code look like now?

    Basically x and y are just pointers to 100 char buffers on the stack.

    Your for loop is bogus. Given x[] and y[] you don't actually need p. You can use array indexes. Then your for loop would use int i as an index into both arrays as you increment i until '\0' == x[i]. I would venture a guess that you added p because you couldn't increment y, but then, it's possible this isn't your code. You're not allowed to change the values of x or y because they are arrays.

    The way you are using p, it is initially an alias of x ( the 'p=x' statement), but then you make it an alias of y (the 'p=y' statement) and after that the p++ expression makes it an alias for the address of y[1]. All of this pointer manipulation does nothing in the way of copying the string in x to y. But, because your conditional is *p, the loop is likely to run forever because x[0], y[0] and y[1] probably aren't '\0'.

    Now if you put some effort into this, you can still complete your homework assignment.

    Good luck and please do let us know how it's going.
    I no longer wish to be associated with this site.

IMN logo majestic logo threadwatch logo seochat tools logo