[PIC] need to improve skill for picc string and array. my code don't work

classic Classic list List threaded Threaded
15 messages Options
Reply | Threaded
Open this post in threaded view
|

[PIC] need to improve skill for picc string and array. my code don't work

microsoftwarecontrol
in picc, have functions:

=========first, call fun1 is working========

char * fun(void)
{
 char ca[6]={0,0,0,0,0,0};

// doing something about ca[]

 return ca;
}

char * fun1(void)
{
 char * charpointer;

 charpointer=fun(); //inside fun1 calls fun()...like one fun....

 return charpointer;
}

/////////////  call:   char * pnt=fun1();



///////////////why below doesn't work? need to call it outside of fun2
///////////////I don't see any concept mistake there, till now, in C language.
==========second, call fun2 and has fun as parameter, don't working
================ can be compiled without warning===========

char * fun(voi)
{
 char ca[]

 return ca;
}

char * fun2(char * charpointer)   //       return of fun will be parameter now
{
 
 charpointer[5]='\0';

 return charpointer;
}

/////call:   char * pnt=fun2(fun()); ////don't work
--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: [PIC] need to improve skill for picc string and array. my code don't work

Matthew Miller-2
Hi,

On Sun, Jun 12, 2005 at 09:55:59AM -0400, microsoftwarecontrol wrote:
> in picc, have functions:

You have a couple problems here. Depending on how picc handles somethings,
my concerns may not be valid (though I know they would be when using C on a
"larger" cpu.)

> char * fun(void)
> {
>  char ca[6]={0,0,0,0,0,0};
>
> // doing something about ca[]
>
>  return ca;
> }

Here you are returning a pointer to an array that is allocated on the stack
(wherever the stack is on the pic your using). This is bad. The very next
function call you make will likely overwrite the contents of the array.

> char * fun1(void)
> {
>  char * charpointer;
>
>  charpointer=fun(); //inside fun1 calls fun()...like one fun....
>
>  return charpointer;
> }

You make the mistake of using that pointer here in fun1().

> char * fun(voi)
> {
>  char ca[]
>
>  return ca;
> }

You can't declare an array without also specifying the size of the
array. I'm sure that your compiler produces an error concerning the line
"char ca[]", though you don't include the error in your email message. Oh,
that line is also missing a semicolon.

> char * fun2(char * charpointer)   //       return of fun will be parameter now
> {
>  
>  charpointer[5]='\0';
>
>  return charpointer;
> }
>
> /////call:   char * pnt=fun2(fun()); ////don't work

I believe you need to think through your code a bit more. The mistakes you
have made can be easily avoided by heeding the warnings and errors that the
compiler reports. Even with no warnings or errors, logic errors may still
exist, and some compilers may not issue a warning regarding the type of
error that is in your fun().

Take care, Matthew.

--
If we don't believe in freedom of expression for people we despise, we
don't believe in it at all.   -- Noam Chomsky

--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: [PIC] need to improve skill for picc string and array. my code don't work

Gerhard Fiedler
Matthew Miller wrote:

> You have a couple problems here. Depending on how picc handles somethings,
> my concerns may not be valid (though I know they would be when using C on a
> "larger" cpu.)
>
>> char * fun(void)
>> {
>>  char ca[6]={0,0,0,0,0,0};
>>
>> // doing something about ca[]
>>
>>  return ca;
>> }
>
> Here you are returning a pointer to an array that is allocated on the stack
> (wherever the stack is on the pic your using).

The HiTech PICC uses a static stack that is generated at compile time. This
means that automatic variables and function arguments reside in general in
a static structure that follows the call tree. (This of course requires a
static call tree, which, among others, limits the use of function pointers
and prohibits recursion.)

> This is bad. The very next function call you make will likely overwrite
> the contents of the array.

Exactly. While this may work in some cases, it is not guaranteed to work.
And it is guaranteed to fail in most cases :)

Automatic variables only live while they are in scope. ca is an automatic
variable with its scope being the function fun(); it ceases to exist after
the return statement, so the pointer to it becomes meaningless. If you want
to do it that way, make ca static:

char * fun(void)
{
  static char ca[6]={0,0,0,0,0,0};
  // doing something about ca[]
  return ca;
}

> You make the mistake of using that pointer here in fun1().

