Thread: nother way of doing strcpy

  1. #1
    Unregistered
    Guest

    nother way of doing strcpy

    what is a good way to do this?

  2. #2
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Why not strcpy?

    This works

    Code:
    	char a[]="Sorensen";
    	char b[256];
    	int i=0;
    	while(b[i++]=a[i])
    		;

  3. #3
    Unregistered
    Guest
    Originally posted by Sorensen
    Why not strcpy?

    This works

    Code:
    	char a[]="Sorensen";
    	char b[256];
    	int i=0;
    	while(b[i++]=a[i])
    		;
    i cant use array notation, pointers only! this is totally getting me! help

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    Ok -

    Code:
    	char* a="Sorensen";
    	char b[256];
    	char* c=b;
    	while(*c++=*a++)
    		;

  5. #5
    Registered User
    Join Date
    Jan 2002
    Posts
    16
    Mmmm would you need to add a line which places a '\0' at the end of the new string?

  6. #6
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >Mmmm would you need to add a line which places a '\0' at the end of the new string?

    No, because the '\0' will be assigned and then the while loop stops because *c contains '\0'. It's checking the target string not the source.

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    16
    Oh yeah, I see. check the target, thats an easier way to do it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A Full Program to analyze.
    By sergioms in forum C Programming
    Replies: 2
    Last Post: 12-30-2008, 09:42 AM
  2. Strcpy
    By Godders_2k in forum C Programming
    Replies: 17
    Last Post: 12-12-2007, 12:34 PM
  3. Where is strcpy() defined? (Can't find in string.h ect)
    By Zero_Point in forum C++ Programming
    Replies: 6
    Last Post: 04-03-2006, 05:14 PM
  4. Question about strcpy
    By Kevinmun in forum C Programming
    Replies: 4
    Last Post: 11-02-2005, 11:00 PM
  5. strcpy
    By Luigi in forum C++ Programming
    Replies: 17
    Last Post: 02-16-2003, 04:11 PM