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 6342235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:12:09+00:00 2026-05-24T20:12:09+00:00

I am trying to access elements of a structure from ctypes. The structure is

  • 0

I am trying to access elements of a structure from ctypes. The structure is created in an init function in the C code and pointer to it is returned to Python. The problem I am having is that I get a segfault when trying to access elements of the returned structure. Here is the code:

The C code (which I’ve called ctypes_struct_test.c):

#include <stdio.h>
#include <stdbool.h>

typedef struct {
    bool flag;
} simple_structure;

simple_structure * init()
{
    static simple_structure test_struct = {.flag = true};
    if (test_struct.flag) {
        printf("flag is set in C\n");
    }
    return &test_struct;
}

The Python code (which I’ve called ctypes_struct_test.py):

#!/usr/bin/env python

import ctypes
import os

class SimpleStructure(ctypes.Structure):
    _fields_ = [('flag', ctypes.c_bool)]

class CtypesWrapperClass(object):

    def __init__(self):

        cwd = os.path.dirname(os.path.abspath(__file__))
        library_file = os.path.join(cwd,'libctypes_struct_test.so')

        self._c_ctypes_test = ctypes.CDLL(library_file)

        self._c_ctypes_test.init.restypes = ctypes.POINTER(SimpleStructure)
        self._c_ctypes_test.init.argtypes = []

        self.simple_structure = ctypes.cast(\
                self._c_ctypes_test.init(),\
                ctypes.POINTER(SimpleStructure))


a = CtypesWrapperClass()
print 'Python initialised fine'
print a.simple_structure.contents
print a.simple_structure.contents.flag

The C is compiled under Linux as follows:

gcc -o ctypes_struct_test.os -c --std=c99 -fPIC ctypes_struct_test.c
gcc -o libctypes_struct_test.so -shared ctypes_struct_test.os

On running python ctypes_struct_test.py, I get the following output:

flag is set in C
Python initialised fine
<__main__.SimpleStructure object at 0x166c680>
Segmentation fault

Is there some problem with what I am trying to do, or the way I am trying to do it?

  • 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-24T20:12:10+00:00Added an answer on May 24, 2026 at 8:12 pm

    To make this work, replace the wrong line

    self._c_ctypes_test.init.restypes = ctypes.POINTER(SimpleStructure)
    

    by the correct line

    self._c_ctypes_test.init.restype = ctypes.POINTER(SimpleStructure)
    

    Also consider to remove the pointless cast

    self.simple_structure = ctypes.cast(
        self._c_ctypes_test.init(), ctypes.POINTER(SimpleStructure))
    

    It’s casting from ctypes.POINTER(SimpleStructure) to ctypes.POINTER(SimpleStructure)!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to access the width of certain elements by using jQuery's .width() function,
I am trying to access HTML elements by their id from an array of
I'm trying to access a drop down box from a page created by SQL
Here is my code structure, that I saw from someone else and am trying
Hello all having some trouble accessing elements from a returned JSON from Twitter. I'm
I'm trying to get access to a form and its elements. The form is
Im trying to access a web service from a remote computer. I managed to
I am trying to access Outlook 2007 from C#. I have installed the PIA
I am trying to access a view inside a splitter from my mainframe. At
I have some xml data and I am trying to access some elements. The

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.