Hello All

I am Posting after a very Long time because of Having a problem :)

The Problem is I want to Convert a String on UNKNOWN size to character array.

Explanation:

I have a file in JAVA that sends some string through JNI, I collect that String in C.

const char *str = (*env)->GetStringUTFChars(env, data, 0);

(Above: the Portion of Code where I do Collect String from JAVA)

The str is a Constant String, I want to Convert it to character array.

Thanx in Advance

There are no strings in C. There are character arrays. The str in your above example is an array and can be used like one ( str[4] for instance). If you'd like to convert that to a non-const array you will need to create storage and copy the contents. For example:

size_t len = strlen (str) + 1;
char * ncstr = malloc (len);
strcpy (ncstr, str);

The str is a Constant String, I want to Convert it to character array.

To what end? What does a character array have that the string doesn't? As L7Sqr stated, there's no string type in C. Strings are represented by "arrays" of char, where the last character is '\0' and "array" could be either an actual array, or a simulated array.

I'm assuming you want to create a writable copy of this string, since I can't really think of any other reason to do this conversion that doesn't stem from a misunderstanding of how strings and arrays work in C. In that case, it's really just a matter of using dynamic allocation:

const char *str = (*env)->GetStringUTFChars(env, data, 0);
char *copy = malloc(strlen(str) + 1); /* +1 for the terminating '\0' */

if (copy == NULL) {
    /* Handle failure to allocate memory */
}

strcpy(copy, str);

The issue being not one of arrays vs. strings, but one of constness. The string coming from Java may not be in writable memory, so a mutable copy needs to be made.

The str in your above example is an array

It's a pointer, not an array. The pointer might refer to the 0th element of an array, but pointers and arrays are different enough that it's wise not to confuse them.

and can be used like one ( str[4] for instance)

Yes, though not in all cases. sizeof(str) won't work like it would with an array, nor would &str .

If you'd like to convert that to a non-const array you will need to create storage and copy the contents.

That's not an array either, it's a simulated array using dynamic allocation.

Here is a Stupid Method how I did it :)

I first Copied my str to a file, then Read that file and Copied everything to a character Array, Very Lengthy, stupid but it worked. The Logic Code is Below:

const char *str = (*env)->GetStringUTFChars(env, data, 0);

	FILE *str_Store = fopen("tmp.txt", "w");
	fputs(str, str_Store);						
	
//	unsigned long ch_in_file;
	
	int ch_in_file = count_char();  // I have wrote count_char, it returns the Number of Characters in a file
		
	char inbuf[ch_in_file];
	
	for (i=0; i<ch_in_file; i++)
		inbuf[i] =fgetc(str_Store);
	
	printf("\nChecking INBUF \n");
	puts(inbuf);
	
	rewind(str_Store);
	fclose(str_Store);

Thanx everyone :)

btw I dont know whether its Legal to say this here, Why am I not getting the Posts on my thread in my Email inbox? I should be getting them to be aware.

stupid but it worked

Yep, that pretty much covers it. It's a viable solution, but not very practical due to the overhead of file management. Might I suggest also playing around with the suggested solutions in this thread? Even if you continue to use your solution, you'll at least learn different ways of doing things and their trade-offs.

playing around with the suggested solutions in this thread

Its getting very Confusing, Switching to JAVA then to C, then Back to Java Again but I will play around.

overhead of file management

May be I can also make use of generated File, May be it will serve as a LOG and can use it for Debugging and other lots of purpose in Future :)

May be I can also make use of generated File, May be it will serve as a LOG and can use it for Debugging and other lots of purpose in Future

Maybe you're reaching for some way to rationalize a horrible solution to the problem at hand. :icon_rolleyes:

Maybe you're reaching for some way to rationalize a horrible solution to the problem at hand

HAHAHAHA May be I did :)

Might I suggest also playing around with the suggested solutions in this thread?

OK I will go with this Solution, No overhead of File management and overall a clean code.

#include <stdio.h>
#include <string.h>

int main(void)
{
    const char *str = (*env)->GetStringUTFChars(env, data, 0);
    int length = strlen(str);
    char str2[length];
    strcpy(str2,str);
    printf("\nValue: %s\n", str);
    printf("\nValue: %s\n", str2);
    return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.