Solved

Creating a C Static Char Array

Posted on 2012-03-19
3
567 Views
Last Modified: 2012-03-19
Hello,

I'm trying to create a static array in C for some JNI testing.  My goal is to have the charArray function return a pointer to a char array and then in my JNI code, I'll retrieve those results.  Whenever I have more than one character in the C char array, my JNI code is only able to get the last character from the array element (i.e. 'C', 'E' and '6' get returned for "doesn't work example below).

I feel I have a bug in the way I'm creating the array on the C side, does anyone have any ideas of where I've gone wrong?  

Works:
inline char *charArray(){
	static char foo[3];
	foo[0]='A';
    foo[1]='5';
    foo[2]='E';
    return foo;
}

Open in new window


Doesn't Work:
inline char *charArray(){
	static char foo[3];
	foo[0]='ABC';
    foo[1]='5CDE';
    foo[2]='EEE6';
    return foo;
}

Open in new window

0
Comment
Question by:cgray1223
  • 2
3 Comments
 
LVL 86

Expert Comment

by:jkr
ID: 37739613
I think you are mixing up a character or chars and a character of strings. If your final goal would be to concatenate strings to one longer string, you should use

inline char *charArray(){
	static char foo[64] = "\0";
	strcat(foo,"ABC");
	strcat(foo,"5CDE");
	strcat(foo,"EEE6");

    return foo;
}                     

Open in new window


The above would return "ABC5CDEEEE6".
0
 

Author Comment

by:cgray1223
ID: 37739784
thanks for the response, jkr.  The goal would be to treat them as separate elements on the Java side and not string the together.
0
 
LVL 86

Accepted Solution

by:
jkr earned 500 total points
ID: 37739806
Hmmm, then you are probably thinking of an array of strings, i.e.

inline char *stringArray(){
	static char* foo[3];
	foo[0]="ABC";
    foo[1]="5CDE";
    foo[2]="EEE6";
    return foo;
}
                                  
}
                                  

Open in new window

0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question