Thread: Question about strcpy

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    9

    Red face Question about strcpy

    Alright, well I would imagine the anwer to this question is quite obvious to anyone who programs in c often. Basically I am trying to use the standard library function strcpy inside of a function I am writing. For some reason when I use strcpy inside of main() it works fine. When I use strcpy outside of main() it compiles fine, but causes a runtime error. I wrote a simple program to demonstrate the problem. The programs runs fine the way it is, but if you uncomment the line in main() that calls testFunction, then it causes a run time error.

    Code:
    #include<stdio.h>
    #include<string.h>
    
    void testFunction(char *string);
    
    int main()
    {
    	char *a;
    	char *b = "test string";
    	strcpy(a,b);
    	puts(a);
    
    	/*testFunction("test string2"); */	
    	return 0;
    }
    
    void testFunction(char *string){
    	char *a;
    	strcpy(a, string);
    	puts(a);
    	
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Actually, you're only getting lucky that the strcpy() call in main() is working. The problem you're having is that you're not allocating any space for the destination string. char *a creates a pointer to type char. The problem is that that pointer doesn't point anywhere meaningful before you're trying to copy a string to it. You either need to use malloc() and friends to allocate some memory in which to store your string or you need to use char arrays to hold the strings.
    If you understand what you're doing, you're not learning anything.

  3. #3
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    Your code is incorrect in either case. That it works in one situation but not the other is because you got lucky.

    Your pointer 'a' is pointing at an arbitrary memory location. strcpy writes characters to the memory address stored by the value 'a'. This means it writes characters somewhere, and this location could be some memory address that contains another variable, since you have not initialized 'a' at all. You need to assign to 'a' a valid memory address. This might be the address of an array of fixed size, or it might be a memory address of a region of memory that has been allocated by the procedure, malloc().

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    9
    Thanks for taking the time to reply. I used a char array and it worked properly.

  5. #5
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    Quote Originally Posted by Kevinmun
    Thanks for taking the time to reply. I used a char array and it worked properly.
    Now it will work since you are statically allocating memory.....if you want to do it the previous way then follow what was mentioned in previous replies(use malloc).....

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. another do while question
    By kbpsu in forum C++ Programming
    Replies: 3
    Last Post: 03-23-2009, 12:14 PM
  2. Strcpy
    By Godders_2k in forum C Programming
    Replies: 17
    Last Post: 12-12-2007, 12:34 PM
  3. strcpy question
    By cman9999 in forum C Programming
    Replies: 9
    Last Post: 03-07-2003, 02:03 PM
  4. opengl DC question
    By SAMSAM in forum Game Programming
    Replies: 6
    Last Post: 02-26-2003, 09:22 PM
  5. strcpy
    By Luigi in forum C++ Programming
    Replies: 17
    Last Post: 02-16-2003, 04:11 PM