Solved

: warning C4172: returning address of local variable or temporary

Posted on 2004-09-13
3
1,099 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
Comment Utility
//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
Comment Utility
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
Comment Utility
thank you Indrawati

Kaz
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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.

771 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

12 Experts available now in Live!

Get 1:1 Help Now