473,320 Members | 2,054 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

how to use an array of struct in java using SWIG

21
Hello All,

I have a function in C++ that takes an array of struct. I want to convert this function to java using SWIG.

Here what I have

structs.h

Expand|Select|Wrap|Line Numbers
  1. typedef struct myStruct{
  2.    char name;
  3.    int type;
  4.  
  5. void Foo( myStruct * mystr){
  6.   //do something
  7. }
In C++ driver (and this works fine)

Expand|Select|Wrap|Line Numbers
  1. #include "structs.h"
  2. myStruct structTest[5];  //an array
  3. structTest[0].name="me";
  4. structTest[0].type='1';
...
SWIG ...
I included this header in my interface file and it generates all the classes.

.... Here is what I did in my Java driver

Expand|Select|Wrap|Line Numbers
  1. myStruct jtest=new myStruct();
  2. jtest.setName("me");
  3. jtest.setType("1");
  4. Foo(jtest);
This worked fine however, I can only do one item at a time. Is there away I create an array of myStruct like I did in C++? I tried but got some errors. Here is one thing I tried:

Expand|Select|Wrap|Line Numbers
  1. myStruct jtest[] = new myStruct();
Can any one help? I will appreciate it.

Regards,
Jazi
Oct 22 '07 #1
4 7889
Laharl
849 Expert 512MB
I'm not familiar with SWIG, but Java array syntax is

Expand|Select|Wrap|Line Numbers
  1. MyStruct[] array = new MyStruct[5];
  2.  
Oct 22 '07 #2
Jazi
21
I'm not familiar with SWIG, but Java array syntax is

Expand|Select|Wrap|Line Numbers
  1. MyStruct[] array = new MyStruct[5];
  2.  
Thanks Kaharl,
Here is what I did

myStruct[] jtest=new myStruct[5];
Foo(jtest);

The syntax seems to be correct but it does not seem to like it

mydriver.java:30: Foo(jtest) in structs cannot be applied to (jtest[])
Foo(jtest);
^
1 error

The argument of this function should be a pointer and now it does not like it because it is an array. Any other explanations?

Thanks
Oct 23 '07 #3
Ganon11
3,652 Expert 2GB
The way SWIG translated your Foo function, it expects a single myStruct variable. Maybe you can, in your C++ code, change the function header to expect a myStruct mystr[] instead of a myStruct * mystr.
Oct 23 '07 #4
Jazi
21
The way SWIG translated your Foo function, it expects a single myStruct variable. Maybe you can, in your C++ code, change the function header to expect a myStruct mystr[] instead of a myStruct * mystr.
I did that but it does not seem to make any difference. I keep getting the same results and therefore the same error. Somehow I need to tell it this is a java array and it has to be done at the interface file level but I am not familiar with it.

Thanks for your tip.
Oct 23 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: Carl Waldbieser | last post by:
I tried to adapt the instructions for building the M2Crypto module (http://sandbox.rulemaker.net/ngps/m2/INSTALL.html) to build a version compatible with Python2.3, but I've had some mixed results....
14
by: Joachim Boomberschloss | last post by:
Hello, I am working on a project in Python, and I"m currently looking into the possibiliy of writing some of the project"s modules in Java. Given that a large part of the code is already...
2
by: SunMan | last post by:
Hello! I am trying to create a program that will ask for a user to enter a series of letters (codes) and then print out a table that shows the codes in decending frequency. Only letters will be...
7
by: Jamil Anwar Zaman | last post by:
If I dynamically allocate memory to a pointer, then is it possible to see afterwards what is the memory size the pointer is looking at. e.g int SIZE = 10; int *a = malloc(SIZE*sizeof(int));...
22
by: Wynand Winterbach | last post by:
I think every C programmer can relate to the frustrations that malloc allocated arrays bring. In particular, I've always found the fact that the size of an array must be stored separately to be a...
5
by: Krish | last post by:
Hello People I hope I am On Topic. Anyways, here is my problem. Any insights would be really appreciated. I have wrapped a C IO module using SWIG -> Python Module. Suppose the name of the...
3
by: Sudheer Gupta | last post by:
Hi, I am having trouble using C struct in python. Hope anyone can help me out ... Say, I have my C struct as typedef struct call { struct call *next;
0
by: Jazi | last post by:
I would appreciate if someone can help me dealing with wrapping java around c++ using SWIG. I have wrapped simple classes without any problem but I had some difficulties getting a vector function...
5
by: emmanuel.rivoire | last post by:
Hello, I spent almost a week to be able to embed Python within my C++ game engine. I wrote a mini-tutorial of what I was able to do so far here :...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.