Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6930439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T11:26:50+00:00 2026-05-27T11:26:50+00:00

I am trying to implement this C structures in python using ctypes: struct _rows

  • 0

I am trying to implement this C structures in python using ctypes:

struct _rows {
    int cols_count;
    char *cols[];
}

struct _unit {
    int rows_count;
    struct _rows *rows;
}

int my_func(struct _unit *param);

Problem is that _rows.cols is a dynamically sized array of char pointer and _unit.rows is a dynamically sized array of _rows structure. How can I implement this using ctypes in python?

I was able to define a function that will return a _rows structure with variable number of char pointers:

def get_row(cols):
    class Row(ctypes.Structure):
        _fields_ = [("cols_count", ctypes.c_int), 
                    ("cols", ctypes.c_char_p * cols)
                   ]

I don’t know what to do nex, it’s all a bit fuzzy and ctypes documentation isn’t helping.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T11:26:51+00:00Added an answer on May 27, 2026 at 11:26 am

    I’m making some assumptions about what the OP wants, and I’d love suggestions if there is an easier way to do this, but this is what I came up with:

    demo.py

    import string
    from ctypes import Structure,c_int,c_char_p,POINTER,cast,pointer,byref,CDLL
    
    class Row(Structure):
        _fields_ = [('cols_count', c_int), 
                    ('cols', POINTER(c_char_p))]
        def __init__(self,cols):
            self.cols_count = cols
            # Allocate an array of character pointers
            pc = (c_char_p * cols)()
            self.cols = cast(pc,POINTER(c_char_p))            
    
    class Unit(Structure):
        _fields_ = [('rows_count', c_int),
                    ('rows',POINTER(Row))]
        def __init__(self,rows,cols):
            self.rows_count = rows
            # Allocate an array of Row structures.
            # This does NOT call __init__.
            pr = (Row * rows)()
            # Call init manually with the column size.
            for r in pr:
                r.__init__(cols)
            self.rows = cast(pr,POINTER(Row))
    
    unit = Unit(2,3)
    
    # Stuff some strings ('aaaaa','bbbbb',etc.)
    for i in xrange(unit.rows_count):
        for j in xrange(unit.rows[i].cols_count):
            unit.rows[i].cols[j] = string.ascii_lowercase[i*5+j]*5
    
    dll = CDLL('test.dll')
    dll.my_func(byref(unit))
    

    test.c

    #include <stdio.h>
    
    struct _rows {
        int cols_count;
        char **cols;
    };
    
    struct _unit {
        int rows_count;
        struct _rows *rows;
    };
    
    __declspec(dllexport) int my_func(struct _unit *param)
    {
        int i,j;
        for(i=0;i<param->rows_count;i++)
            for(j=0;j<param->rows[i].cols_count;j++)
                printf("%d,%d = %s\n",i,j,param->rows[i].cols[j]);
        return 0;
    }
    

    makefile

    Compiled with Visual Studio 2010.

    test.dll: test.c
        cl /W4 /LD test.c
    

    Output

    0,0 = aaaaa
    0,1 = bbbbb
    0,2 = ccccc
    1,0 = fffff
    1,1 = ggggg
    1,2 = hhhhh
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to implement this scenario using Unity and i can't figure out how
I am trying to implement an application by using MySQL/JAVA and I found this
This is a problem I hit when trying to implement a game using the
I'm trying to implement this pattern in my WinForms application (I don't like it,
I am trying to implement this demo on my local machine. http://bit.ly/4g4o1r I have
I'm trying to implement something like this: <div> <table> <thead> <tr> <td>Port name</td> <td>Current
I'm trying to implement the shake tutorial on this page, but I think I'm
I'm trying to implement the answer to this SO question . The problem is:
I'm trying to implement the Media Player custom field control described in this MSDN
I'm trying to implement routing such as the following: posts/535434/This-is-a-post-title posts/tagged/tags+here // Matches {controller}/{action}/{id}

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.