Jump to content



Photo
- - - - -

[casio C] Array And Functions


  • Please log in to reply
4 replies to this topic

#1 Horrowind

Horrowind

    Newbie

  • Members
  • Pip
  • 29 posts
  • Gender:Male

  • Calculators:
    9860G SD
    9850GB+

Posted 21 June 2007 - 04:44 PM

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 :unsure: . could someone help me?

#2 LordNPS

LordNPS

    Casio Freak

  • Members
  • PipPipPipPip
  • 173 posts
  • Gender:Male
  • Location:Porto/Portugal (makes me sad having to write this but....) /Europe
  • Interests:CG<br />Programming<br />Chess<br />Weapons<br />Porn<br />Explosives and rocketry<br />Calculators<br />History( mainly WW2 history)<br />

  • Calculators:
    FX 9860 SD


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...

#3 Andreas B

Andreas B

    Casio Freak

  • Members
  • PipPipPipPip
  • 160 posts
  • Gender:Male
  • Location:Norway

  • Calculators:
    fx-9860G SD

Posted 21 June 2007 - 06:58 PM

When you create an array, the variable you use to acces the array with, is a pointer.

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 Horrowind

Horrowind

    Newbie

  • Members
  • Pip
  • 29 posts
  • Gender:Male

  • Calculators:
    9860G SD
    9850GB+

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:
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 Andreas B

Andreas B

    Casio Freak

  • Members
  • PipPipPipPip
  • 160 posts
  • Gender:Male
  • Location:Norway

  • Calculators:
    fx-9860G SD

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