Thread: Function question for brain-fried newb

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    18

    Question Function question for brain-fried newb

    Hello ne-er do wells,

    I could use a hand with this assignment I've got. I had it all good to go until the professor requested I try rewriting it using functions. Ok no sweat...what I can't figure out now is how to return a string from this function. I know the answer is on the tip of my nose but I'm starting to go cross-eyed...The function needs to accept the full name (first and last with whitespace) and return the full name to another function.

    Code:
    void studentName (void)
    {
    	char name[50];
    
    	printf	("\n\nPlease enter student's name:");
    	scanf	( "%[^\n]", name );
    	printf	("\n\nStudent's name is %s:", name);
    	
    	}
    Thanks in advance!!!!

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    You don't return a local string from a function because local variables are destroyed when the function returns. In most cases the best option is to pass a buffer as an argument to the function:
    Code:
    void studentName ( char name[50] )
    {
      printf ( "\n\nPlease enter student's name: " );
      fflush ( stdout );
      scanf ( "%49[^\n]", name );
      printf ( "\n\nStudent's name is %s\n", name );
    }
    
    ...
    
    char name[50];
    studentName ( name );
    Then you can return a pointer to the buffer for further convenience:
    Code:
    char *studentName ( char name[50] )
    {
      printf ( "\n\nPlease enter student's name: " );
      fflush ( stdout );
      scanf ( "%49[^\n]", name );
    
      return name;
    }
    
    ...
    
    char name[50];
    printf ( "\n\nStudent's name is %s\n", studentName ( name ) );
    My best code is written with the delete key.

  3. #3
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    You will want to return a pointer from studentName() - however, be aware that you will need to do something about where your 'name' will be stored if you go this route - as your function is now, as soon as it finishes, your locally declared array name will go out of scope, and the information in the array will be lost. Now there are a number of ways to remedy this, a few of which are:

    1) Declare your array as static
    2) Allocate some space with malloc()
    3) Declare an array in main() and pass a pointer to it to your function

    ~/

    edit:: Uh, obviously I type slower than Prelude..

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    18
    Many thanks to you both...I guess it wasn't anywhere near the tip of my nose...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM
  2. dllimport function not allowed
    By steve1_rm in forum C++ Programming
    Replies: 5
    Last Post: 03-11-2008, 03:33 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  5. Calling a Thread with a Function Pointer.
    By ScrollMaster in forum Windows Programming
    Replies: 6
    Last Post: 06-10-2006, 08:56 AM