After declaring it static, the result returned by fun() continues to be
valid even after ca is not in scope anymore. Technically, this results in
ca not being located in the static stack anymore, but in a separate area
used for longer living memory objects.

> I believe you need to think through your code a bit more.

It also seems to me that you're new to C. While it definitely is possible
to learn C with PICC on a PIC as target, it is much more comfortable to
learn the basics of C with a "big" C compiler on a "big" computer (like
your PC). The edit/compile/debug cycles are quicker, debugging is more
comfortable, the compilers are usually more elaborate in terms of error and
warning messages, you can use something like splint to help you further
with additional warnings about bad programming style, you can use the many
beginner's C tutorials that are written for such an environment, etc.

This gets you quicker to a point where the language is not a problem
anymore. After that, it's much easier to tackle the particularities of a
specific microcontroller environment.

> The mistakes you have made can be easily avoided by heeding the warnings
> and errors that the compiler reports.

Yep... this doesn't guarantee that it works, but it is a helpful step in
that direction :)

Use warning level -3 with PICC. It is a good compromise and shows pretty
much everything of importance (of what the compiler can show). Research
each and every warning, make sure you understand what it means, and write
your code warning-free. This is still possible at level -3 (not necessarily
anymore at higher warning levels).

Gerhard
--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: [PIC] need to improve skill for picc string and array. my code don't work

Matthew Miller-2
Hi Gerhard,

On Mon, Jun 13, 2005 at 09:17:04AM -0300, Gerhard Fiedler wrote:
> Matthew Miller wrote:
>
> > I believe you need to think through your code a bit more.
>
> It also seems to me that you're new to C.

Not at all, I'm only new to C on the PIC. This is why I had a preface in my
reply about my C on PIC knowledge. ;^)

Matthew.

--
"For the sake of freedom and dignity, for the sake of democracy, in the
interests of shoring up the battered U.S. economy, it is time to call a truce
in the "War on Drugs," an unconditional cease-fire."

-- Jonathan Ott, _Pharmacotheon_
--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: [PIC] need to improve skill for picc string and array. my code don't work

Andre Abelian
Matthew,

I think it is better to learn c first get basic understanding then move
to electronics
in c. c is easy. To my understanding about programming micros shouldn't
be in ansi c
it should be customized like what CCS C does made built in functions to
get things
done faster or have good library.

Andre








Matthew Miller wrote:

>Hi Gerhard,
>
>On Mon, Jun 13, 2005 at 09:17:04AM -0300, Gerhard Fiedler wrote:
>  
>
>>Matthew Miller wrote:
>>
>>    
>>
>>>I believe you need to think through your code a bit more.
>>>      
>>>
>>It also seems to me that you're new to C.
>>    
>>
>
>Not at all, I'm only new to C on the PIC. This is why I had a preface in my
>reply about my C on PIC knowledge. ;^)
>
>Matthew.
>
>  
>

--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: [PIC] need to improve skill for picc string and array. my code don't work

Matthew Miller-2
Hi Andre,

On Mon, Jun 13, 2005 at 05:04:37PM -0700, Andre Abelian wrote:
> Matthew,
>
> I think it is better to learn c first get basic understanding then move to
> electronics in c. c is easy. To my understanding about programming micros
> shouldn't be in ansi c it should be customized like what CCS C does made
> built in functions to get things done faster or have good library.

I don't guess you read my posts too closely... I know C pretty well,
ANSI/ISO C that is. That's why in my original message I said my comments
might not apply to the compiler used by the other person. Gerhard responded
and showed that my assumptions weren't valid because of how PICC handled the
stack (as well as how the static keyword would solve one of the original
posters problems.)

Take care. Matthew.

--
"Every advance in civilization has been denounced while it was still recent."

-- Bertrand Russell
--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re1: [PIC] need to improve skill for picc string and array. my code don't work

microsoftwarecontrol
In reply to this post by Gerhard Fiedler
I tested static inside function and tested
move it to global. Yes, static made local
string bring content out, but only when is
odd number calls and even number calls
gave either no content or mess up content.
Half working.

By the way, is there, some where, good code
to do int2char? I know my code is not good one.

