Reputation: 11
this was the code snippet asked for me in a interview and pls explain me what is the use of &.
#include<stdio.h>
int main()
{
char *str="INCLUDEHELP";
printf("%s",*&*str);
}
Upvotes: 0
Views: 276
Reputation: 141085
The &*
is an empty operation. The &*str
(or like &*&*&*&*&*&*&*&*&*str
or any number of &*
) is equivalent to just str
. See C11 note102. We can omit it.
char *str="INCLUDEHELP";
printf("%s", *str);
This code with %s
is invalid. The %s
expects zero terminated char
array. The *str
is a char
with the value of 'I'
, a character. The program on linux-like systems will most probably receive a segmentation fault signal, because 'I'
will be an invalid address of a zero terminated character array.
If it were:
char *str="INCLUDEHELP";
printf("%c", *str);
or:
#include<stdio.h>
int main()
{
char *str="INCLUDEHELP";
printf("%c",*&*str);
}
, then the program would print a single character I
on it's stdout
.
Upvotes: 2
Reputation: 67546
The output is most probably the segfault.as it is the Undefined Behaviour. The &*...
from the right mean. Dereference the pointer, get the address of the dereferenced char, and dereference this address again passing the char instead of the pointer to printf.
Upvotes: 0