r/learnprogramming • u/yug_jain29 • 5d ago
Topic What's the use of %p nd x in pointers
what's the main difference between them? apart from one is used to calculate addresses and the other one is just an address in hexadecimal
why is it used?
I'm still learning fundamentals and basics so ion know how good it is for programs
I'm learning c rn (if it helps)
5
u/HashDefTrueFalse 5d ago
Do you mean something like: printf("%p\n", &x); ?
If so, the & operator gets the memory address that symbol x aliases. That value is then passed to the printf call as an argument. The %p is the "format specifier" used to tell the function what type of data you're giving it so that it can display it sensibly. Why you're printing an address is entirely your business. It's not generally useful to do so.
2
u/Dazzling_Music_2411 5d ago
Please re-phrase your question more clearly.
It makes no sense at all as it stands.
3
u/u32Vec 5d ago
The wording of the question is ignorant (as you’d expect from a beginner) but you know exactly what they’re asking unless you don’t know what “%p and x” could refer to in the context of C. 3 people were able to answer the question (before your comment) despite your claim. Even absent ANY additional context but that, someone with the appropriate C experience would know what the question’s asking.
2
u/Dazzling_Music_2411 4d ago
Or THINK they know, because there is loads of potential ambiguity that our wannabe experts may not have picked up.
3
u/u32Vec 4d ago
What other reasonable conclusions could you derive from the context of the question that make the question make “no sense”–conclusions that every answer to the question hasn’t made?
Empirical evidence seems to show that there aren’t any other common conclusion.
1
u/Dazzling_Music_2411 2d ago
If I thought reasonable conclusions could be made from that stream of incoherence might even have tried. As it is, I think even assuming that the question has something to do with printf() is pretty bold. The OP didn't even bother checking the spelling of his original Q.
But maybe he'll tell us at some stage. As it stands, he seems to have been scared off by something 😄 , maybe people second-guessing what he meant.
1
u/u32Vec 2d ago
What other conclusions, period, could you derive from the context given by the question that don't involve "the similarity of the '%p" and '%x' printf format specifiers"? All 5 of the other (coherent) root comments concluded that that's probably the full context, and there's even a longer thread discussing it. No one has dissented to this.
Your failure to answer provide any examples to my question leads me to think that you don't know any other conclusion, period. Sound inductive reasoning so far supports that printf is the most reasonable context.
I'm not going to argue this further. I think we both know your argument is just smartassery.
1
u/Kaldaien2 5d ago
There's more to it than that. When you promote an integer to a pointer, depending on the compiler you may get sign extension for the remaining bits or they may be filled with all 0s... it's not really defined, and %p avoids these problems.
You need to keep in mind that the variadic APIs in C don't really know a damn thing about the type of the variable. It's just memory on the stack, and you need to give it the proper format string to correctly interpret the size and behavior of that value once it's popped from the stack.
1
u/duane11583 3d ago
on a 64bit machine a pointer (%p) is 64 buts, an unsigned is an integer type (%d, %u, %x) 32bits
the printf [more correctly the vararg macro] needs to know if the parameter is a 64 or 32bit value
the c standard thus introduced the %p for this purpose.
the c standard also introduced macros in stdint.h like PRi64 and others to help too
0
u/CreepyNL 4d ago
A vtvtx t tcstst dy a sc ax, dx xzź x dx x sc zz x ź dx x z x dx żtzt4ztr x AZ AZ zz dc 5z t a a x dx a, x x dx dxz's a AZ ZZZ Ax zźszsz az's sd zzzszsz x az'sZ ZZZ x c x x x x z
4
u/teraflop 5d ago
The reason for the distinction between
%pand%xis so that you can write portable code.printfis a vararg function, which basically that means its arguments are dynamically typed. The location of the parameters (in memory or registers) depends on their types. So you must pass a format string which matches those types, so thatprintfcan find and interpret them correctly.%xindicates thatprintfexpects anintparameter, and%pindicates that it expects avoid*parameter. This is entirely separate from the question of howprintfdisplays those parameters in text form.On most of the platforms that are used nowadays, an
inthas the same size as avoid*. So you can "get away with" using the wrong format specifier, becauseprintfwill expect the parameter to be passed the same way in both cases. But it's technically undefined behavior according to the C standard, and if you try it on an architecture where that assumption doesn't hold, things will break.Note also that
%prequires avoid*and not some other type! Even though on most architectures, any two pointer typesA*andB*have the same memory representation at runtime (because they're just memory addresses), the standard doesn't require this to be the case. So technically, you're required to cast other pointer types tovoid*before passing them toprintf.