|
|
View previous topic :: View next topic |
Author |
Message |
Guest
|
Problem with returning a pointer in function |
Posted: Sun Aug 10, 2008 3:09 am |
|
|
Hi
Problem with returning a pointer
My func "test" must return the arrar "arr" to the caller...
Code: | int test1(int c){
int arr[3];
arr[0]=c+1;
arr[1]=c+2;
arr[2]=c+3;
return (arr);
}
void test1(){
int arr1[3];
arr1 = test1(100);
}
void main(){
test1();
} |
|
|
|
Guest
|
|
Posted: Sun Aug 10, 2008 3:41 am |
|
|
Try this...
int *test1(int c){
int arr[3];
arr[0]=c+1;
arr[1]=c+2;
arr[2]=c+3;
return (arr);
}
void test1(){
int *arr1;
arr1 = test1(100);
}
void main(){
test1();
} |
|
|
Ttelmah Guest
|
|
Posted: Sun Aug 10, 2008 3:58 am |
|
|
In general in C, you don't pass a physical 'array', but instead to just pass the _address_ of this. In your case (with just three entries), this doesn't make much difference, but if the array had hundreds of elements, it would be a huge time saving.
Start with your declarations. test1, _does not return an integer_. It returns the _address_ of an integer, and needs to be declared as such. "int *", not "int".
Then you need to make your array 'static'. Local variables (declared inside a function), only 'exist', while the function is running. After this,the memory can be re-used for other things. If you want to pass the address of some memory 'back' to the main, you need to ensure this memory is retained. This is what the 'static' keyword does.
You then don't want to declare a new array in the second function, but instead say that arr1, is the address of an array. Again "int *", not int.
So:
Code: |
int * test1(int c) {
static int arr[3];
arr[0]=c+1;
arr[1]=c+2;
arr[2]=c+3;
return (arr);
}
void test1(void) {
int * arr1;
arr1 = test1(100);
printf("%u",arr1[2]); //If you have printing setup, this will print '102'
}
void main(){
test1();
}
|
Best Wishes |
|
|
Ttelmah Guest
|
|
Posted: Sun Aug 10, 2008 3:59 am |
|
|
I see 'guest', pasted the same basic stuff (except the static - watch out for this....).
Best Wishes |
|
|
Guest
|
|
Posted: Sun Aug 10, 2008 5:05 am |
|
|
Thanks for reply Ttelmah. |
|
|
vibrasys
Joined: 20 Nov 2005 Posts: 16 Location: the Netherlands
|
Pointer to char [1024] array |
Posted: Thu Sep 18, 2008 8:01 am |
|
|
Anonymous wrote: | Thanks for reply Ttelmah. |
Ttelmah,
I am trying to display different arrays of 1024 chars using a function .
array def: const char sigla[1024] ={0xfc,0xfe,0xff,0xff,.......} //1024 chars
function def:
Code: |
void load_bmp(char vector[])
{
int8 x,y;
int16 ptr=0;
for(y=0; y<8; y++)
{
output_low(GLCD_DI);
glcd_writeByte(GLCD_CS2, 0xB8 + y); // Turn the display on
glcd_writeByte(GLCD_CS1, 0xB8 + y);
output_high(glcd_di);
for(x=0; x<64; x++)
{
glcd_writeByte(GLCD_CS1, vector[ptr++]);
}
for(x=0; x<64; x++)
{
glcd_writeByte(GLCD_CS2, vector[ptr++]);
}
}
output_low(glcd_di);
glcd_writeByte(GLCD_CS1, 0x3F); // Turn the display on
glcd_writeByte(GLCD_CS2, 0x3F);
output_high(glcd_di);
return;
}
|
I try to call this in main like this:
load_bmp(&sigla);
Any idea why this doesn't work ?
Thank you |
|
|
Ttelmah Guest
|
|
Posted: Thu Sep 18, 2008 8:36 am |
|
|
Because CCS doesn't support pointers to const.....
Read the manual.
Depending on your compiler version, try declaring the array as ROM, rather than const. This results in a different (slightly slower) way of organising the array in the ROM, for which pointers can be used. However the program definition also needs to be changed to say that it is pointing to a ROM array. So:
Code: |
ROM char sigla[1024] ={0xfc,0xfe,0xff,0xff,.......} //1024 chars
void load_bmp(ROM char vector[])
|
Best Wishes |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu Sep 18, 2008 8:45 am |
|
|
Hi,
A couple of comments,
but first a question, not directed at you but to anyone who knows the answer.
Why does the CCS compiler, if indeed it does by looking at the original code allow function overloading if it follows standard C ?
now for the comments,
Firstly, you should call the function with
load_bmp(sigla); // no & sign
That is because the name of an array points to its address.
Your function definition would be better off being
void load_bmp(char *vector)
And lastly CCS has an issue with pointers to consts, there is a compiler directive which fixes this but adds overhead,
#device PASS_STRINGS=IN_RAM
I think will fix your problem. |
|
|
vibrasys
Joined: 20 Nov 2005 Posts: 16 Location: the Netherlands
|
|
Posted: Fri Sep 19, 2008 5:16 am |
|
|
Wayne and Ttelmah,
Thank you for your comments/suggestion.
I managed to get it running. I opted for the ROM storage for my bitmaps, since after only one loaded in ram, the Ram percetage went to 70+%.
Just added , Code: | ROM char array[1024] | and function def like Code: | void load_bmp(char ROM *vector) |
Regards |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|