View previous topic :: View next topic |
Author |
Message |
Freud Guest
|
Pb with Structure |
Posted: Thu Apr 06, 2006 8:57 am |
|
|
Hello,
I've switched a projet from PIC18F chip to LPC Arm chip and I found a problem with structure in C and pointers. I don't know if I've misunderstood something in C or ... so I would like to know what you think about :
I would like to copy some data (unsigned char) from a tab to a struct like this :
ptr1 = (void*)&SFile2.attr;
ptr2 = pb + 11;
for(i=0;i<21;i++)
{
*ptr1++ = *ptr2;
UART_SendByte(*ptr2);
ptr2++;
}
Sfile2 is a struct like this :
typedef struct File2
{
u08 attr;
u08 lcase;
u08 ctime_ms;
u16 ctime;
u16 cdate;
u16 adate;
u16 ClusterHigh;
u16 time;
u16 date;
u16 ClusterLow;
u32 FileSize;
}FILE_Layout_2;
u08 = unsigned char // 8 bits
u16 = same // 16 bits etc ...
Ptr2 is on a buffer (data source) and I want to copy 21 data which are aligned from my buffer into the struct. So Ptr2 pointer is on the first member (attr) and I loop. UART show all the data from buffer are correct, but when I read the data copied in the struct, some are bad. I checked on the listing file the members are right aligned in the address space. If I copy the date in each member, it's work but this means add 21 lines for each one.
For PIC code no pb, but with ARM GNU it doesn't work. Do you think it's an error from my code (C programming skills) or a bug ?
Thank you in advance !
David
PS : sorry for my bad english ... ;-) |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
Ttelmah Guest
|
|
Posted: Thu Apr 06, 2006 9:30 am |
|
|
This is a CCS (PIC) C forum, so the post is really off topic. You would be better asking the question in a group concerning the ARM C...
However there are some comments:
Use of (void *) for a pointer, is not legitimate basic 'C'. It is a 'C++' construct, and if it works in CCS C, this is a 'bonus', but don't expect it to work in a C in general. Since you want to work in bytes, declare the pointer as a pointer to an unsigned character. This may be causing problems...
You don't show what 'pb' is (which ptr2 points to + 11)?.
Why fiddle around taking the address of the first byte in the structure?. As with an array, the name of a structure _is_ it's address. So:
ptr1 = (unsigned char *)SFile2;
Is an easier, and safer construct for standard C.
Best Wishes |
|
|
Freud Guest
|
|
Posted: Thu Apr 06, 2006 10:05 am |
|
|
OK, thank you
Pb is a u08* ptr.
Sorry about OFF-Topic, please forget my first post, I reformulate my question
If you have to copy row data from a tab (unsigned char BUFFER[21]) to a struct data like Sfile2 (size = 21 bytes) with a single loop, how would you do it ?
Thank you for your tips
(The answer would be : get a C book ). |
|
|
Ttelmah Guest
|
|
Posted: Thu Apr 06, 2006 2:08 pm |
|
|
Do the pointer with the form I suggested, and directly access the array.
So:
Code: |
char * ptr;
int8 ctr;
ptr = (char *)Sfile2;
for (ctr=11;ctr<32;ctr++) *(ptr++)=pb[ctr];
|
Best Wishes |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Thu Apr 06, 2006 6:31 pm |
|
|
Pic is an 8 bit processor. An ARM7 is a 32 bit processor. When accessing each byte in the struct, you will encounter "padding" bytes. Take a look at the sizeof you struct. |
|
|
|