So, now I best code for me is here, direct copy
from c file:
========= below two fun are the working perfectly====================
char * uint2bcd(unsigned int v){
// char i;
 unsigned char temp[6]={0,0,0,0,0,0}; // if static, mess up, . no static
here.
   //tried move out as global char []. other use global. but content mess
up!!! keep this way
   //static works at odd int number for uintbcd2char.  half workign!
 while(v>=10000){
  temp[0]++; v-=10000;  //[0] is biggest digit,,,differ from for hd44100.c
and for hd4478 _puts(char *)
 }
 while(v>=1000){
  temp[1]++; v-=1000;
 }
 while(v>=100){
  temp[2]++; v-=100;
 }
 while(v>=10){
  temp[3]++; v-=10;
 }
  temp[4]=v;
 //[5] leave for add end char

 return temp;   //no eof in str
}

char * uint2char(unsigned int i_num){
 char ch[10]={'0','1','2','3','4','5','6','7','8','9'};
 char i=0;
 char * temp; //here is [] will not work. [] only pass add to first???
 char tmp;

 temp = uint2bcd(i_num); //inside it has uchar temp[5],,,[0] is least digit
 //*(temp+5)='\0';

 /*
 while(*temp){ //pnt instead array below....temp hasn't \0 terminated. can
stop here.
  tmp=ch[*temp];
  *temp++=tmp; //here pnt at end...cal below can't do.
 }
 temp-=5;//move pnt back original...still doesn't work...add '\0' still
don't
 */
 ///*
 for (i=0;i<6;i++){
  tmp=ch[temp[i]]; //bcd char value -> ASCII char
  temp[i]=tmp;
 }
 //*/

 return temp;
}

===========below is code what I hope to work...but global, static, pass char
[6], none of them works!!!!=============
char * uintbcd2char( char * temp){ // pointer bring change out
 char ch[10]={'0','1','2','3','4','5','6','7','8','9'};
 char i=0,tmp=0;
 //char * temp; //here is [] will not work

 for (i=0;i<6;i++){
  tmp=ch[temp[i]]; //bcd char value -> ASCII char
  temp[i]=tmp;
 }

 ////leading '0' changed to space
 for(i=0;i<4;i++){   //[1] and [0]no need to change back
  if(temp[i]=='0') temp[i]=' ';  //if =0; stop sending to hd44780. so use '
'...
  ////OK! fix wide of 5 dig...even better
  else break; //once meets none '0', leading 0 end and out!
 }

 temp[5]='\0'; //end char of string...no it, "@_" at end. now, ok!

 return temp;
}







----- Original Message -----
From: "Gerhard Fiedler" <[hidden email]>
To: "Microcontroller discussion list - Public." <[hidden email]>
Sent: Monday, June 13, 2005 8:17 AM
Subject: Re: [PIC] need to improve skill for picc string and array. my code
don't work


> Matthew Miller wrote:
>
> > You have a couple problems here. Depending on how picc handles
somethings,
> > my concerns may not be valid (though I know they would be when using C
on a

> > "larger" cpu.)
> >
> >> char * fun(void)
> >> {
> >>  char ca[6]={0,0,0,0,0,0};
> >>
> >> // doing something about ca[]
> >>
> >>  return ca;
> >> }
> >
> > Here you are returning a pointer to an array that is allocated on the
stack
> > (wherever the stack is on the pic your using).
>
> The HiTech PICC uses a static stack that is generated at compile time.
This

> means that automatic variables and function arguments reside in general in
> a static structure that follows the call tree. (This of course requires a
> static call tree, which, among others, limits the use of function pointers
> and prohibits recursion.)
>
> > This is bad. The very next function call you make will likely overwrite
> > the contents of the array.
>
> Exactly. While this may work in some cases, it is not guaranteed to work.
> And it is guaranteed to fail in most cases :)
>
> Automatic variables only live while they are in scope. ca is an automatic
> variable with its scope being the function fun(); it ceases to exist after
> the return statement, so the pointer to it becomes meaningless. If you
want

> to do it that way, make ca static:
>
> char * fun(void)
> {
>   static char ca[6]={0,0,0,0,0,0};
>   // doing something about ca[]
>   return ca;
> }
>
> > You make the mistake of using that pointer here in fun1().
>
> After declaring it static, the result returned by fun() continues to be
> valid even after ca is not in scope anymore. Technically, this results in
> ca not being located in the static stack anymore, but in a separate area
> used for longer living memory objects.
>
> > I believe you need to think through your code a bit more.
>
> It also seems to me that you're new to C. While it definitely is possible
> to learn C with PICC on a PIC as target, it is much more comfortable to
> learn the basics of C with a "big" C compiler on a "big" computer (like
> your PC). The edit/compile/debug cycles are quicker, debugging is more
> comfortable, the compilers are usually more elaborate in terms of error
and

