If you can't assign const char array, what is the other way?

I'm developing an app for Android with JNI and I'm making a array list to display them in multiple spinners (dropdown) with multiple arrays

The thing is you can't assign it, so i tried other way but it could only list the first item. What did i do wrong?

Google search did not help much. It only suggested me using strcpy in char array which is not i'm looking for.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
const char *weaponsArray[] = {
        OBFUSCATE("AK 47"),
        OBFUSCATE("Assault Rifle"),
        OBFUSCATE("M16"),
        OBFUSCATE("Shotgun"),
};

const char *itemArray[] = {
        OBFUSCATE("Item 1"),
        OBFUSCATE("Item 2"),
        OBFUSCATE("Item 3"),
};

JNIEXPORT jobjectArray JNICALL
Java_com_FloatingService_getLists(JNIEnv *env, jobject activityObject, jstring featureName) {
    jobjectArray ret;
    const char **list= NULL;

    const char *feature = env->GetStringUTFChars(featureName, 0);

    if (feature == "The spinner"){
        list = weaponsArray;
    }
    if (feature == "The spinner 2"){
        list = itemArray;
    }

    int Total_Feature = (sizeof list /
                         sizeof list[0]);
    ret = (jobjectArray)
            env->NewObjectArray(Total_Feature, env->FindClass(OBFUSCATE("java/lang/String")),
                                env->NewStringUTF(""));
    int i;
    for (i = 0; i < Total_Feature; i++)
    {
        env->SetObjectArrayElement(ret, i, env->NewStringUTF(list[i]));
        LOGD("list");
    }

    return (ret);
}


If i initialize the array like this, it works fine and list all

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
JNIEXPORT jobjectArray JNICALL
Java_com_FloatingService_getLists(JNIEnv *env, jobject activityObject, jstring featureName) {
    jobjectArray ret;

    const char *list[] = {
        OBFUSCATE("Item 1"),
        OBFUSCATE("Item 2"),
        OBFUSCATE("Item 3"),
    };

    int Total_Feature = (sizeof list/
                         sizeof list[0]);
    ret = (jobjectArray)
    env->NewObjectArray(Total_Feature, env->FindClass(OBFUSCATE("java/lang/String")),
                        env->NewStringUTF(""));
    int i;
    for (i = 0; i < Total_Feature; i++)
        env->SetObjectArrayElement(ret, i, env->NewStringUTF(list[i]));

return (ret);
}

Last edited on
you should use c++, which is the string type, not char arrays, which is from C.
but if you must, you can assign them like this.

foo ("Item 1"s.data()) ;

you can't compare char arrays. (or ANY C-style arrays).
use strcmp() not ==, line 24 for example is flat out wrong.

you *CAN* use == on c++ strings.
Last edited on
I don't know JNI, but

1
2
3
if (feature == "The spinner") {
        list = weaponsArray;
    }


Using null-terminated char arrays (c-style strings) as opposed to C++ std::string, you can't compare like this. You need to use strcmp() eg:

1
2
3
if (strcmp(feature, "The spinner") == 0) {
        list = weaponsArray;
}

1
2
3
4
    const char **list= NULL;
    ...
    int Total_Feature = (sizeof list /
                         sizeof list[0]);

Total_Feature is guaranteed to be 1. Although you point list to an array of char*, list itself is still just a pointer, not an array. list[0] is a pointer also, so it's basically:
int Total_Feature = (sizeof pointer / sizeof pointer); '' always 1
Thanks guys, I ended up doing other way
And fixed that if-statement with strcmp
Topic archived. No new replies allowed.