pybind / pybind/pybind11

[BUG]: How to return a array of structure from cpp to python

Open
#3,545 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

triage
Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

Required prerequisites
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

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. 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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.