cancel
Showing results for 
Search instead for 
Did you mean: 

Keil and structure pointers

samiassaad
Associate II
Posted on April 06, 2011 at 17:11

Keil and structure pointers

17 REPLIES 17
trevor23
Associate III
Posted on May 17, 2011 at 14:30

What exactly ''does not work''? Does it fail to compile or does it fail to do what you expect at run time?

Andrew Neil
Evangelist II
Posted on May 17, 2011 at 14:30

''it does not work''

 

What, exactly, does not work?

How, exactly, does it fail?

Do you understand the issues of data alignment - and, hence, why compilers often add padding to structures...?

samiassaad
Associate II
Posted on May 17, 2011 at 14:30

I expect  from this line is to cast the sizeof(hdr) bytes from the start of usart_buffer to the structure hdr, but it does not do !

hdr = ( header1*)usart_buffer

or at least give hdr and usart_buffer the same address location , but it does not !

what did I miss there ?

Thank you for your responses .

Posted on May 17, 2011 at 14:30

And how exactly are you testing this, and coming to this conclusion?

int main ()

{

 /* cast buffer to hdr struct */

 hdr = ( header1*)usart_buffer;

 usart_buffer[0] = 0xE5;

 printf(''%02X\n'',hdr->glcd[0]);

}

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Andrew Neil
Evangelist II
Posted on May 17, 2011 at 14:30

''it does not do !''

 

 

How, exactly, do you know that?

What, exactly, does it do?

Again:

Do you understand the issues of data alignment - and, hence, why compilers often add padding to structures...?
samiassaad
Associate II
Posted on May 17, 2011 at 14:30

I use debuger , so I cant see the right result .

samiassaad
Associate II
Posted on May 17, 2011 at 14:30

I checked google for data alignment  nd padding but still confused could you explain please and what is the relation with my problem .

Thank you all .

Andrew Neil
Evangelist II
Posted on May 17, 2011 at 14:30

Why do you have to make it so hard to wring the information out of you?

Just saying ''it doesn't work'' or ''it's wrong'' is virtually useless - it's like saying, ''my car doesn't work - what's wrong with it?''

You need to explain precisely how it's wrong, or how it doesn't work

''I cant see the right result''

So what result, exactly, do you see?

Have you compared the actual result, and the expected result, and thought about the difference - and how you could explain it?

Data Aligbnment refers to the requirement that most processors have for data items bigger than 1 byte to start on certain addresses;

eg, 2-byte objects need to start on an even address, so that they can be fetched in a single operation;

4-byte objects need to start on a 4-byte boundary, so that they can be fetched in a single operation;

But 1-byte objects can start on any arbitrary address.

It should be obvious that this is going to cause issues if you try to mess with pointers where the target objects have different alignment requirements...

Andrew Neil
Evangelist II