hello everybody,
i have a new question for my tests with SDK.
i want to make a function where i must return an array and where the parameter is an array, but i get many errors . could someone help me?
[casio C] Array And Functions
Started by
Horrowind
, Jun 21 2007 04:44 PM
4 replies to this topic
#1
Posted 21 June 2007 - 04:44 PM
#2
Posted 21 June 2007 - 06:28 PM
You mean return a "pointer" to an array receiving a "pointer" to an array in your function right?
I don't think you can return arrays in C ...
and if you get errors returning a pointer and you believe everything is right please post the code... I'll take a look at it...
I don't think you can return arrays in C ...
and if you get errors returning a pointer and you believe everything is right please post the code... I'll take a look at it...
#3
Posted 21 June 2007 - 06:58 PM
When you create an array, the variable you use to acces the array with, is a pointer.
You can instead try to define the array outside of the function, and not locally within the function. Another way to do it is to use dynamic memory allocation using malloc() & friends, but I have not tested this with the SDK C-compiler.
All cases are different anyway
Good luck.
void someFunction() { char someArray[256]; // allocate 256 char's, and one char* on the stack (local declaration) return; // remove the stack and return }This definition of someArray allocates 256 'chars' on the stack for the array, and also space for a pointer. The pointer is initialized to point to the 'first' element of the array data (index 0). The data on the stack is cleaned out when the function returns (explicit return here), so if you try to return the pointer to the data, it will not point to what you intended for the code that calls someFunction.
You can instead try to define the array outside of the function, and not locally within the function. Another way to do it is to use dynamic memory allocation using malloc() & friends, but I have not tested this with the SDK C-compiler.
void someFunction(char[]) { // do whatever on the array // (note that char* equals char[]) return; } void mainFunction { char someArray[256]; // allocate 256 char's, and one char* on the stack (local declaration) someFunction(someArray); // pass the array pointer to the function }
All cases are different anyway
Good luck.
#4
Posted 21 June 2007 - 09:58 PM
ok, the function works fine now (thx ), but how can i fill my array from the returned pointer
exampel:
exampel:
unsigned int length=10; unsigned int testarray[10]; unsigned int returnarray[10]; int setone(unsigned int ar[], unsigned int arlength) { int i; for ( i=0;i<arlength;i++) ar[i]=1; return *ar; } int AddIn_main(int isAppli, unsigned short OptionNum) { unsigned int key; &returnarray=setone(testarray,length); //<--ERROR GetKey(&key); return 0; } pragma section _BR_Size unsigned long BR_Size; #pragma section #pragma section _TOP int InitializeSystem(int isAppli, unsigned short OptionNum) { return INIT_ADDIN_APPLICATION(isAppli, OptionNum); } pragma section
#5
Posted 21 June 2007 - 10:26 PM
You should might want to read up on the relation between pointers and arrays.
For the operation you need to do, you don't need to arrays. If you pass?"testarray"?to?the?function?(it?is?a?pointer?to?an?array),?you?won't?have?to?return?anything?special,?as?the?function?is?able?to?change?the?array?(it?has?a?pointer?to?it).
Here is something that might work:
For the operation you need to do, you don't need to arrays. If you pass?"testarray"?to?the?function?(it?is?a?pointer?to?an?array),?you?won't?have?to?return?anything?special,?as?the?function?is?able?to?change?the?array?(it?has?a?pointer?to?it).
Here is something that might work:
unsigned int arrayLength = 10; unsigned int someArray[10]; int setone(unsigned int array[], unsigned int length) { // expect an array pointer ("someArray"), and length int i; for (i = 0; i < length; i++) { array[i]=1; ?// because array = someArray, this change is equal to "someArray[i] = 1" } } int AddIn_main(int isAppli, unsigned short OptionNum) { unsigned int key; setone(someArray,arrayLength); // the array pointer is passed, with the length GetKey(&key); return 0; } pragma section _BR_Size unsigned long BR_Size; #pragma section #pragma section _TOP int InitializeSystem(int isAppli, unsigned short OptionNum) { return INIT_ADDIN_APPLICATION(isAppli, OptionNum); } pragma section[/quote]
0 user(s) are reading this topic
0 members, 0 guests, 0 anonymous users