Thread: String Returning Function Problems.....

  1. #1
    Mistake is a way to learn
    Join Date
    Sep 2006
    Location
    Malaysia
    Posts
    11

    Arrow String Returning Function Problems.....

    Can somebody give me a clue in this?
    I'm trying to make up a function that will return a string value to the main function.. Here is my best attempt on doing so.

    Code:
    #include <stdio.h>
    
    char* MyFunction();
    
    int main()
    {
    	char Strings[40];
    	Strings = MyFunction();
    	printf("%s",Strings);
    	
    	return 0;
    }
    
    char* MyFunction()
    {
    	char ItIsStrings[40];
    	printf("Please insert strings: ");
    	scanf("%s",ItIsStrings);
    	return(ItIsStrings);
    }
    Can somebody help me with this?? It can't be compiled...

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    1 - You can't return arrays.
    2 - You can't assign strings to arrays.
    3 - Look up the strcpy function to copy something to character array.
    4 - Try passing the array to the function, and then filling using the afore mentioned function.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    204
    ItIsStrings is a local variable that goes out of scope as soon as MyFunction returns. You should either pass MyFunction a pointer to an array of characters or declare ItIsString as static.

  4. #4
    Jaguar
    Join Date
    Sep 2006
    Posts
    12
    Yep,
    define ItIsStrings as static inside the function since it's local and define Strings as a char pointer or use strcpy function to copy the string as mentioned above.

    Jag

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. string erase function problems
    By westm2000 in forum C++ Programming
    Replies: 5
    Last Post: 09-15-2003, 10:19 PM
  5. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM