CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Pointer problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Bertrand
Guest







Pointer problem
PostPosted: Thu Jul 06, 2006 2:03 am     Reply with quote

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







PostPosted: Thu Jul 06, 2006 7:04 am     Reply with quote

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

View user's profile Send private message

Re: Pointer problem
PostPosted: Thu Jul 06, 2006 9:00 am     Reply with quote

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







PostPosted: Thu Jul 06, 2006 9:06 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Jul 06, 2006 9:44 pm     Reply with quote

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







PostPosted: Fri Jul 07, 2006 6:19 am     Reply with quote

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







PostPosted: Fri Jul 07, 2006 8:09 am     Reply with quote

Lesson is 'look at everything, even if you think it is right'. Smile
Glad you have found it.

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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