[Solved] C. for loop with chars


This

char s[]="TvNnFs",*p;

where s is an array of characters and p is character pointer, is looks like below

 s[0]  s[1]   s[2]  s[3]  s[4]  s[5]  s[6]
  ------------------------------------------
 |  T  |  v  |  N  |  n  |  F  |  s  |  \0  |
  ------------------------------------------
 s 0x100 0x101 0x102 0x103 0x104 0x105 0x106.. (assume 0x100 is base address of s)

Next, the for loop

for(p=&s[5];p>=s;p--) 
    --*p;

Here p=&s[5] the pointer p points to address of s[5] i.e 0x105. Next is p>=s i.e 0x105 >= 0x100 which is true, then --*p executes i.e first *p that means value at 0x105 memory location which is s and decrements on that will makes s[5] as r.

Now char array s looks like

 s[0]  s[1]   s[2]  s[3]  s[4]  s[5]  s[6]
  ---------------------------------------------
 |  T  |  v  |  N  |  n  |  F  |  r(new)|  \0  |
  ---------------------------------------------
 s 0x100 0x101 0x102 0x103 0x104 0x105 0x106..

Note : you might interested to know that by doing --*p how does s got affected ? Its because p is pointing or holding the address of s i.e whatever changes are done on *p will affect indirectly to s.

After that p-- happens i.e p now points to one location previous than before i.e 0x104. Same operation will happens until p reach to s i.e 0x100 >= 0x100. Finally char array s looks like

      s[0]  s[1]   s[2]  s[3]  s[4]  s[5]  s[6]
      -------------------------------------------
     |  S  |  u  |  M  |  m  |  E  |  r  |  \0  |
      -------------------------------------------
     s 0x100 0x101 0x102 0x103 0x104 0x105 0x106..

Hence it prints SuMmEr.

Case 2 :

char s[]="TvNnFs",*p;
for(p=&s[5]; p>=s; p--) 
((--*p)<'a')?(*p+=('a'-'A')):(*p);
puts(s);

Here

       s[0]  s[1]   s[2]  s[3]  s[4]  s[5]  s[6]
      ------------------------------------------
     |  T  |  v  |  N  |  n  |  F  |  s  |  \0  |
      ------------------------------------------
     s 0x100 0x101 0x102 0x103 0x104 0x105 0x106..
                                      |
                                      p points here

For 0x105 >= 0x100 : this

((--*p)<'a')?(*p+=('a'-'A')):(*p);

is ternary operator i.e first ((--*p)<'a') executes, if it results in true then (*p+=('a'-'A')) will be the output else (*p). So Here it looks like ((--*p)<'a') i.e 'r' < 'a' which is false so just (*p) but s[5] got changed after this due to --*p.

      s[0]  s[1]   s[2]  s[3]  s[4]  s[5]  s[6]
      ------------------------------------------
     |  T  |  v  |  N  |  n  |  F  |  r  |  \0  |
      ------------------------------------------
     s 0x100 0x101 0x102 0x103 0x104 0x105 0x106..
                                | 
                                p points here due to p--

Next, For 0x104 >= 0x100 : this

((--*p)<'a')?(*p+=('a'-'A')):(*p);

'E' < 'a' i.e 70 < 97 which is true so this (*p+=('a'-'A')) gets execute i.e

*p = *p + ('a' - 'A')
   = 'E' + (97 - 65)
   = 'E' + 32
*p = 'e' /* now s[4] overwritten by e(previously F) */

Now array looks like

   s[0]  s[1]   s[2]  s[3]  s[4]  s[5]  s[6]
  ------------------------------------------
 |  T  |  v  |  N  |  n  |  e  |  r  |  \0  |
  ------------------------------------------
 s 0x100 0x101 0x102 0x103 0x104 0x105 0x106..
                      | 
                      p points here due to p--

Same operation hoes on until 0x100 >= 0x100.

2

solved C. for loop with chars