[BUG]: How to return a array of structure from cpp to python
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 18k
- Forks
- 2.3k
- Avg merge
- 5d 17h
- Merged PRs (30d)
- 10
Description
Required prerequisites
- Make sure you've read the documentation. Your issue may be addressed there.
- Search the issue tracker and Discussions to verify that this hasn't already been reported. +1 or comment there if it has.
- Consider asking first in the Gitter chat room or in a Discussion.
Problem description
Hi, I have a struct:
typedef struct values
{
float x;
float y;
float w;
float h;
}val;
typedef struct Name
{
unsigned int num;
val *data;
}name;
and i am creating a instance of name:
name data = Null;
and storing desired values in data like
ex: data. num = 10
and in wrapper
PYBIND11_MODULE(pybind11_example, m) {
m.doc() = "pybind11 example plugin"; // Optional module docstring
py::class_<val>(m, "values")
.def(py::init<>())
.def_readwrite("x", &val::x)
.def_readwrite("y", &val::y)
.def_readwrite("w", &val::w)
.def_readwrite("h", &val::h);
py::class_<name>(m, "name")
.def(py::init<>())
.def_readwrite("num", &name::num)
.def_readwrite("num", &name::data);
}
N di am returning data from cpp code.
But whenever i try to call (pybind11_example.num)i am able to get only 0th address data
Please suggest correct way to bind.
Reproducible example code
No response
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the PYBIND11_MODULE wrapper and the values and name structures shown in the issue, then consult the pybind11 documentation on binding pointer-backed data and arrays. A complete report should include a reproducible example; the work is done when the returned structure exposes all intended values rather than only the first address.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, python
- Domain
- api
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100