|
|
View previous topic :: View next topic |
Author |
Message |
x!ndy Guest
|
passing strings in functions |
Posted: Wed Apr 01, 2009 11:49 pm |
|
|
Hi everyone, can anybody tell me why this code will not work. Please help, I can't seem to figure out why it wont work.
Code: |
char *listPhNum()
{
char Flist[50]={};
strcpy(Flist,"09102840566+09192997346+09207395561");
return &Flist[0];
}
void main(){
char *msgP1=null;
char Msg[50]={};
delay_ms(3000);
do{
msgP1 = listPhNum();
fprintf(PC,"phnums = %s\n",msgP1);
strcpy(Msg, msgP1);
fprintf(PC,"phnums = %s\n",msg);
}while(1);
}
|
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Apr 02, 2009 1:18 am |
|
|
Make your array into a global array instead of a local one.
The program below works OK with vs. 4.090. If it doesn't
work for you then post your compiler version. Also post
your PIC. Here is the output:
Quote: |
phnums = 09102840566+09192997346+09207395561
phnums = 09102840566+09192997346+09207395561
|
Code: | #include <16F877.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS, stream=PC)
#include <stddef.h>
#include <string.h>
char Flist[50]={};
char *listPhNum(void)
{
strcopy(Flist,"09102840566+09192997346+09207395561");
return &Flist[0];
}
//============================
void main()
{
char *msgP1 = NULL;
char Msg[50]={};
//delay_ms(3000);
msgP1 = listPhNum();
fprintf(PC,"phnums = %s\n",msgP1);
strcpy(Msg, msgP1);
fprintf(PC,"phnums = %s\n",msg);
while(1);
} |
|
|
|
x!ndy Guest
|
|
Posted: Thu Apr 02, 2009 1:41 am |
|
|
thanks PCM programmer.. that was my last option in this problem. i was hoping that there is still some other way to do this. i am working on this somewhat big firmware project and i am trying as much as possible to make it modular and limit the number of global variables.. thanks again for your help.. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Apr 02, 2009 1:51 am |
|
|
Read item #3 in the section on "Don't return a pointer to a local variable":
http://www.openismus.com/documents/cplusplus/cpointers.shtml
Local variables are not stored on the stack with the PCM compiler
(16F PICs don't support it). But, CCS does re-use the RAM that it
allocates for local variables. So it's the same concept. You can't
depend upon the continued existance of local variables once the
program has returned from calling a function. |
|
|
FvM
Joined: 27 Aug 2008 Posts: 2337 Location: Germany
|
|
Posted: Thu Apr 02, 2009 1:57 am |
|
|
The code must be supposed to fail with any existing C compiler. |
|
|
x!ndy Guest
|
|
Posted: Thu Apr 02, 2009 2:10 am |
|
|
gee... i never knew that one.. thanks PCM programmer.. you have been extremely helpful.. |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Thu Apr 02, 2009 2:10 am |
|
|
There is another way to do it:-
Code: |
char *listPhNum()
{
char *Flist;
// Allocate enough memory to hold the string
Flist = malloc(sizeof("09102840566+09192997346+09207395561"));
// or
Flist = malloc(36); // must include the null terminating char /0
strcpy(Flist,"09102840566+09192997346+09207395561");
return Flist; // &Flist[0] is the same as just specifying Flist!
}
void main(){
char *msgP1=null;
char Msg[50]={};
delay_ms(3000);
do{
msgP1 = listPhNum();
fprintf(PC,"phnums = %s\n",msgP1);
strcpy(Msg, msgP1);
free(msgP1); // THIS IS IMPORTANT see note below
msgP1 = NULL; // Just to keep it tidy, may not be required in all cases but it is safer to do it!
fprintf(PC,"phnums = %s\n",msg);
}while(1);
}
|
Because you are dynamically allocating memory you MUST release (free) that memory when you are no longer using it. If you don't you WILL have what is known as a memory leak and you will run out of memory. |
|
|
x!ndy Guest
|
passing strings in functions |
Posted: Tue Apr 14, 2009 8:41 pm |
|
|
what if i use static local variables? will it serve the same purpose?
Code: |
char *listPhNum()
{
char Flist[50]={};
strcpy(Flist,"09102840566+09192997346+09207395561");
return &Flist[0];
}
void main(){
char *msgP1=null;
char Msg[50]={};
delay_ms(3000);
do{
msgP1 = listPhNum();
fprintf(PC,"phnums = %s\n",msgP1);
strcpy(Msg, msgP1);
fprintf(PC,"phnums = %s\n",msg);
}while(1);
}
|
|
|
|
Ttelmah Guest
|
|
Posted: Wed Apr 15, 2009 2:26 am |
|
|
Yes. But with the 'downside', that every static variable permanently uses RAM storage.
It is more efficient, to declare a variable in the _calling_ function, to store the required return, and pass the address _to_ to subroutine. Then the subroutine doesn't have to use any RAM space, and the RAM can be re-used when the calling function has finished with it. Basically, keep the variables alive only as long as they are needed, and the RAM can be re-used.
If you have one 'global' temporary buffer, only one RAM area is needed. If you have five static buffers in five subroutines, five times as much RAM is needed. The most efficient of all, is to only declare the variable at the start of the function that is going to use it, and pass the address of this to each subroutine as required.
Best Wishes |
|
|
|
|
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
|