Solved

Creating a C Static Char Array

Posted on 2012-03-19
3
556 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

Make managing Office 365 email signatures a breeze

Are you using Office 365? Having trouble trying to set up email signatures for your users? Getting stressed out managing multiple signatures? Need an easier way to manage? We have a solution for you, try the most-user friendly and powerful signature management tool on the market.

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
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.

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now