Take the 2-minute tour ×

I am trying to print the value pointed to by an address but the problem is I need to dereference this pointer based on the size that is passed to me. So something of this sort:

void print(Address addr, Int size) {

I am a little confused on how to achieve this. Can someone point me in the right direction?

EDIT: Ok so I'm thinking:

char p[80];
memset(p, '\0', 80);
memcpy(p, addr, size);

And then dereference as *p. If there is a better way or a correct way, please let me know

share|improve this question
Can you elaborate a little - do you mean that sometimes you want to dereference the pointer as a 32-bit int (say if size==4) and sometimes as a 64-bit int (if size==8)? Or are you after something else? –  Michael Burr Oct 15 '10 at 4:49
@Michael: That's right. I am trying to print the hex representation of these numbers. –  Legend Oct 15 '10 at 4:55
add comment

3 Answers

up vote 3 down vote accepted

Your question is very unclear. If you mean you want to dump arbitrary binary data from the address passed, you want something like:

void print(const unsigned char *addr, size_t size)
    while (size--) printf("%.2x", *addr++);

Or if you mean you want to print character data that's not null-terminated, try:

void print(const char *addr, int size)
    printf("%.*s", size, addr);
share|improve this answer
I added the const qualifiers to the char * arguments. Since the functions do not (and should not) modify the targets of the pointer arguments, we can let the compiler's optimizer and error-checking know a little more about what's going on inside. –  Andy Lester Oct 15 '10 at 5:26
Thanks for the fix. –  R.. Oct 15 '10 at 5:44
add comment

You may use a switch statement in the routine:

switch (size) {
   case 1: print( *(char*)pointer); break;
   case 2: print( *(short*)pointer); break;
   case 4: print( *(long *)pointer); break;
   case 8: print( *(__int64*)pointer); break;
   default: puts( "unsupported);

("print" is a routine to print integers of various sizes, perhaps using C++ polymorphism)

A sophisticated solution is to use a C++ template e.g. using a parameter type Integer which is subsituted by the actual variable type upon invocation. This routine may then act on the size of this Integer variable (which may vary from 1 to 8, or more if you implement longer integers yourself). This disadvantage of this approach is that the compiler generates a routine instance for every type of argument, and logic dependent on the size will lead to "always true" conditions for some tests. An easy way out is to use always the longest type of integer you'd expect.

share|improve this answer
The OP has tagged this as C not C++ so I guess that rules out polymorphism or overloading print - but the principal behind your answer is OK. –  Andrew Oct 26 '12 at 9:02
add comment

If it is a number, which I assume it is, you will need something like this:

int n=0;
if (size>sizeof(int)) { return; //int is too small };
  ((char*)(&n))[sizeof(int)-(i+1)] = ((char*)addr)[size-(i+1)];
share|improve this answer
This code at best has implementation-defined behavior, and more likely undefined behavior... –  R.. Oct 15 '10 at 5:08
does it really, why? –  Alexander Rafferty Oct 15 '10 at 5:12
If size is less than sizeof(int), part of the representation of n remains uninitialized. Also whether you should copy to the beginning or end is implementation-defined (endian dependent). –  R.. Oct 15 '10 at 5:46
add comment

Your Answer


