|
|
View previous topic :: View next topic |
Author |
Message |
Bertrand Guest
|
Pointer problem |
Posted: Thu Jul 06, 2006 2:03 am |
|
|
Hello,
I've been trying to use pointers with a small buffer but it doesn't work. I've been searching for my error for some hours and I did not find it. May someone have a look at it please ?
I declare a small table this way:
byte pagebuffer[128];
I then pass a pointer to this buffer to a function:
READ_EXT_EEPROM_PAGE(address,&pagebuffer[0]);
Here is the prototype of this function:
void read_ext_eeprom_page(EEPROM_ADDRESS address, BYTE *dataptr)
In the function, I do this (data is a byte, i is from 0 to 127):
dataptr[i] = data;
If, immediatly below that, I do
printf("%X=%X ",data,dataptr[i]);
...I see that the values are equal. Good.
But, when the READ_EXT_EEPROM_PAGE function returns, the content if pagebuffer is not changed.
Does someone has any idea ? I don't have much hair left to pull out...
Bertrand |
|
|
Ttelmah Guest
|
|
Posted: Thu Jul 06, 2006 7:04 am |
|
|
What you have posted, should work. You need to look for other possibilities. For instance is 'i' actually changing as you expect (you don't test this)?. if not, then the same byte could be being changed on every loop. Alternatively, something else below pagebuffer, overwriting the values.
Best Wishes |
|
|
Neutone
Joined: 08 Sep 2003 Posts: 839 Location: Houston
|
Re: Pointer problem |
Posted: Thu Jul 06, 2006 9:00 am |
|
|
Bertrand wrote: |
In the function, I do this (data is a byte, i is from 0 to 127):
dataptr[i] = data;
Bertrand |
I think this will work.
*(dataptr+i) = data; |
|
|
Ttelmah Guest
|
|
Posted: Thu Jul 06, 2006 9:06 am |
|
|
Both should work.
C, makes no real distinction between pointers, and arrays. So if you have an array 'fred', you can access fred[10], and get exactly the same result as *(fred+10).
I must admit I do suspect 'something else'. There is no error checking, so it is perfectly possible that areas outside the required array are being accessed.
Best Wishes |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Jul 06, 2006 9:44 pm |
|
|
I tried to make a test program based upon your description.
It works OK. It displays the following:
Quote: | 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 10 11 12 13 14 15 etc.
|
I tested this with PCH vs. 3.249. If this program doesn't work,
then post your compiler version.
Code: | #include <18F452>
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
void READ_EXT_EEPROM_PAGE(int16 address, int8 *dataptr)
{
int8 data;
int8 i;
data = (int8)address;
i = (int8)address;
dataptr[i] = data;
}
//================================
void main()
{
int8 i;
int16 address;
int8 pagebuffer[128];
// Zero the buffer.
for(i = 0; i < 128; i++)
pagebuffer[i] = 0;
// Fill buffer with data read from eeprom.
for(address = 0; address < 128; address++)
READ_EXT_EEPROM_PAGE(address, &pagebuffer[0]);
// Display the buffer.
for(i = 0; i < 128; i++)
{
printf("%X ", pagebuffer[i]);
}
while(1);
} |
|
|
|
Bertrand Guest
|
|
Posted: Fri Jul 07, 2006 6:19 am |
|
|
Hi all,
Thanks for your replies. Actually, Ttelmah was right... what a shame of me ! There was a problem with the index, i.
It works well now that I have fixed it...
Sorry and thanks a again !
Bertrand |
|
|
Ttelmah Guest
|
|
Posted: Fri Jul 07, 2006 8:09 am |
|
|
Lesson is 'look at everything, even if you think it is right'.
Glad you have found it.
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
|