Solved

Creating a C Static Char Array

Posted on 2012-03-19
3
583 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

734 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