> warning messages, you can use something like splint to help you further
> with additional warnings about bad programming style, you can use the many
> beginner's C tutorials that are written for such an environment, etc.
>
> This gets you quicker to a point where the language is not a problem
> anymore. After that, it's much easier to tackle the particularities of a
> specific microcontroller environment.
>
> > The mistakes you have made can be easily avoided by heeding the warnings
> > and errors that the compiler reports.
>
> Yep... this doesn't guarantee that it works, but it is a helpful step in
> that direction :)
>
> Use warning level -3 with PICC. It is a good compromise and shows pretty
> much everything of importance (of what the compiler can show). Research
> each and every warning, make sure you understand what it means, and write
> your code warning-free. This is still possible at level -3 (not
necessarily
> anymore at higher warning levels).
>
> Gerhard
> --
> http://www.piclist.com PIC/SX FAQ & list archive
> View/change your membership options at
> http://mailman.mit.edu/mailman/listinfo/piclist
>


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com 
--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: [PIC] need to improve skill for picc string and array. my code don't work

Gerhard Fiedler
In reply to this post by Matthew Miller-2
Matthew Miller wrote:

>> It also seems to me that you're new to C.
>
> Not at all, I'm only new to C on the PIC. This is why I had a preface in my
> reply about my C on PIC knowledge. ;^)

Sorry if it came out that way... I meant of course the OP :)  I was mostly
only reinforcing or expanding on the points you made.

It is quite obvious that you are not new to C.

Gerhard
--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: [PIC] need to improve skill for picc string and array. my code don't work

Gerhard Fiedler
In reply to this post by Matthew Miller-2
Matthew Miller wrote:

> Gerhard responded and showed that my assumptions weren't valid because of
> how PICC handled the stack ...

I don't know what assumptions you mean here, but everything you said was
valid. The automatic char array ca was located on the (static) stack, and
could have been overwritten any time once the variable got out of scope,
just like with a normal stack. And just like with a normal stack, it's
possible that this takes a while and gives the illusion that it works,
under certain circumstances.

I just applied your C-generic comments to the specific compiler the OP is
using.

Gerhard
--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: [PIC] need to improve skill for picc string and array. my code don't work

Matthew Miller-2
In reply to this post by microsoftwarecontrol
On Tue, Jun 14, 2005 at 12:30:24AM -0400, microsoftwarecontrol wrote:
> I tested static inside function and tested
> move it to global. Yes, static made local
> string bring content out, but only when is
> odd number calls and even number calls
> gave either no content or mess up content.
> Half working.

Hi,

Would you mind posting the whole source file and the complete error messages
that your compiler produced? This would enable us to help you better. You
are not providing enough information IMO. Just saying "It's not working" is
less than helpful.

Take care. Matthew.

--
QUOTE OF THE MOMENT:
If you didn't get caught, did you really do it?
--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: [PIC] need to improve skill for picc string and array. my code don't work

microsoftwarecontrol
here it is:

call them is  "lcd_puts(uint2char(1234));"

compile, in any following situation, has no error, no warning:
PICC -W9 -FAKELOCAL -G9 -O  -Zg -D24  -ICD  -II:\MY_MCU\MYHWORK
I:\MY_MCU\MIV_05.C -16F876A




========= below two fun are the working perfectly====================
char * uint2bcd(unsigned int v){
// char i;
 unsigned char temp[6]={0,0,0,0,0,0}; // if static, mess up, . no static
here.
   //tried move out as global char []. other use global. but content mess
up!!! keep this way
   //static works at odd int number for uintbcd2char.  half workign!
 while(v>=10000){
  temp[0]++; v-=10000;  //[0] is biggest digit,,,differ from for hd44100.c
and for hd4478 _puts(char *)
 }
 while(v>=1000){
  temp[1]++; v-=1000;
 }
 while(v>=100){
  temp[2]++; v-=100;
 }
 while(v>=10){
  temp[3]++; v-=10;
 }
  temp[4]=v;
 //[5] leave for add end char

 return temp;   //no eof in str
}

