Go Premium for a chance to win a PS4. Enter to Win


pointers, memory, simple stuff

Posted on 2004-04-07
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?
Question by:phavardel
LVL 12

Accepted Solution

stefan73 earned 100 total points
ID: 10775964
Hi phavardel,
> i get a bus error sometimes
On many architectures, you cannot access data on odd addresses. The alignment must be the same as data size. SPARC is an example. Intel allows this, but you need additional CPU cycles.

What system do you use? Check that sizeof(int)==sizeof(int*).

LVL 12

Expert Comment

ID: 10775991
> 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.

LVL 12

Expert Comment

ID: 10776012
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 12

Expert Comment

ID: 10776755
... or cast the int* to a char*, and _then_ increment it - they you'll be looking at individual bytes.

Assisted Solution

idt earned 100 total points
ID: 10777495
#include <stdio.h>

char* toBin(char c) {
  static char out[9];
  int i;
  for (i=0;i<8;i++) {
    if((c << i)& 0x80)
  return out;

int main (char argc, char **argv) {
  int a,i;
  char* c;

  if(argc==2) a=atoi(argv[1]);
  else a=257;
  for(i=0;i<sizeof(int);i++) {
    printf("byte %u is %s\n",i,toBin(c[i]));

This toy should demostrate the memory layout of an integer, left to right.


Expert Comment

ID: 10782224
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

Author Comment

ID: 10785335
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

Expert Comment

ID: 10785595
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.


Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
Suggested Courses

824 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question