{"id":443,"date":"2023-06-28T12:46:26","date_gmt":"2023-06-28T12:46:26","guid":{"rendered":"http:\/\/localhost\/embeddedwala\/?p=443"},"modified":"2023-07-27T04:11:51","modified_gmt":"2023-07-27T04:11:51","slug":"arrays-in-c","status":"publish","type":"post","link":"https:\/\/embeddedwala.com\/beta\/codes\/arrays-in-c\/","title":{"rendered":"Arrays in C"},"content":{"rendered":"\t\t<div data-elementor-type=\"wp-post\" data-elementor-id=\"443\" class=\"elementor elementor-443\" data-elementor-post-type=\"post\">\n\t\t\t\t\t\t\t\t\t<section data-particle_enable=\"false\" data-particle-mobile-disabled=\"false\" class=\"elementor-section elementor-top-section elementor-element elementor-element-6c9e470 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"6c9e470\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-top-column elementor-element elementor-element-093f682\" data-id=\"093f682\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t\t\t<section data-particle_enable=\"false\" data-particle-mobile-disabled=\"false\" class=\"elementor-section elementor-inner-section elementor-element elementor-element-a8c9ce6 elementor-section-boxed elementor-section-height-default elementor-section-height-default\" data-id=\"a8c9ce6\" data-element_type=\"section\">\n\t\t\t\t\t\t<div class=\"elementor-container elementor-column-gap-default\">\n\t\t\t\t\t<div class=\"elementor-column elementor-col-100 elementor-inner-column elementor-element elementor-element-6cd5668\" data-id=\"6cd5668\" data-element_type=\"column\">\n\t\t\t<div class=\"elementor-widget-wrap elementor-element-populated\">\n\t\t\t\t\t\t\t\t<div class=\"elementor-element elementor-element-b051999 elementor-widget elementor-widget-text-editor\" data-id=\"b051999\" data-element_type=\"widget\" data-widget_type=\"text-editor.default\">\n\t\t\t\t<div class=\"elementor-widget-container\">\n\t\t\t\t\t\t\t<p>Arrays are a fundamental data structure in programming that allows to store a collection of variables of the same data type. In C, arrays are used extensively for data manipulation and processing. In this blog, basics of arrays in C, how to declare and initialize them, and how to access and manipulate array elements are captured.<\/p><p><strong>Declaring and Initializing Arrays<\/strong><\/p><p>In C, an array can be declared by specifying its data type and size, as shown in the following example:<\/p><div><pre>int myArray[5];\n<\/pre><\/div><p>This creates an integer array named\u00a0<code>myArray<\/code>\u00a0with a size of 5. By default, all elements in the array are initialized to 0.<\/p><p>An array can also be initialized when it is declared by assigning a list of values to the elements, as shown in the following example:<\/p><div><pre>int myArray[] = {1, 2, 3, 4, 5};\n<\/pre><\/div><p>This creates an integer array named\u00a0<code>myArray<\/code>\u00a0with a size of 5 and initializes its elements to the values 1, 2, 3, 4, and 5.<\/p><p><strong>Accessing Array Elements<\/strong><\/p><p>User can access individual elements in an array by their index, which starts from 0. For example, to access the first element in\u00a0<code>myArray<\/code>, following code can also be used:<\/p><div><pre>int firstElement = myArray[0];\n<\/pre><\/div><p>Similarly, to access the third element,\u00a0 the following code would be used:<\/p><div><pre>int thirdElement = myArray[2];\n<\/pre><\/div><p><strong>Modifying Array Elements<\/strong><\/p><p>In an array, individual element can be modified by assigning a new value to the element&#8217;s index, as shown in the following example:<\/p><div><pre>myArray[0] = 10;\n<\/pre><\/div><p>This changes the value of the first element in\u00a0<code>myArray<\/code>\u00a0from 1 to 10.<\/p><p><strong>Iterating Over Arrays<\/strong><\/p><p>User can iterate over the elements in an array using a loop. For example, the following code prints all the elements in\u00a0<code>myArray<\/code>:<\/p><div><pre>for (int i = 0; i &lt; 5; i++) \n{\n    printf(\"%d\\n\", myArray[i]);\n}\n<\/pre><\/div><p>This loops through each element in\u00a0<code>myArray<\/code>\u00a0and prints its value to the console.<\/p><p><strong>Multi-Dimensional Arrays In C<\/strong><\/p><p>Multi-dimensional arrays can be created by declaring an array of arrays. For example, the following code declares a 2-dimensional integer array with a size of 3&#215;3:<\/p><div><pre>int myArray[3][3] = \n{\n    {1, 2, 3},\n    {4, 5, 6},\n    {7, 8, 9}\n};\n<\/pre><\/div><p>This creates a 2-dimensional integer array named\u00a0<code>myArray<\/code>\u00a0with 3 rows and 3 columns.<\/p><p>To access an individual element in a multi-dimensional array, two indices can also be used, as shown in the following example:<\/p><div><pre>int element = myArray[1][2];\n<\/pre><\/div><p>This accesses the element in the second row and third column of\u00a0<code>myArray<\/code>. Below share code is showcasing the usage of an array in C.<\/p><div><pre>#include &lt;stdio.h&gt;\n\nint main() \n{\n    \/\/ declare and initialize an integer array with a size of 5\n    int myArray[5] = {1, 2, 3, 4, 5};\n\n    \/\/ access the first element in myArray\n    int firstElement = myArray[0];\n    printf(\"First element: %d\\n\", firstElement);\n\n    \/\/ modify the third element in myArray\n    myArray[2] = 10;\n    printf(\"Modified array: \");\n    for (int i = 0; i &lt; 5; i++) \n    {\n        printf(\"%d \", myArray[i]);\n    }\n    printf(\"\\n\");\n\n    \/\/ create a 2-dimensional integer array with a size of 3x3\n    int my2DArray[3][3] = \n    {\n        {1, 2, 3},\n        {4, 5, 6},\n        {7, 8, 9}\n    };\n\n    \/\/ access the element in the second row and third column of my2DArray\n    int element = my2DArray[1][2];\n    printf(\"Element at (1, 2): %d\\n\", element);\n\n    return 0;\n}\n<\/pre><\/div><p>This code first declares and initializes an integer array with a size of 5, and then accesses the first element and modifies the third element. It then creates a 2-dimensional integer array with a size of 3&#215;3, and accesses an element in the second row and third column of the array.<\/p><p><strong><em>When the code is executed, the output will be:<\/em><\/strong><\/p><div><pre>First element: 1\nModified array: 1 2 10 4 5 \nElement at (1, 2): 6\n<\/pre><\/div><p>This output shows that the first element in the\u00a0<code>myArray<\/code>\u00a0is accessed correctly and the third element is modified from 3 to 10. The output also shows that the element at (1, 2) in\u00a0<code>my2DArray<\/code>\u00a0is accessed correctly and is equal to 6.<\/p><p>At the end, it can be concluded that Arrays are a fundamental data structure in programming that allow you to store multiple values of the same data type in a single variable. In C, arrays are used extensively for data manipulation and processing. In this blog, we explored the basics of arrays in C, including how to declare and initialize them, how to access and modify array elements, how to iterate over arrays, and how to create multi-dimensional arrays.<\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t\t<\/div>\n\t\t<\/div>\n\t\t\t\t\t\t\t<\/div>\n\t\t<\/section>\n\t\t\t\t\t\t\t<\/div>\n\t\t","protected":false},"excerpt":{"rendered":"<p>Arrays are a fundamental data structure in programming that allows to store a collection of&#8230;<\/p>\n","protected":false},"author":1,"featured_media":444,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[29],"tags":[112,113,114,44],"class_list":["post-443","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-codes","tag-array","tag-array-code","tag-c-language","tag-coding"],"acf":[],"views":392,"featured_image_urls":{"full":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Array-c.png",520,350,false],"thumbnail":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Array-c.png",150,101,false],"medium":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Array-c.png",300,202,false],"medium_large":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Array-c.png",520,350,false],"large":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Array-c.png",520,350,false],"1536x1536":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Array-c.png",520,350,false],"2048x2048":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Array-c.png",520,350,false],"tp-image-grid":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Array-c.png",520,350,false],"covernews-slider-full":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Array-c.png",520,350,false],"covernews-slider-center":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Array-c.png",520,350,false],"covernews-featured":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Array-c.png",520,350,false],"covernews-medium":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Array-c.png",505,340,false],"covernews-medium-square":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Array-c.png",371,250,false],"_nx_notification_thumb":["https:\/\/embeddedwala.com\/beta\/wp-content\/uploads\/2023\/06\/Array-c.png",100,67,false]},"author_info":{"display_name":"embeddedwala","author_link":"https:\/\/embeddedwala.com\/beta\/author\/embeddedwala\/"},"category_info":"<a href=\"https:\/\/embeddedwala.com\/beta\/category\/codes\/\" rel=\"category tag\">Codes<\/a>","tag_info":"Codes","comment_count":"0","_links":{"self":[{"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/posts\/443","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/comments?post=443"}],"version-history":[{"count":5,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/posts\/443\/revisions"}],"predecessor-version":[{"id":6192,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/posts\/443\/revisions\/6192"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/media\/444"}],"wp:attachment":[{"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/media?parent=443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/categories?post=443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/embeddedwala.com\/beta\/wp-json\/wp\/v2\/tags?post=443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}