Question

Java code:

public static final byte cw_bArray = 0x01;
public static final byte cw_string = 0x02;

byte[] arr1=new byte[]{0x01};
byte[] arr2=new byte[]{0x02};
byte[] arr3=new byte[]{0x03};
byte[] code_ward_arr=new byte[]{cw_bArray,cw_string,cw_bArray};

method:
mount(null,new Object[]{arr1,arr2,arr3}, code_ward_arr); 

I am trying to convert this code into symbian c/c++. I have done with this but i dont know how to pass argument anybody help me with this.

static const signed char cw_bArray = 0x01;
static const signed char cw_string = 0x02;

signed char a1 = {0x01};
signed char a2 = {0x02}; 
signed char a3 = {0x03}; 

signed char code_ward_arr[]  = {cw_bArray,cw_bArray,cw_bArray};
//signed char bbb[]  = {a1,a2,a3};  
   char *bb=new char[2];
    bb[0] = a1;
    bb[1] = a2;
    bb[2] = a3;

mount(NULL, bb[] , code_ward_arr);

method:

LOCAL_C void mount(signed char initData, char* obj[], signed char codeWord)
    {

    }

i am trying to convert this part of code to c java code:

Object obj[] = null;
if(codeWord==null){
            obj = new Object[10];//Initial Loading Capacity
        }else{
            obj = new Object[codeWord.length];
        }

C code:

char* obj[] = {NULL};

 if(codeWord==NULL)
 {
 obj = new char[10];
 } 
 else 
 {
 obj = new char[Cwlen];
 }

i'll get this error illegal operands 'char *[1]' = 'char *'

Was it helpful?

Solution

This should work

char a1[]={0x01};
char a2[]={0x01};
char a3[]={0x01};

char *  bb [3];
bb[0] = a1;
bb[1] = a2;
bb[2] = a3;


mount(NULL, bb , code_ward_arr);

OTHER TIPS

The lines declaring a1 to a3 are wrong:

signed char a1 = {0x01};

This line declares a single character (byte) and then assign an array to it. Declare the variables as either arrays or as pointers:

// As array (my recommendation)
signed char a1[] = { 0x01 };

// As pointer
signed char *a1 = { 0x01 };

You also have to change the declaration of bb:

char *bb[] = { a1, a2, a3 };

I C++ arrays decay to pointers, so a pointer can be assigned to an array, but not the other way around.

Then you call mount without the brackets:

mount(NULL, bb, code_ward_arr);

Your biggest problem here is mount is going to have no idea of the size of the arrays it is being passed. This isn't a problem for Java as its arrays contain additional meta data that C/C++ arrays do not (length being the useful one here).

There is a slight exception to this. C/C++ arrays (not pointers) have their size available at compile time, but only within the scope they were declared. One an array is passed as a pointer to another method the size of the array is lost.

To get around this problem you need to provide that additional length data. Perhaps the simplest, but most bulky solution is to place the data in a vector. This might be too heavy weight for you, especially if you are converting to C/C++ for efficiency reasons.

example:

vector<signed char> a1(1, 0x01); // create vector of size 1 with all elements 0x01
vector<signed char> a2(1, 0x02); 
vector<signed char> a3(1, 0x03); 

vector<signed char> code_ward_arr(3, cw_bArray);

vector<vector<signed char> > bb(3);
bb[0] = a1;
bb[1] = a2;
bb[2] = a3;

mount(NULL, bb , code_ward_arr);

method:

LOCAL_C void mount(signed char initData, vector<vector<signed char> >& obj,
        vector<signed char>& codeWord);

The lightweight solution is to either create a struct that holds a pointer to the data and a length of that data, or to simply add additional parameters to mount to hold the length of the arrays. This gets complicated and messy when you have arrays of arrays, so let's stick with the struct solution

eg.

template<typename T>
typedef struct array {
    size_t length;
    T* data;
} array;

LOCAL_C void mount(signed char initData, array<array<signed char> >& obj,
        array<signed char>& codeWord);

array<signed char> a1;
signed char a1_data[] = {0x01};
a1.length = sizeof(a1_data) / sizeof(signed char);
a1.data = a1_data;

...

array<signed char> bb_data[] = {a1, a2, a3};
array<array<signed char> > bb;
bb.length = sizeof(bb_data) / sizeof(array<signed char>);
bb.data = bb_data;

mount(NULL, bb, code_ward_arr);

You need to be careful though as the pointers inside the array structs are only valid for the duration of the method in which they were created (unless the actual arrays ([]) are global or created on the heap).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top