Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Two pointers on the same adress.

Status
Not open for further replies.

m_t_blind

Newbie level 5
Joined
Jul 31, 2005
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,376
I have two pointers, one int and one char type :

int *p1;
char *p2;

I do:

p2 = (char*)p1

to get p1 data in bytes.

If there are data in (8bit)adresses (p1)=0x10, (p1+1)=0x12,(p1+2)=0x14.

Can read this data in byte form later whith the p2 pointer ?
 

That usually works fine.

Remember that char and int can have different bit widths on different systems. That's important if you want to create portable code.
 

m_t_blind said:
I have two pointers, one int and one char type :

int *p1;
char *p2;

I do:

p2 = (char*)p1

to get p1 data in bytes.

If there are data in (8bit)adresses (p1)=0x10, (p1+1)=0x12,(p1+2)=0x14.

Can read this data in byte form later whith the p2 pointer ?


p1 should point a valid memory location. If thats is the program works fine. Otherwise it will crash.
*p1 = 0x10, *(p+1)=0x12, *(p1+2)=0x14 is correct.

p1 = 0x10 changes the address pointed by p1 to 10 but not the data.

Cheers
 

Yes it will work fine but take care of the memory organization little endian or big endian.

regards,
Amr.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top