Thread: Pointers (die! die! die! )

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    3

    Pointers (die! die! die! )

    Could anyone spare some advice as to the function of that while statement at the bottom, esp. the "/0". Thanks.

    #include <iostream.h>
    int stringcopy(char *to, char *from);
    int main()
    {
    char from[] = "";
    cin>>from;
    char to[sizeof from] = "";
    stringcopy(to, from);
    cout<<from<<to;
    return 0;
    }

    int stringcopy(char *to, char *from);
    {
    while ( (*to = *from) != '\0' )
    {
    to++;
    from++;
    }
    return 0;
    }

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    #define MAX_STR_LEN 50//Max length
    
    int stringcopy(char *to, char *from);
    
    
    int main()
    {
    char from[MAX_STR_LEN] = {0},
    	 to[MAX_STR_LEN] = {0};
    
    cin.getline(from,MAX_STR_LEN);//Allow line with spaces
    stringcopy(to, from);
    cout  << from << " "<< to;
    return 0;
    }
    
    int stringcopy(char *to, char *from)
    {
    while ( (*to = *from) != '\0' )
    {
    to++;
    from++;
    }
    return 0;
    }
    You need to create space for the strings.....

    Try checking out the string object as defined in the C++ std library....makes life easier

  3. #3
    Registered User
    Join Date
    Jun 2002
    Posts
    3
    Thanks, but i don't really see the significance of the '\0' in the while loop.

  4. #4
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Originally posted by BatmaN77
    Thanks, but i don't really see the significance of the '\0' in the while loop.
    LOL....you put it there dude!!!

    Strings in C all end with a NULL char (0 on the ASCII charector list)....so all you are doing is looping through until you reach the NULL...and that marks the end of the string.

  5. #5
    Registered User
    Join Date
    Jun 2002
    Posts
    3
    Noooo, that example was from a book. But thanks, I finally get it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 1337 bible, Gen 11
    By Paz_Rax in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 05-20-2005, 09:40 PM
  2. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  3. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM