1

I was trying to print the address of the pointer variable not the address where it is pointing to, could anyone assist me in achieving that? Below is what I am trying but it is showing warning which i am not able to resolve. Thanks!

#include <stdio.h>
#include <stdlib.h>

int main()
{
    int* y;
    printf("%p\n",y);
    printf("%x\n",&y);
    y = (int*)malloc(sizeof(int));
    printf("%p\n",y);
    printf("%x\n",&y);
    return 0;
}

Compilation warning:

Warning: format ‘%x’ expects argument of type ‘unsigned int’,
    but argument 2 has type ‘int **’ 

Output:
0xb773fff4
bfa3594c
0x8361008
bfa3594c
4
  • 2
    You took its address with & correctly -- the printf hints are just hints Commented Dec 6, 2012 at 3:48
  • Both y and &y are pointer values. Why are you using %p to print one pointer value, but suddenly switch to %x for another pointer value? All of your printf statements are printing pointers, which means that all of them should use %p. Commented Dec 6, 2012 at 3:59
  • 2
    Also, I find it hard to believe that the output you posted was produced by the code you posted. You posted either wrong code, or wrong output. Commented Dec 6, 2012 at 4:03
  • @AndreyT Sorry, you got it right I have to edit the output, I was trying too much with %x and %p to print the address of the pointer variable itself, and somewhere in between posted the code in hurry I am correcting it. Appreciate your assistance! Commented Dec 6, 2012 at 4:14

2 Answers 2

4

Your second printf() should take a "%p\n" format, and strictly a cast:

printf("%p\n", (void *)&y);

The number of machines where the cast actually changes anything is rather limited.

Sign up to request clarification or add additional context in comments.

2 Comments

As a comment, %x is unsigned int, and for example on 64bit machines, this will not be same as %p. As Jonathan said, always %p for pointers
I'm glad AndreyT asked you about the output because the original output didn't make much sense. Unfortunately, I was helping my kids with homework at the same time and didn't get to raise the question. Be aware that the format for %p varies between platforms. If you want to control the pointer format (I usually do), then use <stdint.h> and printf("0x%.8" PRIXPTR "\n", (muintptr_t)&y);. This uses string concatenation and the macros defined in the header to format the address with at least 8 digits and with upper-case hex digits and the 0x prefix. Not all platforms add the 0x prefix.
-1

The code seems to compile without warning or error on Visual Studio 2012.

    #include <stdio.h>
    #include <stdlib.h>

    int _tmain(int argc, _TCHAR* argv[])
    {
        int* y = 0;
        printf("%p\n",y);
        printf("%x",&y);
        y = (int*)malloc(sizeof(int));
        printf("%p\n", y);
        printf("%x",  &y);

        return 0;
    }

The only recommendation is that you initialize y when you declare it.

1 Comment

I think your answer is a sign that Visual Studio is broken, not that the code is fine. Perhaps you need to turn on more warnings - the warning is valid!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.