{"id":248771,"date":"2025-07-08T18:09:26","date_gmt":"2025-07-08T18:09:26","guid":{"rendered":"https:\/\/learnexams.com\/blog\/?p=248771"},"modified":"2025-07-08T18:09:29","modified_gmt":"2025-07-08T18:09:29","slug":"char-toupperchar-c-if-c-a-c-z-return-c-0x20-else-return-c-char-input-void-loop-if-serial-available-0-input-serial","status":"publish","type":"post","link":"https:\/\/www.learnexams.com\/blog\/2025\/07\/08\/char-toupperchar-c-if-c-a-c-z-return-c-0x20-else-return-c-char-input-void-loop-if-serial-available-0-input-serial\/","title":{"rendered":"char toUpper(char c) { if (c >= &#8216;a&#8217; &amp;&amp; c &lt;= &#8216;z&#8217;) return c &#8211; 0x20; else return c; } char input; void loop() { if (Serial.available() > 0) { input = Serial"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><br>char toUpper(char c) { if (c >= &#8216;a&#8217; &amp;&amp; c &lt;= &#8216;z&#8217;) return c &#8211; 0x20; else return c; } char input; void loop() { if (Serial.available() > 0) { input = Serial.read(); if (input != -1) Serial.print(toUpper(input)); else Serial.println(); } } Rewrite the toUpper() function to take a pointer to a character as a parameter and make whatever other changes are required in the rest of the program. (4)<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-ast-global-color-0-color\">The Correct Answer and Explanation is:<\/mark><\/strong><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Here is the modified version of your program with the <code>toUpper<\/code> function rewritten to take a pointer to a character as a parameter. The changes reflect this new approach, and I&#8217;ll explain how the code works afterward.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Modified Code:<\/h3>\n\n\n\n<pre class=\"wp-block-preformatted\">t<code>void toUpper(char* c) {<br>  if (*c >= 'a' &amp;&amp; *c &lt;= 'z') {<br>    *c = *c - 0x20;  \/\/ Convert to uppercase<br>  }<br>}<br><br>char input;<br><br>void loop() {<br>  if (Serial.available() > 0) {<br>    input = Serial.read();<br>    if (input != -1) {<br>      toUpper(&amp;input);  \/\/ Pass the address of input to the function<br>      Serial.print(input);<br>    } else {<br>      Serial.println();<br>    }<br>  }<br>}<br><\/code><\/pre>\n\n\n\n<h3 class=\"wp-block-heading\">Explanation:<\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Function Signature Change<\/strong>:<br>In the original <code>toUpper<\/code> function, a <code>char<\/code> was passed by value, meaning any modifications made to the variable inside the function wouldn\u2019t affect the original character in <code>loop()<\/code>. By changing the function to accept a <code>char*<\/code> (pointer to <code>char<\/code>), we now modify the original character passed to the function.<\/li>\n\n\n\n<li><strong>Dereferencing the Pointer<\/strong>:<br>Inside the <code>toUpper<\/code> function, we use <code>*c<\/code> to dereference the pointer and access the actual character that the pointer refers to. This allows us to modify the original character directly. The condition <code>if (*c >= 'a' &amp;&amp; *c &lt;= 'z')<\/code> checks if the character is a lowercase letter. If it is, we convert it to uppercase by subtracting <code>0x20<\/code> (which is the difference between the ASCII values of lowercase and uppercase letters).<\/li>\n\n\n\n<li><strong>Calling <code>toUpper<\/code> in <code>loop()<\/code><\/strong>:<br>In the <code>loop()<\/code> function, we pass the address of the <code>input<\/code> variable using <code>&amp;input<\/code>. This allows the <code>toUpper<\/code> function to work directly with the memory location of <code>input<\/code>, changing it in place. After the function call, the updated value of <code>input<\/code> (now potentially in uppercase) is printed.<\/li>\n\n\n\n<li><strong>Other Adjustments<\/strong>:<br>No other major changes were needed in the rest of the code. The logic to check if data is available on the serial port and to print the result remains the same. We just updated the function call to handle a pointer rather than a direct value.<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\">Key Takeaways:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Pointers allow you to modify data directly by accessing its memory location.<\/li>\n\n\n\n<li>By passing a pointer to a character, we are now able to change the character inside the <code>toUpper<\/code> function, and the change is reflected in the calling function (<code>loop()<\/code>).<\/li>\n<\/ul>\n\n\n\n<figure class=\"wp-block-image size-full\"><img decoding=\"async\" src=\"https:\/\/learnexams.com\/blog\/wp-content\/uploads\/2025\/07\/learnexams-banner8-313.jpeg\" alt=\"\" class=\"wp-image-248772\"\/><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>char toUpper(char c) { if (c >= &#8216;a&#8217; &amp;&amp; c &lt;= &#8216;z&#8217;) return c &#8211; 0x20; else return c; } char input; void loop() { if (Serial.available() > 0) { input = Serial.read(); if (input != -1) Serial.print(toUpper(input)); else Serial.println(); } } Rewrite the toUpper() function to take a pointer to a character as a [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[25],"tags":[],"class_list":["post-248771","post","type-post","status-publish","format-standard","hentry","category-exams-certification"],"_links":{"self":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/248771","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/comments?post=248771"}],"version-history":[{"count":0,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/posts\/248771\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/media?parent=248771"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/categories?post=248771"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.learnexams.com\/blog\/wp-json\/wp\/v2\/tags?post=248771"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}