![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
future
Joined: 14 May 2004 Posts: 330
|
A question about function returning pointers to ram |
Posted: Fri May 04, 2007 10:27 am |
|
|
Hi,
How to have functions returning pointers to ram strings without global arrays?
I mean, a function prototyped as char *myfunction( char value ); returning the ascii representation of the value for the printf( "%s", myfunction(10) );.
myfunction would have to do its work with a global array.
So I declare it, but a long printf( "%s %s %s", , myfunction(10), myfunction(20), myfunction(30) ); would need 3 global arrays.
How about other solutions?
Thank you. |
|
![](templates/subSilver/images/spacer.gif) |
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
|
Posted: Fri May 04, 2007 11:55 am |
|
|
What you ask to do with function pointers can not be done on a PIC in an efficient manner.
What information is it that you wish to display? What format do you wish to display it in? |
|
![](templates/subSilver/images/spacer.gif) |
future56 Guest
|
|
Posted: Fri May 04, 2007 1:20 pm |
|
|
It´s not function pointers, but functions returning pointers to ram.
I want to convert many variables to a readable format and display them. So if I need to show 100 counts of 5ms, I´d call the convert function passing 100 to it and it returns a pointer to a string containing "500mS".
I can have a function for each kind of variable and just call them using printf when I need.
One solution is allocating many global arrays and pass the variable and array address to the function and it returns the same array address, but I have to take care of array usage myself when many conversions are needed in the same printf (need 8 at some places).
printf( "%s %s %s %s", myfunction( 100, array1), myfunction( 10, array2), myfunction( 200, array3), myfunction( 300, array4) )
There is another problem with printf not liking more than 3 strings, but this will be addressed later.
Maybe there is a clever way to do this.
Thank you. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah Guest
|
|
Posted: Fri May 04, 2007 2:57 pm |
|
|
This is what the memory handling functions, in memmgmt.c, are designed to handle. When the functon is first called, have it generate a node, using a static variable to store the 'node_t' variable. Then on future calls, work out how long the string needs to be, and use the 'update_node' function, to change the size.
Then when you have finished with the memory, use 'remove_node' to free the space.
Unfortunately, the space has to be pre-allocated, so unless you have other code that also uses the same memory handling ability, you will not save anything. Also, the calls can beccome rather unreliable...
Personally, I'd probably just allocate a static area to hold the returns, or have the calling program, give the function the location it wants the result to be put into.
Best Wishes |
|
![](templates/subSilver/images/spacer.gif) |
future
Joined: 14 May 2004 Posts: 330
|
|
Posted: Fri May 04, 2007 6:04 pm |
|
|
Ok, I'll have static arrays declared inside each function and see how it goes.
Thank you. |
|
![](templates/subSilver/images/spacer.gif) |
libor
Joined: 14 Dec 2004 Posts: 288 Location: Hungary
|
|
Posted: Sat May 05, 2007 2:24 am |
|
|
future56 wrote: | There is another problem with printf not liking more than 3 strings, but this will be addressed later. |
...and what about programmers not liking 3-line-long instructions ?
What is the reason not using separate 'printf's ? You could then even arrange them into a loop using adressable arguments (e.g. an array of strings). |
|
![](templates/subSilver/images/spacer.gif) |
future
Joined: 14 May 2004 Posts: 330
|
|
Posted: Sat May 05, 2007 9:41 am |
|
|
Quote: | ...and what about programmers not liking 3-line-long instructions ?
|
I use long printf's to write a whole 16x2 LCD screen, some have 6 fields.
Quote: | What is the reason not using separate 'printf's ? You could then even arrange them into a loop using adressable arguments (e.g. an array of strings). |
There is not a good reason, it is just a way I found to make the code work.
As I am not a professional programmer I try to write working code and then look how others did the same thing.
I am working on a menu system with about 60 screens, ranging from simple messages, alarms, to char and 3D array editing... |
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|