{"id":14389,"date":"2022-10-07T15:57:12","date_gmt":"2022-10-07T10:27:12","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-how-a-pointer-to-a-string-works-in-a-function\/"},"modified":"2022-10-07T15:57:12","modified_gmt":"2022-10-07T10:27:12","slug":"solved-how-a-pointer-to-a-string-works-in-a-function","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-a-pointer-to-a-string-works-in-a-function\/","title":{"rendered":"[Solved] How a pointer to a string works in a function?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-47025510\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"47025510\" data-parentid=\"47025296\" 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<h1>Snippet 1<\/h1>\n<pre><code>char a[200],*p;\n<\/code><\/pre>\n<p>Defined a block of 200 characters ,<code>a<\/code>,  and a pointer, <code>p<\/code>, to a block of one or more characters.<\/p>\n<pre><code>gets(a); \/\/ cin.get(a,200);\n<\/code><\/pre>\n<p>Read some user input into memory block <code>a<\/code><\/p>\n<pre><code>p=a;\n<\/code><\/pre>\n<p>Points <code>p<\/code> at block <code>a<\/code>. <code>p<\/code> can now be used as a reference to the same thing as <code>a<\/code><\/p>\n<pre><code>strcpy(p,p+1);\n<\/code><\/pre>\n<p>Copy part of the memory block over itself. NOTE: this invokes undefined behaviour because the to and from buffers in <code>strcpy<\/code> overlap (See <a rel=\"nofollow noopener\" target=\"_blank\" href=\"http:\/\/en.cppreference.com\/w\/cpp\/string\/byte\/strcpy\">http:\/\/en.cppreference.com\/w\/cpp\/string\/byte\/strcpy<\/a>). The results may be unusual, ranging  from &#8220;Looks like it works!&#8221; to more fantastic, such as the computer making it rain unicorns.<\/p>\n<p>So, say <code>a<\/code> is created at memory location 10. Data is read into the block starting at location 10. Then <code>p<\/code> is set to point at location ten. Then the memory starting at location 11 is copied over the memory starting at location 10.  Since <code>a<\/code> is at 10, printing <code>a<\/code> prints the altered data. Since <code>p<\/code> is still pointing at 10, printing <code>p<\/code> will print the same thing as <code>a<\/code>.<\/p>\n<h1>Snippet 2:<\/h1>\n<pre><code>char a[200],*p;\n<\/code><\/pre>\n<p>Defined a block of 200 characters ,<code>a<\/code>,  and a pointer, <code>p<\/code>, to a block of one or more characters.<\/p>\n<pre><code>gets(a); \/\/ cin.get(a,200);\n<\/code><\/pre>\n<p>Read some user input into memory block <code>a<\/code><\/p>\n<pre><code>p=a;\n<\/code><\/pre>\n<p>Points <code>p<\/code> at block <code>a<\/code>. <code>p<\/code> can now be used as a reference to the same thing as <code>a<\/code><\/p>\n<pre><code>p++;\n<\/code><\/pre>\n<p>Moved where <code>p<\/code> pointed over one slot to the right. Copied nothing. Just changed the address that <code>p<\/code> points at.<\/p>\n<p>So, say <code>a<\/code> is created at memory location 10. Data is read into the block starting at location 10. Then <code>p<\/code> is set to point at location 10. Then <code>p<\/code> is set to point at memory location 11. The values in memory block <code>a<\/code> are unchanged. Since <code>a<\/code> is still at 10, printing <code>a<\/code> prints the unchanged data. Since <code>p<\/code> is now pointing at 11, printing <code>p<\/code> will print the from the second character of <code>a<\/code> onward.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">10<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved How a pointer to a string works in a function? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] Snippet 1 char a[200],*p; Defined a block of 200 characters ,a, and a pointer, p, to a block of one or more characters. gets(a); \/\/ cin.get(a,200); Read some user input into memory block a p=a; Points p at block a. p can now be used as a reference to the same thing as a &#8230; <a title=\"[Solved] How a pointer to a string works in a function?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-a-pointer-to-a-string-works-in-a-function\/\" aria-label=\"More on [Solved] How a pointer to a string works in a function?\">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":[361,324,472,712],"class_list":["post-14389","post","type-post","status-publish","format-standard","hentry","category-solved","tag-arrays","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] How a pointer to a string works in a function? - 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-how-a-pointer-to-a-string-works-in-a-function\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] How a pointer to a string works in a function? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] Snippet 1 char a[200],*p; Defined a block of 200 characters ,a, and a pointer, p, to a block of one or more characters. gets(a); \/\/ cin.get(a,200); Read some user input into memory block a p=a; Points p at block a. p can now be used as a reference to the same thing as a ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-a-pointer-to-a-string-works-in-a-function\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-07T10:27:12+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-how-a-pointer-to-a-string-works-in-a-function\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-a-pointer-to-a-string-works-in-a-function\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] How a pointer to a string works in a function?\",\"datePublished\":\"2022-10-07T10:27:12+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-a-pointer-to-a-string-works-in-a-function\/\"},\"wordCount\":293,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"arrays\",\"c++\",\"char\",\"pointers\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-a-pointer-to-a-string-works-in-a-function\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-how-a-pointer-to-a-string-works-in-a-function\/\",\"name\":\"[Solved] How a pointer to a string works in a function? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-10-07T10:27:12+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-a-pointer-to-a-string-works-in-a-function\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-how-a-pointer-to-a-string-works-in-a-function\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-a-pointer-to-a-string-works-in-a-function\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] How a pointer to a string works in a function?\"}]},{\"@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=1775798750\",\"contentUrl\":\"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\/\/jassweb.com\"],\"url\":\"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] How a pointer to a string works in a function? - 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-how-a-pointer-to-a-string-works-in-a-function\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] How a pointer to a string works in a function? - JassWeb","og_description":"[ad_1] Snippet 1 char a[200],*p; Defined a block of 200 characters ,a, and a pointer, p, to a block of one or more characters. gets(a); \/\/ cin.get(a,200); Read some user input into memory block a p=a; Points p at block a. p can now be used as a reference to the same thing as a ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-a-pointer-to-a-string-works-in-a-function\/","og_site_name":"JassWeb","article_published_time":"2022-10-07T10:27:12+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-how-a-pointer-to-a-string-works-in-a-function\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-a-pointer-to-a-string-works-in-a-function\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] How a pointer to a string works in a function?","datePublished":"2022-10-07T10:27:12+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-a-pointer-to-a-string-works-in-a-function\/"},"wordCount":293,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["arrays","c++","char","pointers"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-a-pointer-to-a-string-works-in-a-function\/","url":"https:\/\/jassweb.com\/solved\/solved-how-a-pointer-to-a-string-works-in-a-function\/","name":"[Solved] How a pointer to a string works in a function? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-07T10:27:12+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-a-pointer-to-a-string-works-in-a-function\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-a-pointer-to-a-string-works-in-a-function\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-a-pointer-to-a-string-works-in-a-function\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] How a pointer to a string works in a function?"}]},{"@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=1775798750","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1775798750","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\/14389","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=14389"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/14389\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=14389"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=14389"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=14389"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}