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

  • Home
  • SEARCH
  • 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 3317848
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T22:38:07+00:00 2026-05-17T22:38:07+00:00

I’m trying to pass an array of character arrays to a C function using

  • 0

I’m trying to pass an array of character arrays to a C function using ctypes.

void cfunction(char ** strings)
{
 strings[1] = "bad"; //works not what I need.
 strings[1][2] = 'd'; //this will segfault.
 return;
}

char *input[] = {"foo","bar"};
cfunction(input);

Since the array that I throw around is statically defined anyways,
I just changed the function declaration and input parameter like so:

void cfunction(char strings[2][4])
{
 //strings[1] = "bad"; //not what I need.
 strings[1][2] = 'd'; //what I need and now it works.
 return;
}

char input[2][4] = {"foo","bar"};
cfunction(input);

Now I run into the problem of how to define this multi-dimensional character
array in python. I had thought it would go like so:

import os
from ctypes import *
libhello = cdll.LoadLibrary(os.getcwd() + '/libhello.so')
input = (c_char_p * 2)()
input[0] = create_string_buffer("foo")
input[1] = create_string_buffer("bar")
libhello.cfunction(input)

This gives me TypeError: incompatible types, c_char_Array_4 instance instead of c_char_p instance. If I change it to:

for i in input:
 i = create_string_buffer("foo")

Then I get segmentation faults. Also this looks like the wrong way to build the 2d array because if I print input I see None:

print input[0]
print input[1]

# outputs None, None instead of "foo" and "foo"

I also run into the issue of using #DEFINE MY_ARRAY_X 2 and #DEFINE MY_ARRAY_Y 4 to keep the array dimensions straight in my C files, but don’t know a good way to get these constants out of the libhello.so so that python can reference them when it constructs the datatypes.

  • 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-17T22:38:08+00:00Added an answer on May 17, 2026 at 10:38 pm

    Use something like

    input = ((c_char * 4) * 2)()
    input[0].value = "str"
    input[0][0] == "s"
    input[0][1] == "t" # and so on...
    

    Simple usage:

    >>> a =((c_char * 4) * 2)()
    >>> a
    <__main__.c_char_Array_4_Array_2 object at 0x9348d1c>
    >>> a[0]
    <__main__.c_char_Array_4 object at 0x9348c8c>
    >>> a[0].raw
    '\x00\x00\x00\x00'
    >>> a[0].value
    ''
    >>> a[0].value = "str"
    >>> a[0]
    <__main__.c_char_Array_4 object at 0x9348c8c>
    >>> a[0].value
    'str'
    >>> a[0].raw
    'str\x00'
    >>> a[1].value
    ''
    >>> a[0][0]
    's'
    >>> a[0][0] = 'x'
    >>> a[0].value
    'xtr'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I need a function that will clean a strings' special characters. I do NOT
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am reading a book about Javascript and jQuery and using one of the
I am trying to render a haml file in a javascript response like so:

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.