parsing a line for words...
Hi,
I'm getting this when I compile my program. Then, when the program runs I get an error. I assume it is because I have the warning for this problem. How can get around this error.
warning C4172: returning address of local variable or temporary
Code:
#include <iostream>
#include <cstring>
using namespace std;
char* parse_words(char* pstr, int strp);
int main()
{
char* strin("This is the string to be parsed");
cout << " String is: " << strin << " Length is: " << strlen(strin) << "\n\nParsing...\n";
/* cout << "Please enter a string to parse word-by-word: \n";
cin >> strin;
cout << "This is the string entered: " << strin << " \n.";
*/
//char * p(nullptr);
char* p = parse_words(strin, strlen(strin));
//char* p = parse(s1);
while (p)
{
std::cout << p << "\n.";
delete [] p;
p = parse_words(nullptr, strlen(strin));
}
return 0;
}
char* parse_words(char* pstr, int strp)
{
static int start(0);
static int len(strp);
cout << "Value of len: " << len << '\n';
int lenpm(len);
int pos(0);
int x;
char* pReturn(nullptr);
int holdlen(20);
char holdarea[20];
cout << "Start start loop\n";
for (start; start <= len; start++)
{
cout << "Start loop: \n";
for (x = 0; x <= holdlen; x++)
{
holdarea[x] = ' ';
}
if (*pstr != ' ')
while (*pstr != ' ')
{
holdarea[x] = *pstr;
x++;
*pstr++;
start++;
}
return holdarea;
}
return pReturn;
}
Re: parsing a line for words...
Typically you'd pass in holdarea as an output parameter.
Re: parsing a line for words...
Quote:
Originally Posted by
FenixRising
Hi,
I'm getting this when I compile my program. Then, when the program runs I get an error. I assume it is because I have the warning for this problem. How can get around this error.
warning C4172: returning address of local variable or temporary
That isn't the only problem.
First, it is undefined behaviour to return the address or reference to a local variable. The reason is why the variable is called local. When the function returns, that local variable no longer exists -- so what are you pointing to in main() when that function returns? Garbage.
Second, why are you doing this?
Where is there a call to new[] in your program, and then assigning "p" to that value? You only call delete[] when you use new[]. Calling delete[] on an area of memory that was not dynamically allocated is again, undefined behaviour.
Why don't you just use strtok() to parse the string?
http://www.cplusplus.com/reference/cstring/strtok/
In addition, there are other safer ways to parse a string in C++ for individual words. The first way is to get rid of this "C" type of coding and just call string::find_first_of() or string::find_first_not_of() in a loop.
Regards,
Paul McKenzie
Re: parsing a line for words...
First, delete [] p; was left in the program by mistake. Secondly, I am reading two books. I have read 550 pages in one and 450 in another. Both books were written and released in late 2012. The "C" type of coding you speak of comes from these books. And there is only a small amount of information on strings or maybe I missed it. If you know of a book that focuses just on C++ without the comparisons to "C" let me know. I'll be the first one in line to buy it.
Re: parsing a line for words...
Quote:
Originally Posted by
FenixRising
Secondly, I am reading two books. I have read 550 pages in one and 450 in another. Both books were written and released in late 2012. The "C" type of coding you speak of comes from these books.
Then those books aren't teaching C++. They are teaching you 'C'.
Quote:
And there is only a small amount of information on strings or maybe I missed it.
So after 500 pages, there is no mention of std::string? Are you sure you are reading C++ books?
Quote:
If you know of a book that focuses just on C++ without the comparisons to "C" let me know.
Accelerated C++ by Koenig & Moo.
Regards,
Paul McKenzie
Re: parsing a line for words...
Ok, I have ordered the book. It will be here Wednesday.
Regards,
Bob
Re: parsing a line for words...
Which two books are you reading?
I know one is c++ Primer Plus from a previous post which is considered quite good but doesn't really get into c++ proper until about chapter 10 when it introduces classes, and string and the other STL elements are not introduced until chapter 16.
What's the other?
You might also like to have a look at
Exploring c++
http://www.amazon.co.uk/Exploring-Th...loring+c%2B%2B
Note that both Exploring c++ and Accelerated c++ don't cover the new parts of the c++11 standard.
Re: parsing a line for words...
The other book is Beginning Visual C++ 2012. Actually, I like both books. But, everyone seems to think I should be using strings. And, I am a little bit confused about that.
Re: parsing a line for words...
In c++ there are effectively two types of 'strings'. There are the 'c' style strings which are an array of char terminated by the null character. This is what you have been using.
Then there is the c++ string class that is part of the Standard Template LIbrary (STL). This is much more powerful and easier to use than the 'c' string. Unless there is a good reason not to, developers use the c++ string class.
Learning to program in c++ using 'c' strings is learning a way of doing things which later may have to be 'unlearnt'. There are 'c' ways of doing things (which work with c++ mainly) and there are the 'c++' ways of doing things. Professional c++ developers tend to want programmers to learn the 'c++' way rather than the 'c' way. Just as you are using cin and cout (the c++ way) as opposed to using printf(), scanf() etc (which is the 'c' way) use type string (c++ way) as opposed to char* (c way).
Many books, however, - even the good ones - tend to teach the 'c' way with some aspects of the language before showing the c++ way (ie 'c' style strings before the c++ strings). So when code is posted using the 'c' way of doing things (as opposed to the c++ way) this can be commented upon.
Re: parsing a line for words...
Quote:
Originally Posted by
FenixRising
But, everyone seems to think I should be using strings.
Including the author of the C++ language himself:
http://www.stroustrup.com/new_learning.pdf
Regards,
Paul McKenzie
Re: parsing a line for words...
You need to be comfortable with C style null terminated strings as they're still commonly used in a lot of APIs, including the Windows API. Use strings where you can, but you'll need to know both.