0

I ran into a problem where I declared a function this way:

struct my_struct get_info();

It's supposed to return a pointer to a struct, and then in main that pointer is captured, like so:

struct my_struct *p_struct;
p_struct = get_info();

Then I got a compiler error saying that it could not convert from 'my_struct *' to 'my_struct. I looked around at code that does the same thing and I noticed that in their function prototype and definition they had an * before the function name, like:

struct my_struct *get_info();

After I added in the *, everything was fine. I don't know why it fixes it though, and why you would do that. Do you only do that when returning a struct pointer? Or are there other cases you would want to do that? Thanks.

Bruce
  • 7,094
  • 1
  • 25
  • 42
John
  • 3
  • 2

4 Answers4

1

It's supposed to return a pointer to a struct

But your code is not returning a pointer to struct. Is it?

Didn't you mean struct my_struct* get_info(); ?

Prasoon Saurav
  • 91,295
  • 49
  • 239
  • 345
  • It does, I actually just picked up my textbook and found that you have to do this when returning pointers. Just not something I've done in a while. – John Apr 20 '11 at 04:41
1

struct my_struct* denotes a pointer, simple as that. So when you want to return a pointer, the return type should be a pointer aswell, no?
You can write that struct my_struct *get_info(); as struct my_struct* get_info();, which may hint more at what the real return type is for you. The * belongs to that return type, not to the function itself.

Xeo
  • 129,499
  • 52
  • 291
  • 397
0

You can pass (struct)s around by value instead of as pointers. It's usually not a good idea (use references, or pointers if you must, instead). So you need to use & for reference or * for pointer, or it will expect you to return it by value.

geekosaur
  • 59,309
  • 11
  • 123
  • 114
  • 1
    Why is not a good idea to pass structures by value? That is way to grand a blank statement to be either true or useful advice. – Martin York Apr 20 '11 at 04:52
  • @Martin: because they are generally large and unwieldy, especially on the stack. I didn't think it would be necessary to go into details of limits on stack frames, multiple unnecessary copying, etc. – geekosaur Apr 20 '11 at 04:55
  • 3
    Hate to disagree but most structs are not large or unwieldy. std::string, std::vector, std::complex are just a few that spring to mind. – Martin York Apr 20 '11 at 04:56
  • 1
    @Martin: from the point of view of the compiler, anything larger than the largest machine register requires special return arrangements and is therefore unwieldy. – geekosaur Apr 20 '11 at 05:02
  • 2
    Not true. Most modern compiler implement RVO and NRVO. This means that the returned object is not actually returned but built in place at the final destination. Thus the compiler elides the copy construction and the destruction operation. Also the cost of copying a couple of registers worth of information is minimal in comparison to the extra code required to manage dynamically allocated memory. – Martin York Apr 20 '11 at 05:49
0

The bottom line seems to be that your code was inconsistent - saying "return pointer-to-struct-variable" in a function declared to return a struct directly. They are different things. You seem to think the compiler could guess or know which was a mistake, perhaps thinking that people always return structs by pointer, but this isn't true and indeed the compiler can't know for sure, so the best thing it can do to ensure the program works as intended is insist that you - the programmer - return to the code and make it consistent.

Tony Delroy
  • 102,968
  • 15
  • 177
  • 252