{"id":33641,"date":"2023-02-11T05:19:17","date_gmt":"2023-02-10T23:49:17","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-what-does-char-a5050-mean-in-c\/"},"modified":"2023-02-11T05:19:17","modified_gmt":"2023-02-10T23:49:17","slug":"solved-what-does-char-a5050-mean-in-c","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-what-does-char-a5050-mean-in-c\/","title":{"rendered":"[Solved] What does char a[50][50] mean in C?"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-57081241\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"57081241\" data-parentid=\"57073226\" data-score=\"1\" 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><code>char a[50][50]<\/code> declares <code>a<\/code> as a 50-element array of 50-element arrays of <code>char<\/code>.  That means each <code>a[i]<\/code> is a 50-element array of <code>char<\/code>.  It will be laid out in memory like:<\/p>\n<pre><code>   +---+\na: |   | a[0][0]\n   +---+\n   |   | a[0][1]\n   +---+\n   |   | a[0][2]\n   +---+\n    ...\n   +---+\n   |   | a[0][49]\n   +---+\n   |   | a[1][0]\n   +---+\n   |   | a[1][1]\n   +---+\n    ...\n   +---+\n   |   | a[1][49]\n   +---+\n   |   | a[2][0]\n   +---+ \n    ...\n<\/code><\/pre>\n<p>This code is storing up to 50 <em>strings<\/em>, each up to 49 characters long, in <code>a<\/code> (IOW, each <code>a[i]<\/code> can store a 49-character string).  In C, a <em>string<\/em> is a sequence of character values including a 0-valued terminator.  For example, the string <code>\"hello\"<\/code>, is represented as the sequence <code>{'h', 'e', 'l', 'l', 'o', 0}<\/code>.  That trailing <code>0<\/code> marks the end of the string.  String handling functions and output functions like <code>puts<\/code> and <code>printf<\/code> with the <code>%s<\/code> specifier need that 0 terminator in order to process the string correctly.  <\/p>\n<p>Strings are stored in arrays of character type, either <code>char<\/code> (for ASCII, UTF-8, or EBCDIC character sets) or <code>wchar_t<\/code> for &#8220;wide&#8221; strings (character sets that require more than 8 or so bits to encode).  An N-character string requires an array that&#8217;s <em>at least<\/em> N+1 elements wide to account for the 0 terminator.  <\/p>\n<p>Unless it is the operand of the <code>sizeof<\/code> or unary <code>&amp;<\/code> operator, or is a string literal used to initialize an array of character type, an <em>expression<\/em> of type &#8220;N-element array of <code>T<\/code>&#8221; will be converted (&#8220;decay&#8221;) to an expression of type &#8220;pointer to <code>T<\/code>&#8220;, and the value of the expression will be the address of the first element of the array. <\/p>\n<p>When you call<\/p>\n<pre><code>gets( a[i] );\n<\/code><\/pre>\n<p>the expression <code>a[i]<\/code> is converted from type &#8220;50-element array of <code>char<\/code>&#8221; to &#8220;pointer to <code>char<\/code>&#8220;, and the value of the expression is the address of the first element of the array (<code>&amp;a[i][0]<\/code>)<sup>1<\/sup>.  <code>gets<\/code> will read characters from standard input and store them to the array starting at that address.  Note that <code>gets<\/code> is no longer part of the standard C library &#8211; it was removed in the 2011 version of the standard because it is unsafe.  C does not require any sort of bounds checking on array accesses &#8211; if you type in more characters than the target buffer is sized to hold (in this case, 50), those extra characters will be written to memory immediately following the last element of the array, which can cause all sorts of mayhem.  Buffer overflows are a popular malware exploit. You should replace the <code>gets<\/code> call with<\/p>\n<pre><code>fgets( a[i], 50, stdin );\n<\/code><\/pre>\n<p>which will read up to 49 characters into <code>a[i]<\/code> from standard input.  Note that any excess characters are left in the input stream.<\/p>\n<p>Also, the behavior of <code>fflush<\/code> is not defined for input streams<sup>2<\/sup> &#8211; there&#8217;s no good, safe, <em>portable<\/em> way to clear excess input except to read it using <code>getchar<\/code> or <code>fgetc<\/code>.<\/p>\n<\/p>\n<hr>\n<p><sup><\/p>\n<ol>\n<li>This is why you got the error message you did when you changed <code>a<\/code> from <code>char [50][50]<\/code> to <code>char [50]<\/code> &#8211; in that case, <code>a[i]<\/code> has type <code>char<\/code>, not <code>char *<\/code>, and the value of <code>a[i]<\/code> is not an <em>address<\/em>.\n<\/li>\n<li>Microsoft&#8217;s Visual Studio C compiler is a notable exception &#8211; it will clear excess input from the input stream.  However, that&#8217;s specific to MSVC, and not portable across different compilers.  The operation is also a little nonsensical with respect to &#8220;flush&#8221; semantics.\n<\/li>\n<\/ol>\n<p><\/sup><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\"><\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved What does char a[50][50] mean in C? <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] char a[50][50] declares a as a 50-element array of 50-element arrays of char. That means each a[i] is a 50-element array of char. It will be laid out in memory like: +&#8212;+ a: | | a[0][0] +&#8212;+ | | a[0][1] +&#8212;+ | | a[0][2] +&#8212;+ &#8230; +&#8212;+ | | a[0][49] +&#8212;+ | | a[1][0] &#8230; <a title=\"[Solved] What does char a[50][50] mean in C?\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-what-does-char-a5050-mean-in-c\/\" aria-label=\"More on [Solved] What does char a[50][50] mean 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":[324,362],"class_list":["post-33641","post","type-post","status-publish","format-standard","hentry","category-solved","tag-c","tag-string"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>[Solved] What does char a[50][50] mean 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-what-does-char-a5050-mean-in-c\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] What does char a[50][50] mean in C? - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] char a[50][50] declares a as a 50-element array of 50-element arrays of char. That means each a[i] is a 50-element array of char. It will be laid out in memory like: +---+ a: | | a[0][0] +---+ | | a[0][1] +---+ | | a[0][2] +---+ ... +---+ | | a[0][49] +---+ | | a[1][0] ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-what-does-char-a5050-mean-in-c\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2023-02-10T23:49:17+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=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-does-char-a5050-mean-in-c\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-does-char-a5050-mean-in-c\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] What does char a[50][50] mean in C?\",\"datePublished\":\"2023-02-10T23:49:17+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-does-char-a5050-mean-in-c\/\"},\"wordCount\":483,\"publisher\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#organization\"},\"keywords\":[\"c++\",\"string\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-does-char-a5050-mean-in-c\/\",\"url\":\"https:\/\/jassweb.com\/solved\/solved-what-does-char-a5050-mean-in-c\/\",\"name\":\"[Solved] What does char a[50][50] mean in C? - JassWeb\",\"isPartOf\":{\"@id\":\"https:\/\/jassweb.com\/solved\/#website\"},\"datePublished\":\"2023-02-10T23:49:17+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-does-char-a5050-mean-in-c\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/jassweb.com\/solved\/solved-what-does-char-a5050-mean-in-c\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/jassweb.com\/solved\/solved-what-does-char-a5050-mean-in-c\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/jassweb.com\/solved\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] What does char a[50][50] mean 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] What does char a[50][50] mean 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-what-does-char-a5050-mean-in-c\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] What does char a[50][50] mean in C? - JassWeb","og_description":"[ad_1] char a[50][50] declares a as a 50-element array of 50-element arrays of char. That means each a[i] is a 50-element array of char. It will be laid out in memory like: +---+ a: | | a[0][0] +---+ | | a[0][1] +---+ | | a[0][2] +---+ ... +---+ | | a[0][49] +---+ | | a[1][0] ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-what-does-char-a5050-mean-in-c\/","og_site_name":"JassWeb","article_published_time":"2023-02-10T23:49:17+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-what-does-char-a5050-mean-in-c\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-what-does-char-a5050-mean-in-c\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] What does char a[50][50] mean in C?","datePublished":"2023-02-10T23:49:17+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-what-does-char-a5050-mean-in-c\/"},"wordCount":483,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["c++","string"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-what-does-char-a5050-mean-in-c\/","url":"https:\/\/jassweb.com\/solved\/solved-what-does-char-a5050-mean-in-c\/","name":"[Solved] What does char a[50][50] mean in C? - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2023-02-10T23:49:17+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-what-does-char-a5050-mean-in-c\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-what-does-char-a5050-mean-in-c\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-what-does-char-a5050-mean-in-c\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] What does char a[50][50] mean 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\/33641","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=33641"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/33641\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=33641"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=33641"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=33641"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}