Thursday, August 14, 2008

Strings

static char s[]= "Oinks Grunts and Guffaws";

Now we see some results on this string:
1. *(&s[2])=s[2]=*(s+2)=*(2+s ) = n
2. strlen(s) = length of the string
so strlen(s)+s= last character of the string = \0 , whose ascii value is 0.
So when %c is used , \0 is printed and when %d is used 0 is printed.
3. when is printed with %d we get the base address of the string.
4. s+5 with %s prints ^Grunts and Guffaws, that is base address + 5 and rest of the string.

Now let i=0;
ch = s[++i]; // ch = s[1] i.e. i
ch=s[i++];  // ch=s[1] but i=2
ch =i++[s]  // ch = 2[s] = s[2 ] but i = 3.
ch = ++i[s]; // ++3[s] = ++s[3] = k+1 = l(ell)

0 comments:

Post a Comment