Solved

: warning C4172: returning address of local variable or temporary

Posted on 2004-09-13
3
1,137 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
  • 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
getting a fire fox page (view page source ) in c++ 6 42
Arduino EDI - Programming language 3 89
Problem with SqlConnection 4 168
Why isn't object file created? 6 57
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

816 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now