Solved

Creating a C Static Char Array

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

Technology Partners: 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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files 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.

685 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