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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:07:29+00:00 2026-05-18T02:07:29+00:00

I have a method in C++ that takes an array of doubles as an

  • 0

I have a method in C++ that takes an array of doubles as an argument. I’m calling this method from Java and need to pass an array of doubles. The C++ routine reads and modifies the values of the array and I need those updated values in Java. How do I do this?

For example, take the C++ routine:

void myMethod( double *values, int size ) {
    for ( int i=0; i < size; i++ ) {
        values[i] = 2*values[i];
    }
}

And the Java code:

double[] values = { 1.3, 1.1 };
myMethod(values,values.length);
System.out.println(values[0]); // prints 2.6

I guess a call to myMethod cannot be made like the call above… or can it? And what is necessary in Swig to make this work. If I cannot make a call like the one above, how do I get my values to the C++ code?

  • 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-18T02:07:30+00:00Added an answer on May 18, 2026 at 2:07 am

    Use carrays.i!

    See Swig docs on carrays

    %include carrays.i
    %array_functions( double, double_array )
    

    These two lines create the following code in my module:

     public static SWIGTYPE_p_double new_double_array(int nelements) {
        long cPtr = SimulatorModuleJNI.new_double_array(nelements);
        return (cPtr == 0) ? null : new SWIGTYPE_p_double(cPtr, false);
      }
    
      public static void delete_double_array(SWIGTYPE_p_double ary) {
        SimulatorModuleJNI.delete_double_array(SWIGTYPE_p_double.getCPtr(ary));
      }
    
      public static double double_array_getitem(SWIGTYPE_p_double ary, int index) {
        return SimulatorModuleJNI.double_array_getitem(SWIGTYPE_p_double.getCPtr(ary), index);
      }
    
      public static void double_array_setitem(SWIGTYPE_p_double ary, int index, double value) {
        SimulatorModuleJNI.double_array_setitem(SWIGTYPE_p_double.getCPtr(ary), index, value);
      }
    

    Which allows me to use C arrays in Java… this solves my needs and is the best solution for my problem. Thanks everyone for your answers!

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

Sidebar

Related Questions

I have a method that takes an array of queries, and I need to
I have a method that takes a condition and item, from an array of
I have an init method that takes values from a NSDictionary . This is
I have a method (C++) that returns a character and takes an array of
All, I have a method that takes a date (YYYY-MM-DD H:M:S) from the database
I'm wondering how to go about testing this. I have a method that takes
I have a method that takes data from a .csv file and puts it
I have an method that takes in an observable collection (returned from a webservice)
Let's say I have a method m() that takes an array of Strings as
I have a method that takes in a string array and finds the averages

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.