View previous topic :: View next topic |
Author |
Message |
hmnrobots
Joined: 02 Oct 2009 Posts: 11
|
Setting a pointerred value |
Posted: Thu Apr 11, 2013 3:10 am |
|
|
Hi all
A 2 days Nightmare !
Chip is a 16f767, compiler 4.104
I declare a GLOBAL pointer:
int8 *FwdDev=0;
and a variable:
int8 d;
later then when I do:
(*FwdDev) = d;
Fprintf(HF,"\n\r RH d:%u %u Adr %u \n\r",d,*FwdDev,&FwdDev);
then I got:
RH d:177 0 Adr 80
That confirm the value of d is 177 but *FwdDev still remains null.I just don't understand!
any help please.
I even added:
*FwdDev = 5;
if(*FwdDev !=5) fprintf(HF,"\n\r No \n\r");
and I got always the answer NO
what is my mistake?
I added also #device *=16 : No success as well
sorry to disturb you with such a basic question |
|
|
Mike Walne
Joined: 19 Feb 2004 Posts: 1785 Location: Boston Spa UK
|
|
Posted: Thu Apr 11, 2013 8:58 am |
|
|
Post the shortest possible complete compilable code which shows the problem you're having.
Then we can all see exactly what you are doing.
Mike |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Apr 11, 2013 11:25 am |
|
|
The pointer needs to point to some legitmate, allocated storage location
in RAM. Your code doesn't do that.
See the program below. One byte of RAM is allocated to the variable
'myval'. This allocation is done by the "int8 myval;" statement.
Now I can set the pointer, FwdDev, to point to myval. I do that by
taking the address of myval and putting it into FwdDev. Now it all
works. It displays 177.
The key thing you need to remember is that just simply creating a
pointer doesn't create or allocate any RAM storage locations for data.
You have a pointer, but you don't have anything for it to point to.
So to make the pointer be useful, you need to declare a variable, or
an array, and load the pointer with the address of that variable or array.
Code: |
#include <16F690.h>
#fuses INTRC_IO, NOWDT, PUT
#use delay(clock=4M)
#use rs232(baud=9600, UART1, ERRORS)
int8 *FwdDev = 0;
int8 myval;
//=================================
void main()
{
myval = 177;
FwdDev = &myval;
printf("%u \r", *FwdDev);
while(1);
}
|
|
|
|
hmnrobots
Joined: 02 Oct 2009 Posts: 11
|
|
Posted: Thu Apr 11, 2013 11:32 am |
|
|
JUST THANK YOU SO MUCH , at least I've got an answer and I will remember that declaring a pointer doesn't allocate any memory!
Once again Many thanks PCM Programmer to take the time to answer to such a basic question. |
|
|
|