format ľ%d' expects argument of type ěint', but argument 2 has type ěchar *' - What is it? How to tidy up?

Asked

Viewed 3,123 times

11

Code:

#include <stdio.h>

int main(void) {
        char caractere, *caractere_ptr = &caractere;

        printf("Caractere: foi alocado %zu byte\n", sizeof(caractere));
        printf("Endereço: %d", caractere_ptr);

        return 0;
}

Error being displayed:

warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat=]
  printf("Endereço: %d", caractere_ptr);

What I’d like to be shown is memory address variable, not its value. Therefore, I did not initialize it.

3 answers

8


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 is all on the table that I Linkei. I edited the answer to show what you really want.

  • Yeah, the guy at Cprogressivo showed that to print the memory addresses, it would take %d...

  • @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.

  • I did not initialize it, because I did not intend to display its value, but rather its address.

  • @Dracalbus there makes some sense even, at least in a trial.

  • @Dracalbus this might interest you: http://answall.com/q/178443/101

Show 2 more comments

7

The %d is for printing whole numbers.

To print characters use the %c

As stated in the comment the intention is to print the entire value of the memory address of *caractere_ptr.

As stated by Maniero’s reply, the correct is to use the %p

Here I leave some printing parameters that I removed from here.

Código  Conversão/Formato do argumento
%d  Número decimal inteiro (int). Também pode ser usado %i como equivalente a %d.
%u  Número decimal natural (unsigned int), ou seja, sem sinal.
%o  Número inteiro representado na base octal. Exemplo: 41367 (corresponde ao decimal 17143).
%x  Número inteiro representado na base hexadecimal. Exemplo: 42f7 (corresponde ao decimal 17143). Se usarmos %X, as letras serão maiúsculas: 42F7.
%X  Hexadecimal com letras maiúsculas
%f  Número decimal de ponto flutuante. No caso da função printf, devido às conversões implícitas da linguagem C, serve tanto para float como para double. No caso da função scanf, %f serve para float e %lf serve para double.
%e  Número em notação científica, por exemplo 5.97e-12. Podemos usar %E para exibir o E maiúsculo (5.97E-12).
%E  Número em notação científica com o "e"maiúsculo
%g  Escolhe automaticamente o mais apropriado entre %f e %e. Novamente, podemos usar %G para escolher entre %f e %E.
%p  Ponteiro: exibe o endereço de memória do ponteiro em notação hexadecimal.
%c  Caractere: imprime o caractere que tem o código ASCII correspondente ao valor dado.
%s  Sequência de caracteres (string, em inglês).
%%  Imprime um %
  • But that’s exactly what I want to do, print an entire number, which in this case is the memory address...

  • I understand, you could have made the question explicit. Can you edit it now and put your question? I’m going to edit here and put the answer to this, so I’m going to be consistent with anyone who ends up here in the future.

  • 1

    I edited the question in order to make it easier for those with the same problem.

6

%d is to show inteiros

If you want to show char, you should use %s

  • I don’t want to show char, I want to show an integer, which is the memory address of the variable.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.