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

Dani AI

Generated

Quick diagnosis tied to the posted snippets: the array in main is an array of structs, but the function signature you used expects an array of pointers. That mismatch makes expressions like m[0]->x wrong for your data. Also, when an array is passed to a function it decays to a pointer to the first element — so you must either accept that pointer (and the element count) or use a container/type that preserves size. was pointing in this direction; ’s code shows the common confusion between “array of T” and “array of T*”.

Safer, modern alternatives (examples):

std::vector<mystruct> make_structs()
{
    std::vector<mystruct> v(3);
    v[0].x = 2;
    return v; // efficient: move semantics
}

For fixed-size arrays and compile-time size preservation:

template<size_t N>
void myfun(mystruct (&arr)[N])
{
    // arr has known size N inside the function
}

If you must work with raw arrays allocated on the heap, prefer smart pointers:

auto p = std::make_unique<mystruct[]>(3);
// use p[0].x ...
return p; // returns ownership safely (C++14+)

Practical tips and cautions:

  • Do not return a pointer to a local (stack) array — that yields undefined behavior.
  • If function receives an array, pass the length too (or use std::vector/std::span/std::array so size is available).
  • Use . for struct values (elem.x) and -> only when elements are pointers.
  • Compile with warnings enabled (e.g., -Wall -Wextra) and use AddressSanitizer/Valgrind for runtime issues.

These patterns avoid the pointer/pointer-to-pointer trap seen in the thread and give safer, clearer ownership and lifetime semantics than raw arrays.

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.