2uzhan.com
Advertisement
Now Place:2uzhan.com » Related to pointer array to structures

Related to pointer array to structures

Software Development @ November 25, 2010   Views: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

--------------Solutions-------------

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);
}

Before I get into the specific issues, I want to provide some other details:

  • Normally, I work on a live, paid server (even in dev); I realize that is ...

Yes, WPF. The learning curve is steep, but what amazing things you can do with it!
Look at this hexagon window for instance. Serves no purpose in a business environment ...

Hey there,

I am trying to create a register code. So basicly im making a quiz and every time a user starts up my program he/she needs to register (making ...

Tags:
© 2018 2uzhan.com Contact