meaning of the * and & symbol

What do the * and & symbols mean in this code:

#include<stdio.h>
main()
{
  char *p;
  p="hello";
  printf("%s\n",*&*&p);
}

What does the printf statement do in the above program? Specifically, what does *&*&p mean?

Answers


The printf will print the string "hello" because & is the addressOf operator which will return the address of the pointer followed by it and * is the valueOf operator which will return the value stored in the pointer address followed by it.

So in essence, the statement *&*&p will read

valueOf(addressOf(valueOf(addressOf(p))))

which will return the string "hello" which is stored in the actual location.

Hope this would help you!


These:

*&*&

Are redundant and you would never encounter such ridiculous code in a real project. The ampersand takes the address of p, and the asterisk * dereferences it to produce the original char*. Back and forth we go...

Think of it as:

*(&(*(&p)))

As an aside, your type-less signature for main won't cut it on modern compilers where a return type of int is no longer assumed.


Need Your Help

One selected item on a listview

android listview selecteditem

I have made a listview. The plan is that when you select an item, it should appear selected (background color changes) and when you select another one, the one that was selected previously is normal