{"id":29501,"date":"2023-01-08T07:43:50","date_gmt":"2023-01-08T02:13:50","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-c-for-loop-with-chars\/"},"modified":"2023-01-08T07:43:50","modified_gmt":"2023-01-08T02:13:50","slug":"solved-c-for-loop-with-chars","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-c-for-loop-with-chars\/","title":{"rendered":"[Solved] C. for loop with chars"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-55283662\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"55283662\" data-parentid=\"55282981\" data-score=\"1\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>This<\/p>\n<pre><code>char s[]=\"TvNnFs\",*p;\n<\/code><\/pre>\n<p>where <code>s<\/code> is an array of characters and <code>p<\/code> is character pointer, is looks like below<\/p>\n<pre><code> s[0]  s[1]   s[2]  s[3]  s[4]  s[5]  s[6]\n  ------------------------------------------\n |  T  |  v  |  N  |  n  |  F  |  s  |  \\0  |\n  ------------------------------------------\n s 0x100 0x101 0x102 0x103 0x104 0x105 0x106.. (assume 0x100 is base address of s)\n<\/code><\/pre>\n<p>Next, the <code>for<\/code> loop<\/p>\n<pre><code>for(p=&amp;s[5];p&gt;=s;p--) \n    --*p;\n<\/code><\/pre>\n<p>Here <code>p=&amp;s[5]<\/code> the pointer <code>p<\/code> points to address of <code>s[5]<\/code> i.e <code>0x105<\/code>. Next is <code>p&gt;=s<\/code> i.e <code>0x105 &gt;= 0x100<\/code> which is true, then <code>--*p<\/code> executes i.e first <code>*p<\/code> that means value at <code>0x105<\/code> memory location which is <code>s<\/code> and decrements on that will makes <code>s[5]<\/code> as <code>r<\/code>. <\/p>\n<p>Now char array <code>s<\/code> looks like<\/p>\n<pre><code> s[0]  s[1]   s[2]  s[3]  s[4]  s[5]  s[6]\n  ---------------------------------------------\n |  T  |  v  |  N  |  n  |  F  |  r(new)|  \\0  |\n  ---------------------------------------------\n s 0x100 0x101 0x102 0x103 0x104 0x105 0x106..\n<\/code><\/pre>\n<p><strong>Note :<\/strong> you might interested to know that by doing <code>--*p<\/code> how does <code>s<\/code> got affected ? Its because <code>p<\/code> is pointing or holding the address of <code>s<\/code> i.e whatever changes are done on <code>*p<\/code> will affect indirectly to <code>s<\/code>.<\/p>\n<p>After that <code>p--<\/code> happens i.e <code>p<\/code> now points to one location previous than before i.e <code>0x104<\/code>. Same operation will happens until <code>p<\/code> reach to <code>s<\/code> i.e <code>0x100 &gt;= 0x100<\/code>. Finally char array <code>s<\/code> looks like<\/p>\n<pre><code>      s[0]  s[1]   s[2]  s[3]  s[4]  s[5]  s[6]\n      -------------------------------------------\n     |  S  |  u  |  M  |  m  |  E  |  r  |  \\0  |\n      -------------------------------------------\n     s 0x100 0x101 0x102 0x103 0x104 0x105 0x106..\n<\/code><\/pre>\n<p>Hence it prints <code>SuMmEr<\/code>.<\/p>\n<p><strong>Case 2 :<\/strong>  <\/p>\n<pre><code>char s[]=\"TvNnFs\",*p;\nfor(p=&amp;s[5]; p&gt;=s; p--) \n((--*p)&lt;'a')?(*p+=('a'-'A')):(*p);\nputs(s);\n<\/code><\/pre>\n<p>Here<\/p>\n<pre><code>       s[0]  s[1]   s[2]  s[3]  s[4]  s[5]  s[6]\n      ------------------------------------------\n     |  T  |  v  |  N  |  n  |  F  |  s  |  \\0  |\n      ------------------------------------------\n     s 0x100 0x101 0x102 0x103 0x104 0x105 0x106..\n                                      |\n                                      p points here\n<\/code><\/pre>\n<p>For <code>0x105 &gt;= 0x100<\/code> : this<\/p>\n<pre><code>((--*p)&lt;'a')?(*p+=('a'-'A')):(*p);\n<\/code><\/pre>\n<p>is ternary operator i.e first <code>((--*p)&lt;'a')<\/code> executes, if it results in true then <code>(*p+=('a'-'A'))<\/code> will be the output else <code>(*p)<\/code>. So Here it looks like <code>((--*p)&lt;'a')<\/code> i.e <code>'r' &lt; 'a'<\/code> which is false so just <code>(*p)<\/code> but <code>s[5]<\/code> got changed after this due to <code>--*p<\/code>. <\/p>\n<pre><code>      s[0]  s[1]   s[2]  s[3]  s[4]  s[5]  s[6]\n      ------------------------------------------\n     |  T  |  v  |  N  |  n  |  F  |  r  |  \\0  |\n      ------------------------------------------\n     s 0x100 0x101 0x102 0x103 0x104 0x105 0x106..\n                                | \n                                p points here due to p--\n<\/code><\/pre>\n<p>Next, For <code>0x104 &gt;= 0x100<\/code> : this<\/p>\n<pre><code>((--*p)&lt;'a')?(*p+=('a'-'A')):(*p);\n<\/code><\/pre>\n<p><code>'E' &lt; 'a'<\/code> i.e <code>70 &lt; 97<\/code> which is true so this <code>(*p+=('a'-'A'))<\/code> gets execute i.e <\/p>\n<pre><code>*p = *p + ('a' - 'A')\n   = 'E' + (97 - 65)\n   = 'E' + 32\n*p = 'e' \/* now s[4] overwritten by e(previously F) *\/\n<\/code><\/pre>\n<p>Now array looks like<\/p>\n<pre><code>   s[0]  s[1]   s[2]  s[3]  s[4]  s[5]  s[6]\n  ------------------------------------------\n |  T  |  v  |  N  |  n  |  e  |  r  |  \\0  |\n  ------------------------------------------\n s 0x100 0x101 0x102 0x103 0x104 0x105 0x106..\n                      | \n                      p points here due to p--\n<\/code><\/pre>\n<p>Same operation hoes on until <code>0x100 &gt;= 0x100<\/code>.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">2<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved C. for loop with chars <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] This char s[]=&#8221;TvNnFs&#8221;,*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] &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; | T | v | N | n | F | s | \\0 | &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212; s 0x100 0x101 0x102 0x103 0x104 0x105 0x106.. (assume 0x100 is &#8230; <a title=\"[Solved] C. for loop with chars\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-c-for-loop-with-chars\/\" aria-label=\"More on [Solved] C. for loop with chars\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[324,472,712],"class_list":["post-29501","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-char","tag-pointers"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] C. for loop with chars - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-c-for-loop-with-chars\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] C. for loop with chars - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] This char s[]=&quot;TvNnFs&quot;,*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 |  | ------------------------------------------ s 0x100 0x101 0x102 0x103 0x104 0x105 0x106.. (assume 0x100 is ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-c-for-loop-with-chars\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2023-01-08T02:13:50+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-c-for-loop-with-chars\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-c-for-loop-with-chars\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] C. for loop with chars\",\"datePublished\":\"2023-01-08T02:13:50+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-c-for-loop-with-chars\/\"},\"wordCount\":202,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\",\"char\",\"pointers\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-c-for-loop-with-chars\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-c-for-loop-with-chars\/\",\"name\":\"[Solved] C. for loop with chars - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2023-01-08T02:13:50+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-c-for-loop-with-chars\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-c-for-loop-with-chars\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-c-for-loop-with-chars\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] C. for loop with chars\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/jassweb.com\/solved\/#website\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/jassweb.com\/solved\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\/\/jassweb.com\/solved\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"contentUrl\":\"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] C. for loop with chars - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-c-for-loop-with-chars\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] C. for loop with chars - JassWeb","og_description":"[ad_1] 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 |  | ------------------------------------------ s 0x100 0x101 0x102 0x103 0x104 0x105 0x106.. (assume 0x100 is ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-c-for-loop-with-chars\/","og_site_name":"JassWeb","article_published_time":"2023-01-08T02:13:50+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-c-for-loop-with-chars\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-c-for-loop-with-chars\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] C. for loop with chars","datePublished":"2023-01-08T02:13:50+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-c-for-loop-with-chars\/"},"wordCount":202,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","char","pointers"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-c-for-loop-with-chars\/","url":"https:\/\/jassweb.com\/solved\/solved-c-for-loop-with-chars\/","name":"[Solved] C. for loop with chars - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2023-01-08T02:13:50+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-c-for-loop-with-chars\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-c-for-loop-with-chars\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-c-for-loop-with-chars\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] C. for loop with chars"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/image\/","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775193939","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/29501","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=29501"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/29501\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=29501"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=29501"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=29501"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}