-2

When trying to print a pointer, for example

#include <stdio.h>

main() {
    int a = 3;
    printf("The address : %p\n", &a);
    printf("As an hex   : %x\n", &a);
    return 0;
}

I get a 000000ac4f5ffd58 and the nice 4f5ffd58 (the first two characters mismatch is from unknown origin). It's just for comfort and beauty, the %x "works" to display it. Is there any way to have the correct (0x4f5ffd58) way to print my pointer (its format) ?

I'm using CLion (I resetted the settings), and here is the CMakeLists.txt associated with it :

cmake_minimum_required(VERSION 3.22)
project(untitled C)
set(CMAKE_C_STANDARD 99)
add_executable(untitled main.c)
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
DiraD
  • 15
  • 4
  • Great! Sooo why are you writing here? Is there a question you want to ask? Please see [ask] – KamilCuk Aug 12 '22 at 17:09
  • 2
    `%x` print integers. Apparently, in your environment, pointers are larger than integers, so your program is undefined -- you just were lucky to print something remotely useful. In another system you might print the upper part, `ac`... – Lindydancer Aug 12 '22 at 17:11
  • 2
    In this case "works" is same as "invokes undefined behaviour that apprently seem to create some useful result". Using format specifier `%x` for pointers is just illegal. Using `int*` as parameter for format specifier `%p` does the same. You must cast to `void*` – Gerhardh Aug 12 '22 at 17:12
  • Why do you think it "works"? It's printing a different value because the first 2 digits are missing. It's printing the low-order 32 bits of the pointer value, but there are more bits in the pointer. – Barmar Aug 12 '22 at 17:13
  • 1
    `Is there any way to have the correct way to print my pointer` Please define exactly "correct". For correctness as in defined behavior according to C programming standard, you should be using `"%p", (void*)&a)`, which will result in implementation-defined representation. The result you are getting tho - `000000ac4f5ffd58` - looks super very "correct". You want to print the value of the pointer in hex excluding leading zeros? – KamilCuk Aug 12 '22 at 17:15
  • Write your own function that formats pointers. – n. m. could be an AI Aug 12 '22 at 17:18
  • The "correct" way I was thinking of it the `0x4f5ffd58`. – DiraD Aug 12 '22 at 17:20

3 Answers3

1

The "correct" way I was thinking of it the 0x4f5ffd58.

What is the "correct" part of it? The length? If you're on a 64bit system, a 32bit value is by no means "correct".

If you're looking for the 0x intro, you might want to try the "alternative" format (printf("The address : %#p\n", (void *)&a);). What exactly the standard and the alternative format might be is implementation-defined, but usually the alternative form provides the 0x intro.

DevSolar
  • 67,862
  • 21
  • 134
  • 209
0

You are using the conversion specifier x designed to output objects of the type unsigned int. Using it with a pointer invokes undefined behavior. Instead you could write for example

#include <stdio.h>
#include <inttypes.h>

int main( void ) 
{
    int a = 3;

    printf( "The address : %p\n", ( void * )&a );
    printf( "As an hex   : %#" PRIxPTR "\n", ( uintptr_t )&a );

    return 0;
}

The program output might look like

The address : 0x7ffe9bf5f9b8
As an hex   : 0x7ffe9bf5f9b8

Also bear in mind that according to the C Standard (7.21.6.1 The fprintf function)

p The argument shall be a pointer to void. The value of the pointer is converted to a sequence of printing characters, in an implementation-defined manner.

And pay attention to that according to the C Standard the function main without parameters shall be declared like

int main( void )
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • Only the second `printf` actually print the `0x` prefix on my machine. – DiraD Aug 12 '22 at 17:37
  • 1
    @DiraD When the conversion specifier p is used then the output has an implementation-defined form. – Vlad from Moscow Aug 12 '22 at 17:50
  • Do you know how I did (it the problem came from between the keyboard and the chair) to modify this form ? I can't recall doing that, but is there any way to transform it back ? – DiraD Aug 12 '22 at 17:59
  • @DiraD I have not understood what you mean. If you need the prefix 0x when use the second call of printf shown in my demonstrative program. – Vlad from Moscow Aug 12 '22 at 18:01
  • It's more about "having the right display with the default call" than just having a "something that works" – DiraD Aug 12 '22 at 21:20
  • @DiraD What is the "right display with the default call"?! I showed you correct calls of printf using a pointer as an argument. – Vlad from Moscow Aug 12 '22 at 21:23
0

Seems the problem was in my expectation. In every C tutorial, the pointers is printed with the format 0x4f5ffd58, but of course, as I redid every calculation now, 8*4 only makes a 32-bit address, where my system is a 64-bit, thus the longer displayed address (which confused me because I was first printing with the 0x prefix, but no more, maybe due to an IDE update).

DiraD
  • 15
  • 4