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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:10:18+00:00 2026-05-31T18:10:18+00:00

I have the below configuration where I’m trying to create a test C function

  • 0

I have the below configuration where I’m trying to create a test C function that returns a pointer to an Array of Strings and then wrap that using SWIG’s carrays.i and array_functions so that I can access the Array elements in Java. I’m unsure which of %array_class or %array_functions is most appropriate for this situation. This example is a building block towards wrapping a C function that returns a dynamically created array.

Uncertainties:

  • %array_functions(char, SWIGArrayUtility); – not sure if char is correct
  • inline char *getCharArray() – not sure if C function signature is correct
  • String result = getCharArray(); – String return seems odd, but that’s what is generated by SWIG
  • unsure if inline char *getCharArray() creates an array that is of proper structure for wrapping.

SWIG.i:

%module Test

%{
#include "test.h"
%}

%include <carrays.i>
%array_functions(char, SWIGArrayUtility);
%include "test.h"

%pragma(java) modulecode=%{
  public static char[] getCharArrayImpl() {
    final int num = numFoo();
    char ret[] = new char[num];
    String result = getCharArray();
    for (int i = 0; i < num; ++i) {
      ret[i] = SWIGArrayUtility_getitem(result, i);
    }
    return ret;
  } 

%}

Inline Header C Function:

#ifndef TEST_H
#define TEST_H

inline static unsigned short numFoo() {
  return 3;
}

inline char *getCharArray(){
    static char* foo[3];
    foo[0]="ABC";
    foo[1]="5CDE";
    foo[2]="EEE6";
    return foo;
}

#endif

Java Main Tester:

public class TestMain {
    public static void main(String[] args) {
        System.loadLibrary("TestJni");
        char[] test = Test.getCharArrayImpl();
        System.out.println("length=" + test.length);
        for(int i=0; i < test.length; i++){
            System.out.println(test[i]);
        }
    }

}

Java Main Tester Output:

length=3
?
?
,

SWIG Generated Java APIs:

public class Test {
  public static String new_SWIGArrayUtility(int nelements) {
    return TestJNI.new_SWIGArrayUtility(nelements);
  }

  public static void delete_SWIGArrayUtility(String ary) {
    TestJNI.delete_SWIGArrayUtility(ary);
  }

  public static char SWIGArrayUtility_getitem(String ary, int index) {
    return TestJNI.SWIGArrayUtility_getitem(ary, index);
  }

  public static void SWIGArrayUtility_setitem(String ary, int index, char value) {
    TestJNI.SWIGArrayUtility_setitem(ary, index, value);
  }

  public static int numFoo() {
    return TestJNI.numFoo();
  }

  public static String getCharArray() {
    return TestJNI.getCharArray();
  }


  public static char[] getCharArrayImpl() {
    final int num = numFoo();
    char ret[] = new char[num];
    String result = getCharArray();
    System.out.println("result=" + result);
    for (int i = 0; i < num; ++i) {
      ret[i] = SWIGArrayUtility_getitem(result, i);
      System.out.println("ret[" + i + "]=" + ret[i]);
    }
    return ret;
  } 


}
  • 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-31T18:10:20+00:00Added an answer on May 31, 2026 at 6:10 pm

    Two changes are necessary:

    • char * getCharArray() must be char ** getCharArray(), because the function returns an array (C pointer) of pointers to char *. After this change a new SWIGTYPE_p_p_char Java class appears, and to get it an %include "various.i" must be added into the interface file.

    • %array_functions(char, SWIGArrayUtility) must be %array_functions(char *, SWIGArrayUtility), because the array contains pointers to char * (String Java class).

    I have tested the given solution using this include file:

    #ifndef TEST2_H
    #define TEST2_H
    
    unsigned short numFoo() {
      return 3;
    }
    
    char ** getCharArray(){
        static char* foo[3];
        foo[0]="ABC";
        foo[1]="5CDE";
        foo[2]="EEE6";
        return foo;
    }
    
    #endif
    

    This interface file:

    %module Test2
    
    %{
    #include "test2.h"
    %}
    %include "test2.h"
    
    %include "various.i"
    
    %include "carrays.i"
    %array_functions(char *, SWIGArrayUtility);
    
    %pragma(java) modulecode=%{
      public static String[] getCharArrayImpl() {
        final int num = numFoo();
        String ret[] = new String[num];
        SWIGTYPE_p_p_char result = getCharArray();
        for (int i = 0; i < num; ++i) {
            ret[i] = SWIGArrayUtility_getitem(result, i);
        }
        return ret;
      } 
    %}
    

    And this tester class:

    public static void main(String[] args) {
        System.loadLibrary("test2");
        String res[] = Test2.getCharArrayImpl();
        System.out.println("length=" + res.length);
        for(int i=0; i < res.length; i++){
            System.out.println(res[i]);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am told that the configuration below is possible, but have had significant difficulty
I have a configuration page that needs to create a virtual directory under a
We have a configuration table as shown below that stores the start time and
I have below query I am trying to show message 'No SubSource for this
I have below a working query that needs to be simplified. The reason is
I have use below below configuration without https. Now url changed to https .
I have a web-app with the configuration below (inherited from a parent's pluginManagement). JSPC
I have a WCF SOAP 1.1 Webservice with the configuration specified below. A concurrent
I have written a Perl script to read the configuration file and create CGI
In .NET I have already tried the configuration below successfully: <system.net> <connectionManagement> <add address=*

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.