{"id":2641,"date":"2024-05-10T11:06:42","date_gmt":"2024-05-10T11:06:42","guid":{"rendered":"https:\/\/www.skillvertex.com\/blog\/?p=2641"},"modified":"2024-05-10T11:06:42","modified_gmt":"2024-05-10T11:06:42","slug":"array-of-strings-in-c","status":"publish","type":"post","link":"https:\/\/www.skillvertex.com\/blog\/array-of-strings-in-c\/","title":{"rendered":"Array Of Strings In C"},"content":{"rendered":"\n<div class=\"wp-block-rank-math-toc-block\" id=\"rank-math-toc\" id=\"rank-math-toc\"><p>Table of Contents<\/p><nav><ul><li ><a href=\"#array-of-strings-in-c\">Array of Strings in C<\/a><\/li><li ><a href=\"#example\">Example<\/a><\/li><li ><a href=\"#invalid-operations-in-arrays-of-strings\">Invalid Operations in Arrays of Strings\u00a0<\/a><\/li><li ><a href=\"#array-of-pointers-of-strings\">Array of Pointers of Strings<\/a><\/li><li ><a href=\"#c-program-to-print-an-array-of-pointers\">C Program to print an array of pointers<\/a><\/li><li ><a href=\"#faq-array-of-strings-in-c\">FAQ- Array of Strings in C<\/a><\/li><\/ul><\/nav><\/div>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"array-of-strings-in-c\">Array of Strings in C<\/h2>\n\n\n\n<p>In C programming, a string is indeed a one-dimensional array of characters and is typically defined as an array of characters. Each string is terminated with a null character, represented as <code>'\\0'<\/code>, which marks the end of the string.<\/p>\n\n\n\n<p>An array of strings in C, often referred to as a &#8220;two-dimensional array of character types,&#8221; is essentially an array of arrays of characters. Each element in the first dimension is a string (which is an array of characters), and you can have multiple such strings in the second dimension. This creates a tabular structure, making it a 2D array. It&#8217;s commonly used to store and manage multiple strings, making it an application of a 2D array.<\/p>\n\n\n\n<p><strong>Syntax:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>char variable_name&#91;r] = {list of string};\n<\/code><\/pre>\n\n\n\n<p>In C, you can use variables with the following names and meanings:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>var_name<\/code>: This variable represents the name of a variable in your C program. It&#8217;s a placeholder for the actual variable name you want to use in your code. For example, if you want to create an integer variable, you can use <code>int var_name;<\/code> and then replace <code>var_name<\/code> with your desired variable name, like <code>int age;<\/code>.<\/li>\n\n\n\n<li><code>r<\/code>: This variable represents the maximum number of string values that can be stored in a string array. If you have an array of strings and want to specify the maximum number of strings it can hold, you can use <code>int r;<\/code> and assign the desired value to <code>r<\/code>.<\/li>\n\n\n\n<li><code>c<\/code>: This variable represents the maximum number of character values that can be stored in each string array. In the context of a 2D character array representing an array of strings, <code>c<\/code> would typically refer to the maximum length of each individual string. You can use <code>int c;<\/code> and assign the desired value to <code>c<\/code> to set the maximum length for each string in the array.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"example\">Example<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program to print Array \n\/\/ of strings\n#include &lt;stdio.h&gt;\n \n\/\/ Driver code\nint main()\n{\n  char arr&#91;3]&#91;10] = {\"Skill\", \n                     \"Skills\", \"Skillver\"};\n  printf(\"String array Elements are:\\n\");\n   \n  for (int i = 0; i &lt; 3; i++) \n  {\n    printf(\"%s\\n\", arr&#91;i]);\n  }\n  return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String array Elements are:\nSkill\nSkills\nSkillver<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"invalid-operations-in-arrays-of-strings\">Invalid Operations in Arrays of Strings&nbsp;<\/h2>\n\n\n\n<p>In C, you can directly change or assign values to an array of strings. An array of strings is essentially a 2D array of characters, where each row (or element in the first dimension) represents a string (a sequence of characters terminated by a null character &#8216;\\0&#8217;).<\/p>\n\n\n\n<p>Example<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>char arr&#91;3]&#91;10] = {\"Skill\", \"Skills\", \"Skillver\"};\n<\/code><\/pre>\n\n\n\n<p>We can&#8217;t directly change or put  a  value of array of string in C<\/p>\n\n\n\n<p>The value of <code>arr[0]<\/code> in the array <code>char arr[3][10] = {\"Skill\", \"Skills\", \"Skillver\"};<\/code> is the string &#8220;Skill.&#8221; So, <code>arr[0]<\/code> represents the string &#8220;Skill,&#8221; which is a sequence of characters ending with the null terminator &#8216;\\0&#8217;.<\/p>\n\n\n\n<p><strong>To change values we can use strcpy() function in C<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>strcpy(arr&#91;0],\"GFG\"); \/\/ This will copy the value to the arr&#91;0].\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"array-of-pointers-of-strings\">Array of Pointers of Strings<\/h2>\n\n\n\n<p>In C, you can use an array of pointers to represent an array of strings. This is often referred to as an array of pointers to characters, and it provides a more flexible way to manage strings compared to a 2D character array.<\/p>\n\n\n\n<p>Syntax<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>char *arr&#91;] = { \"Skill\", \"Skills\", \"Skillver\" };\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"c-program-to-print-an-array-of-pointers\">C Program to print an array of pointers<\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/ C Program to print Array \n\/\/ of Pointers\n#include &lt;stdio.h&gt;\n \n\/\/ Driver code\nint main()\n{\n  char *arr&#91;] = {\"Skill\", \"Skills\", \"Skillver\"};\n  printf(\"String array Elements are:\\n\");\n   \n  for (int i = 0; i &lt; 3; i++) \n  {\n    printf(\"%s\\n\", arr&#91;i]);\n  }\n  return 0;\n}<\/code><\/pre>\n\n\n\n<p>Output<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>String array Elements are:\nSkill\nSkills\nSkillver<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"faq-array-of-strings-in-c\">FAQ- Array of Strings in C<\/h2>\n\n\n<div id=\"rank-math-faq\" class=\"rank-math-block\">\n<div class=\"rank-math-list \">\n<div id=\"faq-question-1697025740896\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q1. How to get array of strings in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. In C, an array of strings is typically represented as a one-dimensional array of strings, where each element of the array is a pointer to a string (which is essentially a one-dimensional array of characters). This is often referred to as an array of pointers to characters.<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1697025749990\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q2. How do you write an array of strings?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. <strong>String[] strArray = new String[3]; strArray[0] = \u201cone\u201d; strArray[1] = \u201ctwo\u201d; strArray[2] = \u201cthree\u201d;<\/strong>\u00a0Here the String Array is declared first. Then in the next line, the individual elements are assigned values<\/p>\n\n<\/div>\n<\/div>\n<div id=\"faq-question-1697025756594\" class=\"rank-math-list-item\">\n<h4 class=\"rank-math-question \">Q3. What is the syntax of string in C?<\/h4>\n<div class=\"rank-math-answer \">\n\n<p>Ans. <strong>char string_name[size];<\/strong> is the syntax of string in  C<\/p>\n\n<\/div>\n<\/div>\n<\/div>\n<\/div>","protected":false},"excerpt":{"rendered":"<p>Array of Strings in C In C programming, a string is indeed a one-dimensional array of characters and is typically defined as an array of characters. Each string is terminated with a null character, represented as &#8216;\\0&#8217;, which marks the end of the string. An array of strings in C, often referred to as a &#8230; <a title=\"Array Of Strings In C\" class=\"read-more\" href=\"https:\/\/www.skillvertex.com\/blog\/array-of-strings-in-c\/\" aria-label=\"More on Array Of Strings In C\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":5364,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[27],"tags":[447],"class_list":["post-2641","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-c-programming","tag-array-of-strings-in-c","generate-columns","tablet-grid-50","mobile-grid-100","grid-parent","grid-33"],"_links":{"self":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2641","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/comments?post=2641"}],"version-history":[{"count":10,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2641\/revisions"}],"predecessor-version":[{"id":10718,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/posts\/2641\/revisions\/10718"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media\/5364"}],"wp:attachment":[{"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/media?parent=2641"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/categories?post=2641"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.skillvertex.com\/blog\/wp-json\/wp\/v2\/tags?post=2641"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}