Thread: copy char array

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    123

    copy char array

    This code is meant to copy string from one string to another. output I get is gibberish. I suspect it is a matter of initializing (or not...).

    Code:
    #include<stdio.h>
    void cpystring(const char *source, char *target)
    {
    	
    	char *p=target;
    	while (*p++=*source++);
    	puts (target);
    	puts (source);
    }
    
    int main(void)
    {
    	char source[6]="qwerty", target[6];
    	cpystring (source, target);
    
    	return 0;
    }

  2. #2
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    Try this:
    Code:
    void cpystring(const char *source, char *target)
    {
    	/*
    	char *p=target;
    	while (*p++=*source++);
    	puts (target);
    	puts (source);*/
    	
    	
    	int i=0;
    	while(source[i]!='\0')
    	{
    		target[i]=source[i];
    		++i;
    	}
    	target[i]='\0';
    	puts(source);
    	puts(target);
    }

  3. #3
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    And main:
    Code:
    int main(void)
    {
    	char source[7]="qwerty", target[7];
    	cpystring (source, target);
    
    	return 0;
    }
    The reasons why your code don't work.
    1. char source[6]="qwerty";
    you have 6 characters + additional character calle zero character ('\0') that terminates string. Remember string is just ordinary array of characters that ends with special character '\0'. So you always must to make a room for it.
    2.
    You are incrementing source. That is pointer to first element in the array in the begining. After you increment it you are trying to display it with puts() but what is the address of pointer source and what is the value on which points to after while?
    For example at the start of your function source has this value:
    0x0012fed0, that is the address (beacuse source is a pointer) of first element (byte) in the array and that is letter 'q'. After while
    value of pointer variable source is: 0x0012fed7, because there is 7 elements (including '\0') and source is pointing to a byte of memory immediately after '\0' and what is on that memory? You don't know. That's why you get rgarbage characters.

    Another solution would be:
    Code:
    void cpystring(const char *source, char *target)
    {
    	
    	char *p=target;
    	char *m=(char*) source;
    	while (*p++=*m++);
    	puts (target);
    	puts (source);
    }
    Cheers!
    Last edited by Micko; 08-29-2004 at 11:36 AM.

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    OK, thanx!
    the only thing is I'm not familiar with this syntax:

    (char*) source
    please explain what does it mean.

  5. #5
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    (char*) is a type cast, which means to convert whatever source is into a char *. Of course it isn't needed since source is already a char *

  6. #6
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    Thank's.
    ((-:

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Of course it isn't needed since source is already a char *
    It's a const char *, so the cast would silence an annoying warning. Of course, a better solution would be to make m a const char * as well and avoid the issue altogether.
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    what does the const char mean?

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >what does the const char mean?
    In theory you aren't allowed to change a const qualified variable. In reality you can easily subvert the type system and modify something that is const, sometimes with disasterous results.
    My best code is written with the delete key.

  10. #10
    Registered User
    Join Date
    Jun 2004
    Posts
    123
    Do you recomend not to use it? How do you manage without? what is the substitute?

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Do you recomend not to use it?
    Of course not. If you behave when using it then const will catch bugs that would otherwise show up at run-time. I recommend liberal use of const.

    >How do you manage without?
    Prayer seems to work for many of my coworkers.

    >what is the substitute?
    Code:
    /* PLEASE DON'T CHANGE THIS!!!!!!!111 */
    My best code is written with the delete key.

  12. #12
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    opps didn't even see the const there. Thats twice in the last 24 hours I've done something like that.

  13. #13
    Registered User Micko's Avatar
    Join Date
    Nov 2003
    Posts
    715
    >Of course it isn't needed since source is already a char *
    It's a const char *, so the cast would silence an annoying warning. Of course, a better solution would be to make m a const char * as well and avoid the issue altogether.
    Credit to teacher Prelude!

    I cast it to avoid compiler warning because of const.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Sorting Linked Lists
    By DKING89 in forum C Programming
    Replies: 6
    Last Post: 04-09-2008, 07:36 AM
  2. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  4. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM