?
Solved

: warning C4172: returning address of local variable or temporary

Posted on 2004-09-13
3
Medium Priority
?
1,165 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 1000 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

766 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