Solved

: warning C4172: returning address of local variable or temporary

Posted on 2004-09-13
3
1,154 Views
Last Modified: 2007-12-19
I get this warning on the following  piece of code.  Can anyone tell me how to stop it please?
//converts the string data type to char array
char* stringToChar(string temp)                              
{                                          
      char arr[BIRD_SIZE];                              
      int i=0;                                          while(temp[i]!='\0')  
      {
      arr[i]=temp[i];                                    i++;                                          }                                          arr[i]='\0';                                          return(arr);                              
}                                                                                    
0
Comment
Question by:KazIT
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 

Author Comment

by:KazIT
ID: 12049871
//converts the string data type to char array
char* stringToChar(string temp)                                          
{                                                                                    
      char arr[BIRD_SIZE];                                                
      int i=0;                                                                  
      while(temp[i]!='\0')  
      {
            arr[i]=temp[i];                                                      
            i++;                                                                  
      }                                                                              
      arr[i]='\0';                                                                  
      return(arr);                                                                  
}                                                                                    
0
 
LVL 3

Accepted Solution

by:
Indrawati earned 250 total points
ID: 12050101
An easier way: if you are using C++ std::str, you can use it c_str member function to get its char representation. It's constant, however, you you may have to use strcpy to copy it to the resulting buffer.

Back to the question: the cause of the warning message is because you're returning arr, which is a local variable and it's not valid anymore after the function exits. You can change it to:

void stringToChar(string temp, char *arr)                                  
{                                                                                      
     int i=0;                                                      
     while(temp[i]!='\0')  
     {
          arr[i]=temp[i];                                            
          i++;                                                      
     }                                                                
     arr[i]='\0';                                                            
}

to remove the warning message.
0
 

Author Comment

by:KazIT
ID: 12050109
thank you Indrawati

Kaz
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question