Output of the program | Dereference, Reference, Dereference, Reference….

2.1

Predict the output of below program

int main()
{
 char *ptr = "geeksforgeeks";
 printf("%c\n", *&*&*ptr);
 
 getchar();
 return 0;
}

Output: g

Explanation: The operator * is used for dereferencing and the operator & is used to get the address. These operators cancel effect of each other when used one after another. We can apply them alternatively any no. of times. For example *ptr gives us g, &*ptr gives address of g, *&*ptr again g, &*&*ptr address of g, and finally *&*&*ptr gives ‘g’

Now try below

int main()
{
 char *ptr = "geeksforgeeks";
 printf("%s\n", *&*&ptr);
 
 getchar();
 return 0;
}

GATE CS Corner    Company Wise Coding Practice

Recommended Posts:



2.1 Average Difficulty : 2.1/5.0
Based on 40 vote(s)










Writing code in comment? Please use ide.geeksforgeeks.org, generate link and share the link here.