-
April 27th, 2009, 08:22 AM
#1
return function
Why do I need char *Function()? I mean, why do I need the * in front of the Function1() ??
Does the * mean pointer?
Code:
#include <iostream>
using namespace std;
char *Function1()
{
return "Some text";
}
int main()
{
cout<<Function1();
system("PAUSE");
return 0;
}
-
April 27th, 2009, 08:30 AM
#2
Re: return function
Originally Posted by StGuru
Why do I need char *Function()? I mean, why do I need the * in front of the Function1() ??
Does the * mean pointer?
Yes. If you only return "char", you are returning a single character. With "char*" you are returning a pointer to the first element in an array of characters, whose end is signified by the character '\0', aka a C-style string.
-
April 27th, 2009, 08:35 AM
#3
Re: return function
1. To make it more convenient to understand I'd rewrite it as:Which just means that the function Function1() returns the value of the char* type (a pointer to the character array)
1. Your Function1() implementation is wrong: the char array "Some text" goes out of scope just before this function is returning. So it will always return a pointer to the garbage!
Victor Nijegorodov
-
April 27th, 2009, 08:50 AM
#4
Re: return function
Thanks for the replies.
@Speedo does the pointer points to the first element of the array? The array itself its a pointer, right?
@VictorN I don't understand why you say my Implementation is wrong?
-
April 27th, 2009, 09:11 AM
#5
Re: return function
Originally Posted by VictorN
Your Function1() implementation is wrong: the char array "Some text" goes out of scope just before this function is returning. So it will always return a pointer to the garbage!
I had the impression that since a string literal is returned that is not so due to static storage duration. What would be wrong is the return type, i.e., it should be const char* instead of just char*.
Originally Posted by StGuru
does the pointer points to the first element of the array?
Yes, assuming that VictorN is wrong and the string literal is not destroyed, in which case the pointer returned would be pointing to something that does not exist.
Originally Posted by StGuru
The array itself its a pointer, right?
No, it is an array.
-
April 27th, 2009, 09:17 AM
#6
Re: return function
An array is always convertible to the pointer of the same type, however, so in effect the array "is" a pointer in a sense.
-
April 27th, 2009, 09:18 AM
#7
Re: return function
Wait are u saying to me that I return pointer which doesn't exists in the function implementation?
-
April 27th, 2009, 09:21 AM
#8
Re: return function
Originally Posted by laserlight
I had the impression that since a string literal is returned that is not so due to static storage duration.
Yes, it is correct.
However, since the OP wrote this "string literal" as "Some text" I was (and still am) afraid, this function is supposed to return any text somehow generated there...
Victor Nijegorodov
-
April 27th, 2009, 09:24 AM
#9
Re: return function
Originally Posted by Lindley
An array is always convertible to the pointer of the same type
No, an array is always convertible to a pointer to its first element, which certainly is not of the same type as the array
Originally Posted by StGuru
Wait are u saying to me that I return pointer which doesn't exists in the function implementation?
No, what VictorN is saying that by returning a string literal as you do in your Function1() function, the caller (i.e., the main() function) gets a pointer to something that no longer exists since by going out of scope the string literal is destroyed. However, I recall being told differently in the case of string literals, so I am not sure if VictorN is correct. The easiest way out is to simply don't risk it: e.g., return a std::string instead of a pointer.
EDIT:
Read VictorN's reply in post #8.
-
April 27th, 2009, 09:36 AM
#10
Re: return function
laserlight now I understand what are u saying to me. But I don't find I reason, why the code should not work. It is everything correct. It is true that after calling the function Function1 the pointer will be destroyed. But how that changes something?
-
April 27th, 2009, 09:46 AM
#11
Re: return function
Originally Posted by StGuru
But I don't find I reason, why the code should not work. It is everything correct. It is true that after calling the function Function1 the pointer will be destroyed. But how that changes something?
No, the pointer in main() will correctly point to the first character of the string literal. So, in this sense it is much ado about nothing: your code is correct on that point.
Let's look at an example where it is not correct:
Code:
#include <iostream>
const char* function2()
{
char text[] = "hello world";
return text;
}
int main()
{
const char* str = function2();
std::cout << str << std::endl;
}
The problem is that text only exists within function2(). When function2() returns, it is destroyed. Therefore, str would point to something that no longer exists. This is a problem because if it no longer exists, how can you print it? Maybe you do manage to print it, but that would just be "luck".
-
April 27th, 2009, 09:56 AM
#12
Re: return function
I got error [Warning] address of local variable `text' returned.
Anyway, I got the text printed on the screen without altering the code.
btw why u use const?
Last edited by StGuru; April 27th, 2009 at 09:58 AM.
-
April 27th, 2009, 10:04 AM
#13
Re: return function
Originally Posted by StGuru
I got error [Warning] address of local variable `text' returned.
Anyway, I got the text printed on the screen without altering the code.
Well, try another example:
Code:
char* function3()
{
char text[20];
strcpy(text, "hello world");
return text;
}
Victor Nijegorodov
-
April 27th, 2009, 10:05 AM
#14
Re: return function
Originally Posted by StGuru
I got error [Warning] address of local variable `text' returned.
That's a good thing.
Originally Posted by StGuru
Anyway, I got the text printed on the screen without altering the code.
Pure luck.
Originally Posted by StGuru
btw why u use const?
const-qualification in this context doesn't matter much. The point of the snippet was to demonstrate what happens when you attempt to read/write to something that no longer exists.
Conversely, const-qualification is very important in your original snippet's context. In fact, implicit convention from const to non-const qualification has been deprecated (for string literals) -- not even mentioning the fact that it's extremely easy to run into undefined behavior by doing this.
Last edited by Plasmator; April 27th, 2009 at 10:09 AM.
Reason: Added clarification.
-
April 27th, 2009, 10:14 AM
#15
Re: return function
Perhaps this will better demonstrate the problem.
Code:
#include <iostream>
int function3()
{
int vals[] = {42, 23, 64, 1, 0};
return vals[1];
}
const char* function2()
{
char text[] = "hello world";
return text;
}
int main()
{
const char* str = function2();
int val = function3();
std::cout << str << std::endl;
}
The point being that while it was incorrect to access the no-longer-valid str pointer before, there was a good chance it would "work" because nothing was upsetting the state of the stack before it was output. Now there's this other function that tramples all over the stack in between, so the results are less likely to be pretty.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|