{"id":11707,"date":"2022-09-28T08:02:37","date_gmt":"2022-09-28T02:32:37","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-c-errors-for-c-code\/"},"modified":"2022-09-28T08:02:37","modified_gmt":"2022-09-28T02:32:37","slug":"solved-c-errors-for-c-code","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-c-errors-for-c-code\/","title":{"rendered":"[Solved] C++ errors for C code"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-31591800\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"31591800\" data-parentid=\"31586245\" 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>First of all, you need to decide whether you&#8217;re writing C <em>or<\/em> C++.  C does not support classes, and you should not use C-style strings, I\/O, and memory management routines in C++ code.  Mixing elements of the two languages is a recipe for heartburn.  <\/p>\n<p>C and C++ are completely different languages that happen to share a lot of syntax and semantics, and a well-written C program doesn&#8217;t look or behave much like a well-written C++ program.  <\/p>\n<p>Now that the obligatory rant is out of the way, your two main type errors are as follows:<\/p>\n<pre><code>while(gets(*(myLines+s)) != ' ') \n<\/code><\/pre>\n<p><code>gets<\/code> (which you should <strong><em>never<\/em><\/strong> <strong><em>never<\/em><\/strong> <strong><em>never<\/em><\/strong> use in either C or C++ <strong><em>for any reason<\/em><\/strong>) returns a value of type <code>char *<\/code>, but the character literal <code>' '<\/code> has type <code>char<\/code> (in C++; in C, it has type <code>int<\/code>).  <code>char<\/code> and <code>char *<\/code> are different, <em>incompatible<\/em> types, and you cannot do comparisons between them.  <\/p>\n<p>Your other type error is <\/p>\n<pre><code> myWord[s] = strdup(myLines); \n<\/code><\/pre>\n<p><code>myLines<\/code> is an <em>array<\/em> of <code>char *<\/code>, which in this context &#8220;decays&#8221; to type <code>char **<\/code>.  <code>strdup<\/code> expects an argument of type <code>char *<\/code>, hence the error.  You need to pass a specific element of the array to <code>strdup<\/code>:<\/p>\n<pre><code>myWord[s] = strdup( myLines[s] );\n<\/code><\/pre>\n<p>Additionally, you cannot compare C-style strings using the <code>==<\/code> or <code>!=<\/code> operators; you must use a library function like <code>strcmp<\/code>, such as<\/p>\n<pre><code>while ( strcmp( buffer, \" \" ) != 0 )\n  ...\n<\/code><\/pre>\n<p>Note that we&#8217;re doing the comparison against the <em>string<\/em> literal <code>\" \"<\/code> instead of the <em>character<\/em> literal <code>' '<\/code>.  <\/p>\n<p>If you want to write <em>C code<\/em>, then you need to change <code>assem<\/code> from a <code>class<\/code> to a <code>struct<\/code>, and move the function delcarations outside of the <code>struct<\/code> definition, and pass the struct type as an argument to the functions:<\/p>\n<pre><code>\/**\n * .h file\n *\/\n#include &lt;stdio.h&gt;\n#include &lt;stdlib.h&gt;\n#include &lt;string.h&gt;\n\nstruct assem\n{\n    char myString[101]; \n    char *myLines[20]; \n    int counter;                            \n};\n\nint readFile(FILE *FileToBeRead, struct assem *item );       \nint firstCheck( struct assem item );\nvoid printFile( struct assem item);                       \n<\/code><\/pre>\n<p>Then you need to remove the <code>assem::<\/code> prefix from your function definitions and use the <code>struct assem<\/code> argument:<\/p>\n<pre><code>int readFile(FILE *file, struct assem *item) \n{\n  size_t i = 0;\n  item-&gt;counter = 0;\n\n  \/**\n   * get the number of array elements in item-&gt;myLines\n   *\/\n  size_t maxLines = sizeof item-&gt;myLines \/ sizeof *item-&gt;myLines;\n\n  while( i &lt; maxLines &amp;&amp; fgets( item-&gt;myString, sizeof item-&gt;myString, file ) ) \n  {\n    item-&gt;myLines[i] = strdup(item-&gt;myString);\n    i++;\n    item-&gt;counter++;\n  }   \n\n  return 0;\n}\n\nvoid printFile( struct assem item ) \n{\n  printf(\"\\n\");\n  for(int s = 0; s &lt; item.counter; s++)\n  {\n      printf(\"%s\\n\", item.myLines[s])); \/\/ use subscript notation; it's \n                                        \/\/ easier on the eyes\n  }\n  printf(\"\\n\");\n}\n\nint firstCheck( struct assem item )                 \n{\n  char *myWord [7] = {NULL};\n\n  for(int s = 0; s &lt; item.counter; s++)        \n  {\n    \/**\n     * In your original code, you were overwriting the myLines array,\n     * which contained what you read from the input file; I'm not sure\n     * you want to do that here, so I'm using myString instead.\n     *\/\n    while( strcmp( fgets( item.myString, sizeof item.myString, stdin ), \" \" ) != 0 ) \n    {                                                        \n      myWord[s] = strdup(item.myString); \n    }           \n  }\n\n  \/**\n   * At this point, you've dynamically allocated memory for the elements\n   * of the myWords array, but you aren't using the array outside of this\n   * function and you aren't freeing that memory when you're done, meaning\n   * you have a memory leak.\n   *\n   * If they myWords array doesn't need to exist outside of this function,\n   * then you need to free each element before exiting.\n   *\/\n  return 0;       \n}\n<\/code><\/pre>\n<p>and then save all that as a <strong>.c<\/strong> file and compile it with a <strong>C compiler<\/strong> such as <code>gcc<\/code>.  <\/p>\n<p>If you want to write <em>C++ code<\/em>, then you should use the <code>fstream<\/code>, <code>string<\/code>, and <code>vector<\/code> types instead of <code>FILE<\/code> and array types, and you should take advantage of C++ features like the <code>std::copy<\/code> function template and stream iterators:<\/p>\n<pre><code>#include &lt;string&gt;\n#include &lt;vector&gt;\n#include &lt;fstream&gt;\n#include &lt;iterator&gt;\n#include &lt;algorithm&gt;\n\nclass assem\n{\n  public:\n\n    \/\/ don't need the temporary myString buffer\n\n    std::vector&lt; std::string &gt; myLines;\n\n    \/\/ since vectors know how big they are, you don't need a separate\n    \/\/ counter attribute.\n\n    int readFile( std::istream&amp; fileToBeRead );\n    int firstCheck( );\n    int printFile( );\n};\n\nint assem::readFile( std::istream&amp; fileToBeRead )\n{\n  \/**\n   * Use the copy function template with a stream iterator to \n   * read the input file into your myLines vector\n   *\/\n  std::copy( \n    std::istream_iterator&lt;std::string&gt;( fileToBeRead ), \/\/ start \n    std::istream_iterator&lt;std::string&gt;( ),              \/\/ end\n    back_inserter( myLines )                            \/\/ destination\n  );\n  return 0;\n}\n\nvoid assem::printFile( )\n{\n  \/**\n   * Use the same copy method to write each string in the myLines vector\n   * to standard output (again using a stream iterator), separated by \n   * newlines.  \n   *\/\n  std::cout &lt;&lt; std::endl;  \/\/ write the leading newline\n  std::copy(\n    myLines.begin(),                                          \/\/ start\n    myLines.end(),                                            \/\/ end\n    std::ostream_iterator&lt; std::string &gt;( std::cout, \"\\n\" )   \/\/ destination\n  );\n}\n\nint assem::checkFirst( )\n{\n  std::vector&lt; std::string &gt; myWords;\n\n  for ( std::vector::size_type s = 0; s &lt; myLines.size(); s++ )\n    std::cin &gt;&gt; myWords;\n\n  return 0;\n}\n\nint main( void )\n{\n  std::ifstream fileToBeRead( \"myfile.txt\" ); \/\/ or whatever its name is\n  if ( fileToBeRead )\n  {\n    assem myAssem;\n    myAssem.readFile( fileToBeRead );\n    fileToBeRead.close();\n    ...\n  }\n  ...\n}\n<\/code><\/pre>\n<p>then save that as a <strong>.cpp<\/strong> file and compile it with a <strong>C++ compiler<\/strong>, such as <code>g++<\/code>.  Notice that with the C++ code you don&#8217;t need to muck with <code>strdup<\/code> or worry about fixed array sizes, making the code a bit cleaner.  <\/p>\n<p>There are other issues with some of the program logic (what are the return values of <code>readFile<\/code> and <code>checkFirst<\/code> going to be used for, and is <code>0<\/code> the right value for that purpose), but this should at least get you on the right track.<\/p>\n<p>Hopefully.  <\/p>\n<p><strong>EDIT<\/strong> <\/p>\n<p>I realized the logic in the C++ <code>checkFirst<\/code> routine is not a correct translation of your original code.  I know I said you shouldn&#8217;t mix C-style strings and I\/O in C++ code, but sometimes you don&#8217;t have a choice.  Here&#8217;s something that should be similar:<\/p>\n<pre><code>#include &lt;cstring&gt;         \/\/ C++ header file for C-style string routines\n... \nchar buf[N];               \/\/ N is large enough for your input plus 0 terminator\nwhile ( std::cin.get( buf, sizeof buf ) &amp;&amp; std::strcmp( buf, \" \" ) != 0 )\n  myWords.push_back ( std::string( buf ) );\n<\/code><\/pre>\n<\/p><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">1<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved C++ errors for C code <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] First of all, you need to decide whether you&#8217;re writing C or C++. C does not support classes, and you should not use C-style strings, I\/O, and memory management routines in C++ code. Mixing elements of the two languages is a recipe for heartburn. C and C++ are completely different languages that happen to &#8230; <a title=\"[Solved] C++ errors for C code\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-c-errors-for-c-code\/\" aria-label=\"More on [Solved] C++ errors for C code\">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,943],"class_list":["post-11707","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-compiler-errors"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] C++ errors for C code - 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-errors-for-c-code\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] C++ errors for C code - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] First of all, you need to decide whether you&#8217;re writing C or C++. C does not support classes, and you should not use C-style strings, I\/O, and memory management routines in C++ code. Mixing elements of the two languages is a recipe for heartburn. C and C++ are completely different languages that happen to ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-c-errors-for-c-code\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-09-28T02:32:37+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=\"5 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-errors-for-c-code\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-c-errors-for-c-code\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] C++ errors for C code\",\"datePublished\":\"2022-09-28T02:32:37+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-c-errors-for-c-code\/\"},\"wordCount\":461,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\",\"compiler-errors\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-c-errors-for-c-code\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-c-errors-for-c-code\/\",\"name\":\"[Solved] C++ errors for C code - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2022-09-28T02:32:37+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-c-errors-for-c-code\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-c-errors-for-c-code\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-c-errors-for-c-code\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] C++ errors for C code\"}]},{\"@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] C++ errors for C code - 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-errors-for-c-code\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] C++ errors for C code - JassWeb","og_description":"[ad_1] First of all, you need to decide whether you&#8217;re writing C or C++. C does not support classes, and you should not use C-style strings, I\/O, and memory management routines in C++ code. Mixing elements of the two languages is a recipe for heartburn. C and C++ are completely different languages that happen to ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-c-errors-for-c-code\/","og_site_name":"JassWeb","article_published_time":"2022-09-28T02:32:37+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-c-errors-for-c-code\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-c-errors-for-c-code\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] C++ errors for C code","datePublished":"2022-09-28T02:32:37+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-c-errors-for-c-code\/"},"wordCount":461,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","compiler-errors"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-c-errors-for-c-code\/","url":"https:\/\/jassweb.com\/solved\/solved-c-errors-for-c-code\/","name":"[Solved] C++ errors for C code - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-09-28T02:32:37+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-c-errors-for-c-code\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-c-errors-for-c-code\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-c-errors-for-c-code\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] C++ errors for C code"}]},{"@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\/11707","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=11707"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/11707\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=11707"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=11707"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=11707"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}