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

Pb with Structure

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







Pb with Structure
PostPosted: Thu Apr 06, 2006 8:57 am     Reply with quote

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

View user's profile Send private message

PostPosted: Thu Apr 06, 2006 9:27 am     Reply with quote

LPC ARM forum:
http://www.sparkfun.com/cgi-bin/phpbb/viewforum.php?f=11
Ttelmah
Guest







PostPosted: Thu Apr 06, 2006 9:30 am     Reply with quote

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







PostPosted: Thu Apr 06, 2006 10:05 am     Reply with quote

OK, thank you Embarassed Embarassed

Pb is a u08* ptr.

Sorry about OFF-Topic, please forget my first post, I reformulate my question Smile

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 ? Confused

Thank you for your tips Smile

(The answer would be : get a C book Twisted Evil ).
Ttelmah
Guest







PostPosted: Thu Apr 06, 2006 2:08 pm     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Thu Apr 06, 2006 6:31 pm     Reply with quote

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.
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