{"id":24756,"date":"2022-12-05T13:53:43","date_gmt":"2022-12-05T08:23:43","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-how-to-correct-segmentation-fault-with-char-to-int-conversion\/"},"modified":"2022-12-05T13:53:43","modified_gmt":"2022-12-05T08:23:43","slug":"solved-how-to-correct-segmentation-fault-with-char-to-int-conversion","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-how-to-correct-segmentation-fault-with-char-to-int-conversion\/","title":{"rendered":"[Solved] How to correct segmentation fault with char * to int conversion"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-48977134\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"48977134\" data-parentid=\"48977054\" data-score=\"0\" data-position-on-page=\"2\" data-highest-scored=\"0\" data-question-has-accepted-highest-score=\"0\" itemprop=\"suggestedAnswer\" 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>You are confusing pointers with arrays. A pointer just points; declaring a pointer does not reserve memory, it just creates a pointer variable pointing to some undefined place.<\/p>\n<p>Corrected program:<\/p>\n<pre><code>char *recepcao(char *receber) {\n    scanf(\"%s\", receber);\n    return receber;\n}\nint conversao(char *string) { int i, j;\n    for(i=0; string[i]!='\\0'; ++i) {\n        continue;\n    }\n    int var[100];\n    int contagem=0;\n    for(j=0; j&lt;i-1; ++j) {\n        var[j]=(string[j]-'0');\n        contagem+=var[j]*pow(10, (i-j-1));\n    }\n\n    return contagem;\n}\nint main() {\n    char receber[100];\n    printf(\"%i\", conversao(recepcao(receber))); return 0;\n}\n<\/code><\/pre>\n<p>Update:<\/p>\n<p>Arrays and pointers are used in a very similar manner in C, but they are not the same. When you declare an array, the compiler reserves the required memory:<\/p>\n<pre><code>int a[10], b[10];\na[1] = 1;  \/\/ OK\na = b;     \/\/ Error!\n<\/code><\/pre>\n<p>Element of array is variable, but array name is <strong>not<\/strong>. Array name is sort of label, you cannot change its value so that it pointed to another array.<\/p>\n<p>Pointer <strong>is<\/strong> variable. When you declare a pointer, the compiler creates it with undefined contents (like any other variable).<\/p>\n<pre><code>int *p, *q;\np = q;      \/\/ OK but useless because both the pointers contain junk addresses\na[0] = *p;  \/\/ Depending on where p points you will get either junk in a[0] or memory access violation\n. . . \np = a;                 \/\/ Now you can use p as a synonym for array a declared above\nif (*(p + 1) == a[1])  \/\/ Always true\nif (p[2] == *(a + 2))  \/\/ Always true\n. . .\nb = p;   \/\/ This won't work; can't assign a new value to array name b\n. . .\nq = (int*)malloc(sizeof(int) * 10); \/\/ Another way of creating int array of 10 elements\nq = p;  \/\/ Array created with malloc is lost because nothing points to it now\n<\/code><\/pre>\n<p>Basically, the only difference between pointers and arrays is that pointer is variable and array name is not. You can&#8217;t write &#8220;a = p&#8221; exactly as you can&#8217;t write &#8220;2 = i&#8221;.<\/p>\n<p>There also is a rather confusing quirk with array as a formal argument:<\/p>\n<pre><code>   void xyz(int a[10]) {\n       . . .\n   }\n\n   void main() {\n       int b[20];\n       . . .\n       xyz(b);\n   }\n<\/code><\/pre>\n<p>Looks like we assign b to a, and change array size, right? No, &#8220;int a[10]&#8221; is treated as &#8220;int *a&#8221;, and sizeof(a) will return the size of the pointer, not the size of the array.<\/p>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">6<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved How to correct segmentation fault with char * to int conversion <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] You are confusing pointers with arrays. A pointer just points; declaring a pointer does not reserve memory, it just creates a pointer variable pointing to some undefined place. Corrected program: char *recepcao(char *receber) { scanf(&#8220;%s&#8221;, receber); return receber; } int conversao(char *string) { int i, j; for(i=0; string[i]!=&#8217;\\0&#8242;; ++i) { continue; } int var[100]; &#8230; <a title=\"[Solved] How to correct segmentation fault with char * to int conversion\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-how-to-correct-segmentation-fault-with-char-to-int-conversion\/\" aria-label=\"More on [Solved] How to correct segmentation fault with char * to int conversion\">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,5223,391],"class_list":["post-24756","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-fault","tag-loops"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] How to correct segmentation fault with char * to int conversion - 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-to-correct-segmentation-fault-with-char-to-int-conversion\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] How to correct segmentation fault with char * to int conversion - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] You are confusing pointers with arrays. A pointer just points; declaring a pointer does not reserve memory, it just creates a pointer variable pointing to some undefined place. Corrected program: char *recepcao(char *receber) { scanf(&quot;%s&quot;, receber); return receber; } int conversao(char *string) { int i, j; for(i=0; string[i]!=&#039;&#039;; ++i) { continue; } int var[100]; ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-how-to-correct-segmentation-fault-with-char-to-int-conversion\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-05T08:23:43+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-to-correct-segmentation-fault-with-char-to-int-conversion\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-correct-segmentation-fault-with-char-to-int-conversion\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] How to correct segmentation fault with char * to int conversion\",\"datePublished\":\"2022-12-05T08:23:43+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-correct-segmentation-fault-with-char-to-int-conversion\/\"},\"wordCount\":211,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\",\"fault\",\"loops\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-correct-segmentation-fault-with-char-to-int-conversion\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-how-to-correct-segmentation-fault-with-char-to-int-conversion\/\",\"name\":\"[Solved] How to correct segmentation fault with char * to int conversion - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-12-05T08:23:43+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-correct-segmentation-fault-with-char-to-int-conversion\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-how-to-correct-segmentation-fault-with-char-to-int-conversion\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-how-to-correct-segmentation-fault-with-char-to-int-conversion\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] How to correct segmentation fault with char * to int conversion\"}]},{\"@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 to correct segmentation fault with char * to int conversion - 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-to-correct-segmentation-fault-with-char-to-int-conversion\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] How to correct segmentation fault with char * to int conversion - JassWeb","og_description":"[ad_1] You are confusing pointers with arrays. A pointer just points; declaring a pointer does not reserve memory, it just creates a pointer variable pointing to some undefined place. Corrected program: char *recepcao(char *receber) { scanf(\"%s\", receber); return receber; } int conversao(char *string) { int i, j; for(i=0; string[i]!=''; ++i) { continue; } int var[100]; ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-how-to-correct-segmentation-fault-with-char-to-int-conversion\/","og_site_name":"JassWeb","article_published_time":"2022-12-05T08:23:43+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-to-correct-segmentation-fault-with-char-to-int-conversion\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-correct-segmentation-fault-with-char-to-int-conversion\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] How to correct segmentation fault with char * to int conversion","datePublished":"2022-12-05T08:23:43+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-correct-segmentation-fault-with-char-to-int-conversion\/"},"wordCount":211,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","fault","loops"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-correct-segmentation-fault-with-char-to-int-conversion\/","url":"https:\/\/jassweb.com\/solved\/solved-how-to-correct-segmentation-fault-with-char-to-int-conversion\/","name":"[Solved] How to correct segmentation fault with char * to int conversion - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-12-05T08:23:43+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-how-to-correct-segmentation-fault-with-char-to-int-conversion\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-how-to-correct-segmentation-fault-with-char-to-int-conversion\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-how-to-correct-segmentation-fault-with-char-to-int-conversion\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] How to correct segmentation fault with char * to int conversion"}]},{"@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\/24756","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=24756"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/24756\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=24756"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=24756"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=24756"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}