tags:

views:

113

answers:

5

How do I know what a particular address pointed by my pointer contains? I want to print the content in this address location? What should I add as a placeholder?

+2  A: 

Any other pointer type can be cast to a void pointer to print its address:

char c='A';
char *pc=&c;

printf("%p",(void *) pc);

Now, if you want the character at this address:

char c='A';
char *pc=&c;

printf("%c",*pc);
Michael Goldshteyn
+3  A: 

If your question is - how can you deduce the type of the object stored at a given location denoted by void*, then you can't. If your question is how can I know the value of the byte my void* points to, then it is

unsigned char valueAtAddressP = *((unsigned char*)p);
Armen Tsirunyan
Should be `unsigned char` to be portable to pathological systems with ones complement or sign/magnitude and a signed plain-`char` type. (On these systems, the distinction between 0x00/0xFF or 0x00/0x80 would be collapsed away by using plain `char`.)
R..
@R.. Editing. Actually I wasn't sure there was unsigned char in C :)
Armen Tsirunyan
+1  A: 

You can do it in multiple ways

int x = 5;
int *ptrX = 6;
printf("The pointer of x is: %d", &x);
printf("The ptrX is: %d", ptrX);

Both of the above approaches give you the address value.

Ravi Gummadi
That will print nothing but the string in quotes...
Michael Goldshteyn
+3  A: 

There is no way to detect that in C. Your application should treat the data pointed to by the pointer to a specific type - depends on your needs.

void treat_as_char(const void* ptr)
{
    const char *p = ptr;
    printf("Data: %s\n", p);
}

void treat_as_int(const void* ptr)
{
    const int *p = ptr;
    printf("Data (int): %d\n", p);
}

You need some custom structure to allow various type for a specific pointer:

typedef struct _Variant
{
    enum {INT, CHAR, DOUBLE} type;
    void *ptr;
} Variant;
Donotalo
+2  A: 

Another way you could do it is to refer to your data as a void* and then refer to that as an unsigned char*, and print everything out as hexadecimal bytes. I found it was really useful when having to deal with contiguous bytes where their use was not yet known.

However, this method requires that you have a way of knowing how long your data is, as there's no way to call sizeof on data of arbitrary type.

#include <stdio.h>

int main() {
    char name[16] = "hello there";
    void* voidBuffer = name;

    unsigned char* buffer = voidBuffer;
    int i;
    for (i=0; i<16; i++) {
        printf("%x ", (unsigned int)buffer[i]);
    }

    return 0;
}

This would output 68 65 6c 6c 6f 20 74 68 65 72 65 0 0 0 0 0.

Jengerer
It seems you are trying to print out the ASCII code in that address, which are not the questions asked.
antonio081014
@antonio081014: It isn't obvious, at least for me, what -are- the questions asked
Armen Tsirunyan
+1 When the type of an object is not known, the best you can do is print out the value as a hex dump. But you have to be careful not to access addresses that may be out of bounds.
Ziffusion
@antonio081014: with `char` it works as ASCII, but even with something like an `int`, you'll get the hexadecimal representation of it.
Jengerer
@Jengerer: I think those numbers which are printed out are still the corresponding ASCII code. right? I am not quite sure. If you know more, please explain in detail. Thank you very much.
antonio081014
@antonio081014: Yes, you're right. What I meant was that, with `char` it prints out the corresponding ASCII code, but if, for instance, I chose to print out an `int` that was set to the maximum value, it would print out: `ff ff ff ff` because that's the hexadecimal representation of that int.
Jengerer