To print the memory address you must use the %p
. In this way:
#include <stdio.h>
int main(void) {
char caractere, *caractere_ptr = &caractere;
printf("Caractere: foi alocado %zu byte\n", sizeof(caractere));
printf("Endereço: %p", caractere_ptr);
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
I will keep the original answer below for reference because it is still useful to some people, but it is not what the author of the question desired, and what was said is part of an assumption that now turns out wrong.
But if you want to make it work properly you must use %s
.
There’s a code table which can be used in a string of formatting the printf()
.
In this case you have a pointer to character (char *
) for a character, therefore it is a string that you want to print, so the correct is %s
. If that were the character it would be %c
. Hopefully he won’t print anything, but he can print a lot of unwanted things because when he prints a string it only stops when it finds a null character \0
, and can have a lot of dirt until you find a null. I speak of this in What is the difference between "NULL", "0" and 0?
Note that what will print is garbage, since the variable caractere
was not initialized.
I just want to print the memory address... If memory addresses are integer numbers, wouldn’t it be appropriate to use %d? Why %s?
– Dracalbus
@Dracalbus is all on the table that I Linkei. I edited the answer to show what you really want.
– Maniero
Yeah, the guy at Cprogressivo showed that to print the memory addresses, it would take %d...
– Dracalbus
@Dracalbus, I don’t know what this Cprogressive is, but it’s not normal. I know it has compiler that accepts, but it is out of pattern, so it is better to learn for sure. Programming is not seeing working, it is doing the right thing. All C-quality literature clearly states which pointers should be printed with
%p
.– Maniero
I did not initialize it, because I did not intend to display its value, but rather its address.
– Dracalbus
@Dracalbus there makes some sense even, at least in a trial.
– Maniero
@Dracalbus this might interest you: http://answall.com/q/178443/101
– Maniero