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

Recommended Answers

All 3 Replies

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.

<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 ?

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);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.