Solved

Creating a C Static Char Array

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

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…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.

746 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