Thread: Error - function returns address of local variable

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    3

    Error - function returns address of local variable

    Code:
    int main(void)
    {
        char brand[20];
    
        printf("Enter brand: ");
       brand = validatebrand();
    
    }
    
    char * validatebrand()
    {
       char brandName[20];	
    .....
    .......
    ......
    	if( (strlen (brandName)) >= 1 && (strlen (brandName)) < 20)
    	{	
    	  return brandName; <-- this is the warning
    	}		
    	else
           {	
    	return NULL;
           }
    }
    Ask user to key in brand, and use a validatebrand() to check whether it's valid or not, if it is valid, return the brandName. But when it is return, and i tried a printf to check what's returned in the main(), i found out tat it's not returned the exact data but the address. How i am going to make the validatebrand() to return the entire valid data after validated? Can anyone point it out?
    Thanks

  2. #2
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    C doesn't pass arrays. It insteads reduces it to a pointer.

    Now since the function is done by the time you would do a printf in main, all the local variables are no longer valid to read from.

    Also look at your else condition, you have a logic error there. If the else condition is done then you are trying to assign NULL to an array, which if you then try to printf it off would (hopefully) seg fault.

    Instead of returning the data try passing it the array.

    Code:
    int main(void)
    {
        char brand[20];
    
        printf("Enter brand: ");
       validatebrand(brand);
    
    }
    
    void validatebrand(char *brandName)
    {
    	fgets(brandName, 20, STDIN);  // fgets won't let you overflow the buffer.
    }

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    3
    problem solved
    thankss

  4. #4
    Day Dreamer
    Join Date
    Apr 2007
    Posts
    45
    Your problem might have been solved, but if you really want to write good robust code, dont ever return pointers to local variables.
    That is suicide!

    What if your program stack gets overwritten at the place where your local variable is defined?
    You may get the correct output now, but never ever do it in a large program.

  5. #5
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    Back in the day I *wish* I had warnings like that... as a matter of fact anyone know how (if) you can turn this warning on in GCC/MingW?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Code:
    #include <stdio.h>
    #include <string.h>
    
    char *foo ( void ) {
        char temp[100];
        strcpy(temp,"hello");
        return temp;            /* This is a pointer to a local */
    }
    
    int main ( ) {
        printf("&#37;d\n", foo() ); /* The format is wrong */
        return 0;
    }
    Two hard to detect problems found with these flags
    Code:
    $ gcc -W -Wall -ansi -pedantic -O2 foo.c
    foo.c: In function `foo':
    foo.c:7: warning: function returns address of local variable
    foo.c: In function `main':
    foo.c:11: warning: int format, pointer arg (arg 2)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  2. 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
  3. Im so lost at . .
    By hermit in forum C Programming
    Replies: 18
    Last Post: 05-15-2002, 01:26 AM
  4. eh, help?
    By The_Nymph in forum C Programming
    Replies: 2
    Last Post: 04-25-2002, 02:27 PM
  5. pointerz
    By xlordt in forum C Programming
    Replies: 6
    Last Post: 01-11-2002, 08:31 PM