Like Tree2Likes
  • 2 Post By tabstop

Strcpy function

This is a discussion on Strcpy function within the C Programming forums, part of the General Programming Boards category; I was trying to copy a string into another without with my own program but there is no output on ...

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    19

    Strcpy function

    I was trying to copy a string into another without with my own program
    but there is no output on screen -->

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    char * copy(char*s)
    {
    	char *b=malloc(strlen(s));
    	
    	
    	
    	while(*s!='\0')
    	{
    		*b=*s;
    		s++;
    		b++;
    
    	}
    	*b='\0';
    	return b;
    }
    
    int main(void)
    {
    	char *a="Hello Tarun";
    	char *b;
    	b=copy(a);
    	puts(b);
    	return 0;
    
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    At the end of your loop, where is b pointing?

    And, fwiw, not seeing the result isn't the only problem you have.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    19
    Quote Originally Posted by CommonTater View Post
    At the end of your loop, where is b pointing?

    And, fwiw, not seeing the result isn't the only problem you have.
    b is collecting the pointer returned from function
    yes no output is something wrong with code ??

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by tarunjain07 View Post
    b is collecting the pointer returned from function
    That may be what you want it to do. But that's not what it's doing. Read what's actually there, not what you want it to be.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,866
    Quote Originally Posted by tarunjain07 View Post
    b is collecting the pointer returned from function
    yes no output is something wrong with code ??
    There are many things wrong with the code. As with your other post you are not understanding what you are trying to do. You cannot just randomly throw code at a problem and hope a working solution comes out. I would like to point something out for you, via code, in case we have a language barrier here. Note: Even in example code I am not using single letter variables. It is an extreme bad practice outside of the world of loop iterators.

    Code:
    #include <stdio.h>
    
    int myChar(char *myString){
    
    	int FunctionNumber=0;
    
    	while(*myString != '\0'){
    		myString++;
    		FunctionNumber++;
    	}
    
    	return FunctionNumber;
    }
    
    int main(void){
    
    	int numberReturn = 0;
    	char *StartString = "Hello Tarun";
    
    	numberReturn = myChar(StartString);
    
    	printf("%d", numberReturn);
    
    	getchar();
    	return (0);
    }
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tarunjain07 View Post
    b is collecting the pointer returned from function
    yes no output is something wrong with code ??
    At the end of your function when you return b ... b is pointing at the null on the end of your string.

    Not only that, but the minute you did b++ you orphaned the memory you reserved for the string, causing a "memory leak" where free() will fail to release it.

    You need to preserve your pointers in their original locations or you lose your strings...
    Code:
    char * copy(char*s)
      { char *b=malloc(strlen(s) + 1);  // allow for trailing null
         int i = 0;
         do 
           { b[i]=s[i];
             i++; }
         while(s[1])
         return b; }
    Then in main you need to release the memory you've reserved...
    Code:
    int main(void)
    {
        char *a="Hello Tarun";
        char *b  = NULL;
        b = copy(a);
        puts(b);
        free(b);
        return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 06-19-2010, 08:42 AM
  2. problem in my strcpy strcat function
    By genie in forum C Programming
    Replies: 7
    Last Post: 11-07-2008, 09:36 AM
  3. Strcpy
    By Godders_2k in forum C Programming
    Replies: 17
    Last Post: 12-12-2007, 12:34 PM
  4. strcpy(pstring, function())
    By kooma in forum C++ Programming
    Replies: 8
    Last Post: 03-03-2002, 04:33 AM
  5. strcpy() function
    By Narciss in forum C Programming
    Replies: 4
    Last Post: 09-21-2001, 01:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21