0

Hi all,

I have a structure for which array of objects are created
Now i want to return that array of objects to a function just like below:

struct mystruct s[5];
.
. code
.

return s;

in the function i captured using *s
void myfun(mystruct *s)

but i am unable to retrieve values using this s
c

If there is anything wrong in my syntax.. can anyone help me out to retrieve the structure values in the function

Thanks

2
Contributors
3
Replies
4
Views
6 Years
Discussion Span
Last Post by Isaac Remuant
0

The wording of this post is weird. Maybe posting the relevant code in CODE tags might help.
You can't return arrays. You can return a pointer to an array though.
What do you want to do? Use that array within a function? In that case, I recommend you to google how to pass an array as a paramater in a function.

0

<CODE>
struct mystruct
{
int x;
};

int main()
{
mystruct m[3];
m[0].x=2;
m[1].x=3;
m[2].x=4;
myfun(m);
}

void myfun(mystruct *m[])
{
cout<<m[0]->x;
}
</CODE>

this the type of functionality that i need..
but unable to get it...
wht could be other possible way ?

0

correct syntax:

<return type> <function name> (<type> <array name>[]);

You're mixing up concepts.

remember that arrays are passed automatically by reference in c++. I suggest you read about references, pointers and passing arrays into functions.
http://www.learncpp.com/cpp-tutorial/611-references/
http://www.learncpp.com/cpp-tutorial/612-references-vs-pointers-and-member-selection/
etc.


This will do what you ask:

#include <iostream>

using namespace std;

struct mystruct
{
int x;
};

void myfun(mystruct m[])
{
cout<<m[0].x; 
}

int main()
{
mystruct m[3];
m[0].x=2;
m[1].x=3;
m[2].x=4;
myfun(m);
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.