Searching \ for '[pic]: const pointers in hitech C' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/languages.htm?key=c
Search entire site for: 'const pointers in hitech C'.

Exact match. Not showing close matches.
PICList Thread
'[pic]: const pointers in hitech C'
2001\06\28@063616 by mike

flavicon
face
How do I do this in Hitech C

I'm trying to pass a const pointer to a long, to allow a sub-procedure
access to a local variable of its caller without the overhead of
passing the pointer (I would hope the compiler would notice the
pointer is constant and  so not create code to pass it):  
I tried this....

void proc1(const long *var)
{
var++;
}

void proc2;
{
long count;
proc1(&count);
}


Looking at the code generated, proc1 is incrementing the pointer, not
the contents of count - I probably heva the syntax wrong  -  what is
the correct C syntax for the above ? Can what I'm trying to do actually be done ?

I know I could use a global, but that wastes RAM, as locals are
overlapped.
--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2001\06\28@070949 by Spehro Pefhany

picon face
At 11:36 AM 6/28/01 +0100, you wrote:
>How do I do this in Hitech C
>
>I'm trying to pass a const pointer to a long, to allow a sub-procedure
>access to a local variable of its caller without the overhead of
>passing the pointer (I would hope the compiler would notice the
>pointer is constant and  so not create code to pass it):
>
>I tried this....

>void proc1(const long *var)

(this declares function returning void and accepting a pointer to a const
long)

>{
>var++;

(this increments the pointer)

>}

You want:

void procl(long *var)   // function returns void and accepts a pointer to long
 {
 (*var)++                      // increment long variable referenced by the pointer
 }

>void proc2;
>{
>long count;
>proc1(&count);
>}

Best regards,
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Spehro Pefhany --"it's the network..."            "The Journey is the reward"
spam_OUTspeffTakeThisOuTspaminterlog.com             Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog  Info for designers:  http://www.speff.com
Contributions invited->The AVR-gcc FAQ is at: http://www.bluecollarlinux.com
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2001\06\28@093917 by mike

flavicon
face
OK, I understand that now, but this solution does not tell the
compiler that it will only ever be passed a pointer to the same RAM
location, and it isn't smart enough to figure it out for itself. By 'const pointer' I meant that the _value_ of the pointer will be
constant at link time, i.e. it always points to the same RAM address
(the one allocated to the local variable of the calling procedure) ,
so the compiler can generate fixed addresses for operations involving
the variable referenced by the pointer. Is there any way to explicitly
do this ?
the pointerOn Thu, 28 Jun 2001 07:12:20 -0400, you wrote:

{Quote hidden}

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2001\06\28@102157 by Spehro Pefhany

picon face
(Arhg.. sorry about the 2nd copy, Mike)
At 02:38 PM 6/28/01 +0100, you wrote:

> Is there any way to explicitly do this ?

Offhand, I don't think so. At least not using standard C alone. You
are really trying to outsmart the compiler.

I suggest you either inline the code (using more ROM space) or using
a static variable and live with the 2 bytes (using more RAM space) that
can't be overlayed- your code will be more portable and future
versions of the compiler may optimize better.

Best regards,


=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Spehro Pefhany --"it's the network..."            "The Journey is the reward"
speffspamKILLspaminterlog.com             Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog  Info for designers:  http://www.speff.com
Contributions invited->The AVR-gcc FAQ is at: http://www.bluecollarlinux.com
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

--
http://www.piclist.com hint: The PICList is archived three different
ways.  See http://www.piclist.com/#archives for details.


2001\06\30@044334 by Peter L. Peres

picon face
I don't use Hitech C but in standard C you want to write: ++(*var); in
your function to increment the pointer. If var is const then the compiler
should complain about your incrementing it. Do you get a warning ?

Peter

--
http://www.piclist.com hint: To leave the PICList
.....piclist-unsubscribe-requestKILLspamspam.....mitvma.mit.edu


2001\06\30@084636 by Spehro Pefhany

picon face
At 09:58 AM 6/29/01 +0300, you wrote:
>I don't use Hitech C but in standard C you want to write: ++(*var); in
>your function to increment the pointer. If var is const then the compiler
>should complain about your incrementing it. Do you get a warning ?

Yes, it was difficult for him to see what was going on because there were two
mistakes, the pointer was created *to* const (perfectly valid), and then the
*pointer* was incremented (also valid). Fix either one and you'll get a
message.

HiTech C conforms reasonably well to C, but to call it "ANSI C" is a bit
optimistic. Not that it would make much sense on a PIC anyway.

Best regards,
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
Spehro Pefhany --"it's the network..."            "The Journey is the reward"
EraseMEspeffspam_OUTspamTakeThisOuTinterlog.com             Info for manufacturers: http://www.trexon.com
Embedded software/hardware/analog  Info for designers:  http://www.speff.com
Contributions invited->The AVR-gcc FAQ is at: http://www.bluecollarlinux.com
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=

--
http://www.piclist.com hint: To leave the PICList
piclist-unsubscribe-requestspamspam_OUTmitvma.mit.edu


More... (looser matching)
- Last day of these posts
- In 2001 , 2002 only
- Today
- New search...