{"id":13323,"date":"2022-10-03T18:29:27","date_gmt":"2022-10-03T12:59:27","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-incorrect-behavior-of-a-pointer-in-function-in-c\/"},"modified":"2022-10-03T18:29:27","modified_gmt":"2022-10-03T12:59:27","slug":"solved-incorrect-behavior-of-a-pointer-in-function-in-c","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-incorrect-behavior-of-a-pointer-in-function-in-c\/","title":{"rendered":"[Solved] Incorrect behavior of a pointer in function in C"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-37578460\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"37578460\" data-parentid=\"37577500\" data-score=\"0\" 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>There are numerous errors in the program although it compiled without any warnings. Chiefly the pointer types for your array, and the memory allocated. Secondly the function does not know how many words is allowed, and does not return how many were read &#8211; your method did not work at all (as in comments). Thirdly the string comparisons: you did not state the goals clearly, but in comment you want the &#8220;biggest string&#8221;. <code>strcoll<\/code> does not do that &#8211; it&#8217;s a lexical comparison, so I changed that section to find the longest string for the two sentences you enter. See comments, I made a large number of changes.<\/p>\n<pre><code>#include &lt;stdio.h&gt;\n#include &lt;string.h&gt;\n#include &lt;stdlib.h&gt;\n\nint returnArrayOfWords (char *str4Parsing, char *arrayParsed[], int maxtokens)  \/\/ added max\n{\n    char seps[] = \" ,\\t\\n\"; \/\/ separators\n    char *token1 = NULL;\n    char *next_token1 = NULL;\n    int i = 0;\n\n    \/\/ Establish string and get the first token:\n    token1 = strtok_s( str4Parsing, seps, &amp;next_token1);\n\n    \/\/ While there are tokens in \"str4Parsing\" \n    while (token1 != NULL)\n    {\n        if(i &gt;= maxtokens)\n            return i;                                   \/\/ ignore the rest\n        arrayParsed[i] = token1;\n        printf( \" %s\\n\", token1 );\n        token1 = strtok_s( NULL, seps, &amp;next_token1);\n        i++;\n    }\n    return i;\n}\n\nint main (void)                                         \/\/ correct signature\n{\n    int i, j, n = 80; \/*max number of words in string*\/\n    char arrS1[80], arrS2[80];\n    \/\/const char *w1, *w2; \/*pointers*\/                 \/\/ deleted\n    char **ptrArray1, **ptrArray2;                      \/\/ changed type\n    int currLength1 = 0, currLength2 = 0 ;\n    int sizeArr1 = 0, sizeArr2 = 0;\n    int maxLength = 0;\n    char *wordMaxLength;                                \/\/ changed to pointer\n\n    ptrArray1 = malloc(n * sizeof (char*));             \/\/ allocate mem for pointer array\n    if (ptrArray1 == NULL)\n        return 1;\n    ptrArray2 = malloc(n * sizeof (char*));             \/\/ allocate mem for pointer array\n    if (ptrArray2 == NULL)\n        return 1;\n\n    printf(\"Type your first string: \"); \n    fgets(arrS1, 80, stdin);\n    sizeArr1 = returnArrayOfWords(arrS1, ptrArray1, n); \/\/ indirection error, added max words, get actual num\n\n    printf(\"Type your second string: \");\n    fgets(arrS2, 80, stdin);\n    sizeArr2 = returnArrayOfWords(arrS2, ptrArray2, n); \/\/ indirection error, added max words, get actual num\n\n    for (i = 0; i &lt; sizeArr1; i++)                      \/\/ this section rewritten\n    {\n        \/\/ to find the largest word in the array\n        currLength1 = strlen(ptrArray1[i]);\n        if(currLength1 &gt; maxLength) \n        {\n            maxLength = currLength1;\n            wordMaxLength = ptrArray1[i];               \/\/ changed definition to pointer\n        }\n    }\n\n    for (j = 0; j &lt; sizeArr2; j++)\n    {\n        \/\/ to find the largest word in the array\n        currLength2 = strlen(ptrArray2[j]);\n        if(currLength2 &gt; maxLength) \n        {\n            maxLength = currLength2;\n            wordMaxLength = ptrArray2[j];               \/\/ changed definition to pointer\n        }\n    }\n\n    printf(\"The largest word is: %s\", wordMaxLength);\n\n    free(ptrArray1);                                    \/\/ added\n    free(ptrArray2);\n    return 0;\n}\n<\/code><\/pre>\n<p>Program session:<\/p>\n<pre><code>Type your first string: one two three four\n one\n two\n three\n four\nType your second string: apple banana pear\n apple\n banana\n pear\nThe largest word is: banana\n<\/code><\/pre>\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 Incorrect behavior of a pointer in function in C <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] There are numerous errors in the program although it compiled without any warnings. Chiefly the pointer types for your array, and the memory allocated. Secondly the function does not know how many words is allowed, and does not return how many were read &#8211; your method did not work at all (as in comments). &#8230; <a title=\"[Solved] Incorrect behavior of a pointer in function in C\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-incorrect-behavior-of-a-pointer-in-function-in-c\/\" aria-label=\"More on [Solved] Incorrect behavior of a pointer in function in C\">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,712,2961],"class_list":["post-13323","post","type-post","status-publish","format-standard","hentry","category-solved","tag-arrays","tag-c","tag-pointers","tag-strtok"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] Incorrect behavior of a pointer in function in C - 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-incorrect-behavior-of-a-pointer-in-function-in-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] Incorrect behavior of a pointer in function in C - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] There are numerous errors in the program although it compiled without any warnings. Chiefly the pointer types for your array, and the memory allocated. Secondly the function does not know how many words is allowed, and does not return how many were read &#8211; your method did not work at all (as in comments). ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-incorrect-behavior-of-a-pointer-in-function-in-c\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-10-03T12:59:27+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-incorrect-behavior-of-a-pointer-in-function-in-c\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-incorrect-behavior-of-a-pointer-in-function-in-c\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] Incorrect behavior of a pointer in function in C\",\"datePublished\":\"2022-10-03T12:59:27+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-incorrect-behavior-of-a-pointer-in-function-in-c\/\"},\"wordCount\":132,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"arrays\",\"c++\",\"pointers\",\"strtok\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-incorrect-behavior-of-a-pointer-in-function-in-c\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-incorrect-behavior-of-a-pointer-in-function-in-c\/\",\"name\":\"[Solved] Incorrect behavior of a pointer in function in C - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-10-03T12:59:27+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-incorrect-behavior-of-a-pointer-in-function-in-c\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-incorrect-behavior-of-a-pointer-in-function-in-c\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-incorrect-behavior-of-a-pointer-in-function-in-c\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] Incorrect behavior of a pointer in function in C\"}]},{\"@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] Incorrect behavior of a pointer in function in C - 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-incorrect-behavior-of-a-pointer-in-function-in-c\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] Incorrect behavior of a pointer in function in C - JassWeb","og_description":"[ad_1] There are numerous errors in the program although it compiled without any warnings. Chiefly the pointer types for your array, and the memory allocated. Secondly the function does not know how many words is allowed, and does not return how many were read &#8211; your method did not work at all (as in comments). ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-incorrect-behavior-of-a-pointer-in-function-in-c\/","og_site_name":"JassWeb","article_published_time":"2022-10-03T12:59:27+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-incorrect-behavior-of-a-pointer-in-function-in-c\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-incorrect-behavior-of-a-pointer-in-function-in-c\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] Incorrect behavior of a pointer in function in C","datePublished":"2022-10-03T12:59:27+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-incorrect-behavior-of-a-pointer-in-function-in-c\/"},"wordCount":132,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["arrays","c++","pointers","strtok"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-incorrect-behavior-of-a-pointer-in-function-in-c\/","url":"https:\/\/jassweb.com\/solved\/solved-incorrect-behavior-of-a-pointer-in-function-in-c\/","name":"[Solved] Incorrect behavior of a pointer in function in C - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-10-03T12:59:27+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-incorrect-behavior-of-a-pointer-in-function-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-incorrect-behavior-of-a-pointer-in-function-in-c\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-incorrect-behavior-of-a-pointer-in-function-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] Incorrect behavior of a pointer in function in C"}]},{"@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\/13323","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=13323"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/13323\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=13323"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=13323"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=13323"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}