char * uint2char(unsigned int i_num){
 char ch[10]={'0','1','2','3','4','5','6','7','8','9'};
 char i=0;
 char * temp; //here is [] will not work. [] only pass add to first???
 char tmp;

 temp = uint2bcd(i_num); //inside it has uchar temp[5],,,[0] is least digit
 //*(temp+5)='\0';

 /*
 while(*temp){ //pnt instead array below....temp hasn't \0 terminated. can
stop here.
  tmp=ch[*temp];
  *temp++=tmp; //here pnt at end...cal below can't do.
 }
 temp-=5;//move pnt back original...still doesn't work...add '\0' still
don't
 */
 ///*
 for (i=0;i<6;i++){
  tmp=ch[temp[i]]; //bcd char value -> ASCII char
  temp[i]=tmp;
 }
 //*/

 return temp;
}

===========below is code what I hope to work...but global, static, pass char
[6], none of them works!!!!=============
char * uintbcd2char( char * temp){ // pointer bring change out
 char ch[10]={'0','1','2','3','4','5','6','7','8','9'};
 char i=0,tmp=0;
 //char * temp; //here is [] will not work

 for (i=0;i<6;i++){
  tmp=ch[temp[i]]; //bcd char value -> ASCII char
  temp[i]=tmp;
 }

 ////leading '0' changed to space
 for(i=0;i<4;i++){   //[1] and [0]no need to change back
  if(temp[i]=='0') temp[i]=' ';  //if =0; stop sending to hd44780. so use '
'...
  ////OK! fix wide of 5 dig...even better
  else break; //once meets none '0', leading 0 end and out!
 }

 temp[5]='\0'; //end char of string...no it, "@_" at end. now, ok!

 return temp;
}

----- Original Message -----
From: "Matthew Miller" <[hidden email]>
To: "Microcontroller discussion list - Public." <[hidden email]>
Sent: Tuesday, June 14, 2005 9:02 PM
Subject: Re: [PIC] need to improve skill for picc string and array. my code
don't work


> On Tue, Jun 14, 2005 at 12:30:24AM -0400, microsoftwarecontrol wrote:
> > I tested static inside function and tested
> > move it to global. Yes, static made local
> > string bring content out, but only when is
> > odd number calls and even number calls
> > gave either no content or mess up content.
> > Half working.
>
> Hi,
>
> Would you mind posting the whole source file and the complete error
messages
> that your compiler produced? This would enable us to help you better. You
> are not providing enough information IMO. Just saying "It's not working"
is

> less than helpful.
>
> Take care. Matthew.
>
> --
> QUOTE OF THE MOMENT:
> If you didn't get caught, did you really do it?
> --
> http://www.piclist.com PIC/SX FAQ & list archive
> View/change your membership options at
> http://mailman.mit.edu/mailman/listinfo/piclist
>


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com 
--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

c fileb Re: [PIC] need to improve skill for picc string and array. my code don't work

microsoftwarecontrol
In reply to this post by Matthew Miller-2
ok. attach c here


----- Original Message -----
From: "Matthew Miller" <[hidden email]>
To: "Microcontroller discussion list - Public." <[hidden email]>
Sent: Tuesday, June 14, 2005 9:02 PM
Subject: Re: [PIC] need to improve skill for picc string and array. my code
don't work


> On Tue, Jun 14, 2005 at 12:30:24AM -0400, microsoftwarecontrol wrote:
> > I tested static inside function and tested
> > move it to global. Yes, static made local
> > string bring content out, but only when is
> > odd number calls and even number calls
> > gave either no content or mess up content.
> > Half working.
>
> Hi,
>
> Would you mind posting the whole source file and the complete error
messages
> that your compiler produced? This would enable us to help you better. You
> are not providing enough information IMO. Just saying "It's not working"
is

> less than helpful.
>
> Take care. Matthew.
>
> --
> QUOTE OF THE MOMENT:
> If you didn't get caught, did you really do it?
> --
> http://www.piclist.com PIC/SX FAQ & list archive
> View/change your membership options at
> http://mailman.mit.edu/mailman/listinfo/piclist
>

--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist

