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

long int & pointer

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



Joined: 24 Jun 2004
Posts: 3

View user's profile Send private message

long int & pointer
PostPosted: Wed Aug 11, 2004 7:32 am     Reply with quote

unsigned long temp;
byte *ptr;
ptr=&temp;

in this code, does ptr show high byte of "temp"? or low byte?
valemike
Guest







"Little Endian"
PostPosted: Wed Aug 11, 2004 10:14 am     Reply with quote

For want of a descriptive term, it's stored as "Little Endian", i.e. LSByte first.

Code:

#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)

void main(void)
{
   unsigned long mike;
   unsigned char *byte_p;
   
   mike = 0x1234;
   byte_p = &mike;
   
   while(1);
}


I simulated it in MPLAB, and found:
address of "mike": 0x06
So the contents of byte_p is 0x06

The contents in RAM:
0x06: 0x34
0x07: 0x12

So if you cast byte_p to mike, you get 0x34.
If you cast byte_p to (mike + 1), you get 0x12.

-Mike

p.s. This is usually encountered in programming quizes during job interviews. They ask you, "How can you tell if the processor is using Big Endian or Little Endian?" The answer is to make an int, assign 0x12345678 to it, then cast a char pointer to the int. If you read back 0x12, then it's BIG endian, otherwise it's Little endian.
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