0
#include <stdio.h>
int main(){
    int integer = 300;
    int* p = &integer;
    int** pp = &p;
    printf("*pp = %i", *pp);
}

My question is what is actually *pp? What does it mean to print the value in the address of *p ?

Sayem Rahman
  • 91
  • 1
  • 10
  • 2
    A pointer is nothing more than a sort on integer value. `p` points to `integer`, therefore `*p` is the same value as `integer` . `pp` points to `p`, therefore `*pp` is the same as `p`. – Jabberwocky Oct 08 '21 at 10:19
  • @Jabberwocky: [Pointers are not “nothing more than a sort \[of\] integer value.”](https://stackoverflow.com/a/11714314/298225) – Eric Postpischil Oct 08 '21 at 10:52
  • @EricPostpischil that's why I wrote "sort on" (should be "sort **of**" BTW, typo). – Jabberwocky Oct 08 '21 at 10:58

3 Answers3

1

It is a reference to the integer converted to an integer value.

To print the reference you should use %p format specifier instead of the %i format specifier.

[pp] -> [p] -> [integer]

If you modify the program you can see the references and how they are chained:

int main()
{
    int integer = 300;
    int* p = &integer;
    int** pp = &p;
    printf("pp = %p *pp = %p\n",(void *)pp, (void *)*pp);
    printf("&integer = %p &p = %p\n", (void *)&integer, (void *)&p);
}
pp =        0x7fffb6adc7d8 *pp =    0x7fffb6adc7e4
&integer =  0x7fffb6adc7e4 &p =     0x7fffb6adc7d8
Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
0___________
  • 60,014
  • 4
  • 34
  • 74
0
#include <stdio.h>
int main(){
    int integer = 300;
    int* p = &integer;
    int** pp = &p;
    printf("**pp=%d\n", **pp);
    printf("*p=%d\n", *p);
    printf("*pp = %p\n", *pp);
    printf("p=%p\n", p);
    printf("&integer=%p\n", &integer);
    printf("&(*p)=%p\n", &(*p));
    printf("pp=%p\n", pp);
    printf("&p=%p\n", &p);
}

Since &(*p) is simply p, yes *pp is the address of *p.
Since %i is integer but what you want to print is pointer. So I changed it to %p.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
Savrona
  • 173
  • 1
  • 10
  • In your question, you wrote: `"Since %i is integer"` -- I interpret this as meaning that `%i` is the correct `printf` format specifier for integers. However, that is not quite correct. The `printf` format specifier `%i` is not for all integer data types, only for the data type `int`. For example, the correct format specifier for the integer data type `long` is `%li`. Writing only `%i` would be wrong for `long`. It may work on some platforms (e.g. Windows), but will fail on others (e.g. Linux). Therefore, you may want to consider changing the general word "integer" to the data type "int". – Andreas Wenzel Oct 08 '21 at 10:51
  • You may also want to elaborate on what your posted code is doing. If you simply paste code without explaining the meaning of that code, then your answer will be harder to understand. – Andreas Wenzel Oct 08 '21 at 11:02
-1

I'm not a pro in C programming, but I believe I have a decent knowledge of pointers. So, to understand what is *pp, you need to understand what is * operator used for?

It is no doubt used for multiplication, but it is also used for pointer de-referencing.

So when I say *pp, it means the value of pp i.e. what is stored in pp. Each defined variable in C will have a memory space of its own. So *pp means what is stored in the memory space that was given to the variable pp.

In your case,

int integer = 300;

A variable of type integer named integer is declared and given the value 300

int* p = &integer;

A pointer that needs to point to an integer variable is declared and given the address of integer.

int** pp = &p;

A pointer that needs to point to an integer variable is declared and given the address of p

printf("*pp = %i", *pp);

*pp means printing the value which is stored in pp i.e. address of p **pp means printing the value which is stored in the value which is stored in p i.e. 300

As I explained earlier, each variable has a memory space of its own, and each memory space will have an address assigned to it, so that it can be identified (the way we have an email address which is used to contact us via email or a postal address which is used to contact us via post).

Hope this answers your question. Do ask follow-up questions if any. Cheers..!!

Gaurav Gilalkar
  • 128
  • 2
  • 13
  • 1
    Re “So when I say `*pp`, it means the value of `pp` i.e. what is stored in `pp`”: No. Do not write this; it will be confusing to students. Per C 2018 6.2.5 20, the value of a pointer provides a reference to some entity. After `int **pp = &p;`, the value of `pp` is the address of `p`, and the value of `*pp` is `p`, not what is stored in `pp`. It could be said to be what is stored **at** `pp`, but that is different. Avoid referring to a pointer as if it were the memory it points to. E.g., do not say “free `pp`” to mean “free the memory that `pp` points to.” – Eric Postpischil Oct 08 '21 at 11:22