Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

warning: format ‘%d’ expects argument of type ‘int’, but argument 2 has type ‘int **’ [-Wformat=]

+4 votes
asked Nov 13, 2021 by GEE DEE ZHE (160 points)
#include <stdio.h>
int main()
{
    int c, *pc, **ppc; c = 12;
    pc = &c; ppc = &pc;

    printf("pc = %d, the value pc points to = %d\n", pc, *pc );
    printf("ppc = %d, the value ppc points to = %d\n", ppc, **ppc);
}

i'm not really understand what does it mean on the warn problem

can someone explain to me how pointer is working i'll appreciate a lot to him/her

1 Answer

+2 votes
answered Nov 13, 2021 by Peter Minarik (86,740 points)

Your code correctly:

#include <stdio.h>

int main()
{
    int c = 12;
    int * pc = &c;
    int ** ppc = &pc;

    printf("pc = %p, the value pc points to = %d\n", pc, *pc);
    printf("ppc = %p, the value ppc points to = %d\n", ppc, **ppc);
    
    return 0;
}

In the first printf() call notice that I changed the first format to %p, that is, to print the value as a memory address and I left the second format to be %d, that is, to print the value (int). Accordingly, the first argument after the format string is pc, which is a pointer to int (int *). A memory address. The second argument is the value of pc (*pc) using the dereference operator to get the value where the memory address points to, that is, to the int value. This is the value stored in c.

Similarly, in the second printf(), first we want a memory address to be printed (ppc), and after that, we want to dereference twice as ppc is a pointer that points to another pointer that points to a number (int**). So **ppc has the same value as c.

I hope this helps.

Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...