-4
#include <stdio.h>

int main(int argc, char **argv) {
    // your code goes here
    char *p;
    p = "hello";

    printf("%s",*&*&p);
    return 0;
}

It give same output for p, *&p, *&*&p. how is this possible?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261

2 Answers2

1

p is the pointer pointing to string "hello". &p is address of p , So *&p will be the value at address of p which is again the pointer pointing to string "hello"

Priyansh Goel
  • 2,660
  • 1
  • 13
  • 37
  • ok then if char p= "hello"; printf("%s",p); it prints error but char *p;p="hello"; printf("%s",p); it prints hello – Nishant Kumar Sep 02 '16 at 17:08
  • Because char p means p is a single character and not a string. char *p means that p is a pointer to a string. See http://stackoverflow.com/a/2938924/4183255 for understanding. – Priyansh Goel Sep 02 '16 at 17:12
  • it was really helpful after visualizing. that is what i was looking for. thanks alot. :) – Nishant Kumar Sep 02 '16 at 17:26
0

* means give whatever is at this location in memory.
& gives the memory address of this object.

Therefore:
p is the address that the string hello starts at.
*&p means give me whatever is at the location of p (i.e. the address that hello starts at).
*&*&p means give me whatever is at the location that is at the location of p

So all of those just give you p.

Riley
  • 698
  • 6
  • 11
  • but p cannot store value, it should be an address? – Nishant Kumar Sep 02 '16 at 16:53
  • So the address that is stored in `p` is sent to `printf` and `printf` starts printing characters at that address until it sees a `\0` – Riley Sep 02 '16 at 16:55
  • when we give p =20, and printf("%d",*&*&p); then it will not work right? – Nishant Kumar Sep 02 '16 at 16:58
  • @NishantKumar Yes, it would be trying to read from memory address 20 which is probably invalid, so you'd get a seg fault. – Riley Sep 02 '16 at 17:02
  • @NishantKumar You should get a compiler error because you're assigning an `int` to a `char*` – Riley Sep 02 '16 at 17:02
  • ok then if char p= "hello"; printf("%s",p); it prints error but char *p;p="hello"; printf("%s",p); it prints hello – Nishant Kumar Sep 02 '16 at 17:05
  • @NishantKumar Correct, you cannot assign a string (which is an array of `char`s) to a single `char`. – Riley Sep 02 '16 at 17:09
  • ok last question does in char *p; p= "hello", p store address pf string hello – Nishant Kumar Sep 02 '16 at 17:16
  • how its store adress of the string, bcz when give p = 20, it store it as address. – Nishant Kumar Sep 02 '16 at 17:19
  • @NishantKumar It stores the address that the string starts at. Try `printf("%p", p)` to see the address, `printf("%c", *p)` for the first character, `printf("%c", *(p+1))` for the second character and so on. – Riley Sep 02 '16 at 17:22
  • In C, "`p` is the string" is not correct. A string is a character array/sequence up to and including a null character. A string is not a pointer. `p` is type `char *` - a pointer. `p` has a value of an address. `p` has the value of the address of the first character of a string. – chux - Reinstate Monica Sep 02 '16 at 19:14
  • @chux You're correct, but sometimes it's easier to think about the pointer to a character array that is null terminated as a string even though that's not technically correct. – Riley Sep 02 '16 at 19:22