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

  • Home
  • SEARCH
  • 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 6876957
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:32:32+00:00 2026-05-27T04:32:32+00:00

Id like to make a simple function, that return value of two Strings, basically:

  • 0

Id like to make a simple function, that return value of two Strings, basically:

java

public native String getAppendedString(String name);

c

jstring Java_com_example_hellojni_HelloJni_getAppendedString(JNIEnv* env,   jobject thiz, jstring s) {
    jstring sx = (*env)->GetStringUTFChars(env, s, NULL);

    return ((*env)->NewStringUTF(env, "asd ")+sx);
}

It returns:

jni/hello-jni.c:32: warning: initialization discards qualifiers from pointer target type
jni/hello-jni.c:34: error: invalid operands to binary + (have 'char *' and 'char *')

The retval will be: "asd qwer", how can I do this?

jstring s1  = (*env)->NewStringUTF(env, "456");
jstring s2  = (*env)->NewStringUTF(env, "123");
jstring sall=strcat(s1, s2);

return sall;

Only returns "456"

  • 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-27T04:32:33+00:00Added an answer on May 27, 2026 at 4:32 am

    There are a few issues here:

    1. GetStringUTFChars returns a jbyte * (a null-terminated C string), not a jstring. You need this C string to do string manipulation in C.

    2. You need to call ReleaseStringUTFChars when you’re done with it.

    3. You need to allocate enough memory to hold the concatenated string, using malloc.

    4. As ethan mentioned, you need to concatenate your two C strings with strcat. (You cannot do this with the + operator. When applied to a pointer, + returns the pointer from the offset of the original pointer.)

    5. Remember to free the memory you allocated after you’re done with it (ie, after it’s been interned as a Java string.)

    You should do something along the lines of:

    char *concatenated;
    const jbyte *sx;
    jstring retval;
    
    /* Get the UTF-8 characters that represent our java string */
    sx = (*env)->GetStringUTFChars(env, s, NULL);
    
    /* Concatenate the two strings. */
    concatenated = malloc(strlen("asd ") + strlen(sx) + 1);
    strcpy(concatenated, "asd ");
    strcat(concatenated, sx);
    
    /* Create java string from our concatenated C string */
    retval = (*env)->NewStringUTF(env, concatenated);
    
    /* Free the memory in sx */
    (*env)->ReleaseStringUTFChars(env, s, sx);
    
    /* Free the memory in concatenated */
    free(concatenated);
    
    return retval;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to make a simple web app that displays 20 or so
Simple task like make AJAX request , pass one parameter and return result, can
Would like to make anapplication in Java that will not automatically parse parameters used
In simple words, I want to implement a function like that: let myfunction (scalar
I'd like to make a simple html form where a person can upvote or
I would like to make a simple desktop application to learn this kind of
I am trying to make a simple redirector controller in CakePHP. I'd like the
On a simple directory creation operation for example, I can make an OSError like
Today, without much thought, I wrote a simple function return to a char* based
Imagine such situation that I have a function like this: Object f() { Object

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.