We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now


pointers, memory, simple stuff

phavardel asked
Medium Priority
Last Modified: 2010-04-15
This isn't to accomplish anything in particular, just playing w/ stuff, really. Here's the code:

#include "stdio.h"

int main()
        int a = 257;

        int * pa = &a;

        int ar = (int) pa;

        printf("\n%i\n", ar);


        printf("\n%i\n", ar);

        char *pNewPa = (char *) ar;

        printf("%i", *pNewPa);

        return 0;


Basically I just wanted to see if I could separate a primitive's memory into bytes. I start with an integer. I picked 257 because it has a 1 in the lowest and second-to-lowest order bytes, so that way if i was able to isolate the second byte, I would be sure(er) that I had done it than if I picked something under 256 and got a 0.

I first tried just incrementing the pointer: pa++;

Well, it must be smart enough to know that this is a pointer to an integer and (incorrectly) assume that I wanted the next memory space past the integer, so it incremented pa by 4 (4 byte integers). So, I did the little presto-chango there to model the pointer as an int, increment the int by 1, and put then cast that as a character pointer. Basically, I wanted to go from the base addy of the integer a up one BYTE, and get that BYTE - hence incrementing the addy by 1 instead of 4 and casting the result as a char pointer rather than another int.

I would then expect, when I printed the value @ this new location, I would get the value of the second-order byte of the integer a. In the case of 257, this would be 1:

00000000 00000000 00000001 00000001 = 257

but, i don't. i get a bus error sometimes, and sometimes i'll get an integer value, but not 1. I have seen 44, 20, and 5 so far.

what's up?
Watch Question

Top Expert 2004
Unlock this solution and get a sample of our free trial.
(No credit card required)
Top Expert 2004

> 00000000 00000000 00000001 00000001 = 257

This depends on your system's endianess. On Intel systems, this will look like
00000001 00000001 00000000 00000000 = 257

and it also depends on what is in the next byte after the int.

Top Expert 2004

The correct way to get the single bytes within an int is to use shift and and operators:

byte 0: int & 0xff
byte 1: (int>>8) & 0xff
byte 2: (int>>16) & 0xff
byte 3: (int>>24) & 0xff

... or cast the int* to a char*, and _then_ increment it - they you'll be looking at individual bytes.
Unlock this solution and get a sample of our free trial.
(No credit card required)

Hi phavardel,
the result depends on whether ur system is litle endian or big endian. The most significant and least significant bytes would be chenged if endianness of the machine changes.
hence the result is unpredictable unless you know the endianness of your machine


Okay, well, I'm a SPARCer, so I guess I can't address odd bytes.

Instead I've now tried the same code, except incrementing ar by 2. With 257, I would expect to get a 0, but I get a 1 that way. I changed it to 16842753 (2^16), so I would expect to see a 1 in the 3rd order byte but a 0 everywhere else, and I get a 0 then.

what's up now?

and SPARC is big endian

The program that I sent should run just fine on a SUN box
Where I perform the &0x80, the char value has been passed by value, and will be on the stack on the even byte.

Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.