-3
int main(int argc, char *argv[])
{ 
    int *p=5,**P;
    printf("%d",&P);
}

What is the difference between *p and **P?

Richard
  • 56,349
  • 34
  • 180
  • 251
vishwesh_1
  • 11
  • 2
  • do you use `p` lower case and `P` upper case? Yeah, that's readable and not confusing at all... – bolov Jun 15 '17 at 21:22
  • If you want to declare a pointer to an int variable of value 5 you should write `int x = 5; int *px = &x;`. Then you can print that memory address using the correct format specifier `printf("%p", px);`. – Bob__ Jun 15 '17 at 21:32
  • With `int *p = 5` you declare a variable `p` of type `int *` (or pointer to int) and assign to p the memory address `5`, whatever it contains (probably not an int), not the address of a variable of type int whith value 5. This is a [bad idea](https://ideone.com/2H0adX). – Bob__ Jun 15 '17 at 21:44

3 Answers3

1

*p is a pointer which points to an int that is equal to 5

**P is a pointer to A pointer;it is a variable that contains an address.

A pointer is a variable that contains an address. In your PC every variable is stored in a certain place in its memory. The exact place where a variable is placed is called variable's address.

With a pointer you are able to know the exact address of another variable.

Example

int c = 5; // this value of this int is stored at a certain address; 
int *p = &c; // the pointer p now contains the address where 5 

Keep in mind that *p is a variable too and as such is stored somewhere in the memory as well.

int **P = &p ; // a double pointer that contains the address of the pointer p

this will be a new pointer that points to the address where p is stored ( not the variable c!) –a pointer;

Eduard6421
  • 295
  • 1
  • 11
0

A single asterisks represents a pointer whereas a double asterisks represents a pointer to a pointer.

gavsta707
  • 365
  • 3
  • 16
0

*p is an address to an int that equals 5. So somewhere in memory, there is an int that equals 5 and that pointer points to that address of that int.

**p is an address to a pointer in memory. So think of it that we have a pointer pointing to the int above, but now on top of that, we have another pointer pointing to the pointer for the int. Another way to think of it is we have an address for another address that's for the int.

  • ok thanks.....so if we have pointer **a and a=5 then we have to write &a to get that value?? – vishwesh_1 Jun 15 '17 at 18:32
  • Actually, that will only give us the address when we use & in front of a variable. In order to dereference a pointer, we have to use * still to do so, but doing it once will still give us the pointer (the address). We are going to have to dereference it again but that's not how we use double pointers anyways. Normally we use a double pointer to pass a regular pointer to a function so we keep the true pointer because it would be copied otherwise. This allows us to work with that pointer without losing the original pointer. I'll post a link in a sec to show what I mean there. – Ahmad Fawaz Jun 15 '17 at 18:44
  • 1
    Ok this has a simple way of explaining double pointers and their uses: [link](https://stackoverflow.com/questions/5580761/why-use-double-pointer-or-why-use-pointers-to-pointers) – Ahmad Fawaz Jun 15 '17 at 18:50