Thursday, February 18, 2010

26

#include < stdio.h >
int main()
{
char a[]="shark selva";
printf("%c",a[0]);
printf("%d",&a[1]);
printf("%s",&a[1]);
printf("%c",*(&a[1]));
printf("%c",*a);
printf("%s",&a);
return 0;
}


Output


s
-14
hark selva
h
s
shark selva


Logic:


string array 'a' stores characters like this





















space








a[0]

a[1]

a[2]

a[3]

a[4]

a[5]

a[6]

a[7]

a[8]

a[9]

a[10]

a[11]

s

h

a

r

k

s

e

l

v

a

\0

so obviously a[0] prints 's' character .
in case of &a[1] ,it specify the address of a[1].
so printf("%d",&a[1]); prints the address of a[1].
in case of printf("%s",&a[1]) ,it'll print all character starting from address a[1]
i.e., it 'll print "hark selva"(because i give %s format specifier instead of %d).
printf("%c",*(&a[1]));
  '*' is known as indirection operator(indirectly specifies 
the value in specified address).

so *(&a[1]) specifies the 'h' character.
*a is equal to a[0]
it will print the 's' character.
&a is equal to &a[0].
this will print the "shark selva" string.

0 comments:

Post a Comment