int2charb.c (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: c fileb Re: [PIC] need to improve skill for picc string and array. my code don't work

Matthew Miller-2
Hi,

On Tue, Jun 14, 2005 at 11:45:00PM -0400, microsoftwarecontrol wrote:
> ok. attach c here

Thanks for sending your source file. I'm sorry if I wasn't clear in my last
message, but could you provide a description of what is not working? I do
not have a copy of the PICC compiler, so if this source doesn't compile
please copy and paste the whole compiler output in your next message. If the
program isn't operating as you want it to, please tell me how it behaves
verses how you want it to. Please, flood me with information! ;^)

Take care. Matthew

--
"Everytime you declare main() as returning void - somewhere a little
baby cries.  So please, do it for the children."  -- Daniel Fox
--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: [PIC] need to improve skill for picc string and array. my code don't work

Gerhard Fiedler
In reply to this post by microsoftwarecontrol
Hello you (what's your name? :)

When you post code for others to help you, you could post it in a way that
is readable and quote-able. That means that you break your sources (code
and comments) at approx. 60-70 chars; pretty much everybody can read that
and it can be quoted and is still readable. (This is different from normal
free flowing text, because that can be re-flown to whatever line length the
reader wants it. But this doesn't work well with source code, unless you're
participating in a C obfuscation contest :)

Then it would be nice if you followed the advice you got previously. Gives
people the nice feeling that their advice has been taken seriously. Both
Matthew and I told you that returning a pointer to an automatic variable
/does not work/. To quote myself: "While this may work in some cases, it is
not guaranteed to work. And it is guaranteed to fail in most cases." I
added a smiley, but I was dead serious.

You don't have to believe what people tell you. But if you ask, get an
answer, and don't believe it, you better research it before you ask again
-- and know why you don't think the answer was correct. If you had done
that, you'd have come to the conclusion that what Matthew and I told you
was correct, and that the problem with your code is still in your lack of
understanding of the storage classes of C.

This thing with storage classes and scopes can be a bit confusing in C, and
you really should spend a few hours on it. It's quite essential for any C
programming.

A quick run-down:

Storage classes:
- Automatic: Variables not declared "static" inside blocks (functions are
blocks). Get created every time the block goes into scope. Get initialized
if they have an initializer, otherwise are not initialized.
- Static: Variables declared "static" inside blocks and all variables
declared outside of functions. Live from before the call to main() until
after main() returns. Usually get initialized to 0 (or to the initializer
value), but only once (before calling main() ).

Scopes:
- Block: Variables declared inside a block, whether declared "static" or
not. Is not visible outside of the block, even if it still exists (in the
case of "static" variables), but can be accessed through pointers, for
example (that's what you are trying to do).
- File: Variables declared "static" outside of functions. Are visible from
the point of creation to the end of the file, but not outside of the file.
- Global: Variables not declared "static" outside of functions. Are visible
in all modules that are linked with your program.
- Any variable of a more "outer" scope can be hidden by a variable of the
same name of a more "inner" scope.

But that's not quite enough to get a grip on it, probably. So look for a
good tutorial and study this until you get a grip.



microsoftwarecontrol wrote:

> ========= below two fun are the working perfectly====================

Well, no, they don't.

> char * uint2bcd(unsigned int v){
>  unsigned char temp[6]={0,0,0,0,0,0};
>  // ...
>  return temp;
> }

This is guaranteed to break sooner or later.

Not sure what you did when you made temp static. However, something like
this is not going to work in your case:

char * uint2bcd(unsigned int v){
  static unsigned char stemp[6]={0,0,0,0,0,0};
  // ...

The difference is that the automatic variable temp gets created /and
initialized/ every time the function gets called. The static variable stemp
gets created and initialized only once: at the start of the program.
(Before the call to main, actually.)

Since you are expecting the variable to be initialized to all zeroes, you'd
have to do that explicitly and individually:

char * uint2bcd(unsigned int v){
  static unsigned char stemp[6];
  stemp[0] = 0; // individual initialization of each byte
  while( v>=10000 ){
    stemp[0]++;
    v-=10000;
  }
  // ...

Since that's already pretty much guaranteed to not work reliably in your
code, I'll stop here... No use in debugging the rest until this is fixed.
(Especially since it's pretty much unreadable.)

Take care,
Gerhard
--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Reply | Threaded
Open this post in threaded view
|

Re: [PIC] need to improve skill for picc string and array. my code don't work

microsoftwarecontrol
thank you for advises.

if having time, I will go to mplab to trace var.
I believe there must be some error hiding somewhere
and have not been touched yet.

----- Original Message -----
From: "Gerhard Fiedler" <[hidden email]>
To: "Microcontroller discussion list - Public." <[hidden email]>
Sent: Wednesday, June 15, 2005 8:03 AM
Subject: Re: [PIC] need to improve skill for picc string and array. my code
don't work


> Hello you (what's your name? :)
>
> When you post code for others to help you, you could post it in a way that
> is readable and quote-able. That means that you break your sources (code
> and comments) at approx. 60-70 chars; pretty much everybody can read that
> and it can be quoted and is still readable. (This is different from normal
> free flowing text, because that can be re-flown to whatever line length
the
> reader wants it. But this doesn't work well with source code, unless
you're
> participating in a C obfuscation contest :)
>
> Then it would be nice if you followed the advice you got previously. Gives
> people the nice feeling that their advice has been taken seriously. Both
> Matthew and I told you that returning a pointer to an automatic variable
> /does not work/. To quote myself: "While this may work in some cases, it
is

> not guaranteed to work. And it is guaranteed to fail in most cases." I
> added a smiley, but I was dead serious.
>
> You don't have to believe what people tell you. But if you ask, get an
> answer, and don't believe it, you better research it before you ask again
> -- and know why you don't think the answer was correct. If you had done
> that, you'd have come to the conclusion that what Matthew and I told you
> was correct, and that the problem with your code is still in your lack of
> understanding of the storage classes of C.
>
> This thing with storage classes and scopes can be a bit confusing in C,
and

> you really should spend a few hours on it. It's quite essential for any C
> programming.
>
> A quick run-down:
>
> Storage classes:
> - Automatic: Variables not declared "static" inside blocks (functions are
> blocks). Get created every time the block goes into scope. Get initialized
> if they have an initializer, otherwise are not initialized.
> - Static: Variables declared "static" inside blocks and all variables
> declared outside of functions. Live from before the call to main() until
> after main() returns. Usually get initialized to 0 (or to the initializer
> value), but only once (before calling main() ).
>
> Scopes:
> - Block: Variables declared inside a block, whether declared "static" or
> not. Is not visible outside of the block, even if it still exists (in the
> case of "static" variables), but can be accessed through pointers, for
> example (that's what you are trying to do).
> - File: Variables declared "static" outside of functions. Are visible from
> the point of creation to the end of the file, but not outside of the file.
> - Global: Variables not declared "static" outside of functions. Are
visible

> in all modules that are linked with your program.
> - Any variable of a more "outer" scope can be hidden by a variable of the
> same name of a more "inner" scope.
>
> But that's not quite enough to get a grip on it, probably. So look for a
> good tutorial and study this until you get a grip.
>
>
>
> microsoftwarecontrol wrote:
>
> > ========= below two fun are the working perfectly====================
>
> Well, no, they don't.
>
> > char * uint2bcd(unsigned int v){
> >  unsigned char temp[6]={0,0,0,0,0,0};
> >  // ...
> >  return temp;
> > }
>
> This is guaranteed to break sooner or later.
>
> Not sure what you did when you made temp static. However, something like
> this is not going to work in your case:
>
> char * uint2bcd(unsigned int v){
>   static unsigned char stemp[6]={0,0,0,0,0,0};
>   // ...
>
> The difference is that the automatic variable temp gets created /and
> initialized/ every time the function gets called. The static variable
stemp
> gets created and initialized only once: at the start of the program.
> (Before the call to main, actually.)
>
> Since you are expecting the variable to be initialized to all zeroes,
you'd

> have to do that explicitly and individually:
>
> char * uint2bcd(unsigned int v){
>   static unsigned char stemp[6];
>   stemp[0] = 0; // individual initialization of each byte
>   while( v>=10000 ){
>     stemp[0]++;
>     v-=10000;
>   }
>   // ...
>
> Since that's already pretty much guaranteed to not work reliably in your
> code, I'll stop here... No use in debugging the rest until this is fixed.
> (Especially since it's pretty much unreadable.)
>
> Take care,
> Gerhard
> --
> http://www.piclist.com PIC/SX FAQ & list archive
> View/change your membership options at
> http://mailman.mit.edu/mailman/listinfo/piclist
>


__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around
http://mail.yahoo.com 
--
http://www.piclist.com PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist