Logo 
Search:

C Programming Articles

Submit Article
Home » Articles » C Programming » BeginnersRSS Feeds

Simple Array, 2D Array, Multidimensional Array

Posted By: Ryan Evans     Category: C Programming     Views: 19530

This article explains about arrays of single dimensional, 2D and Multidimensional array in C programming.

-  It’s a group of related data items that share a common name
-  Index number or subscript to differentiate the data items
-  Individual items are called elements
-  Arrays can be of any variable type
-  Ex. int roll_no [10]

One Dimensional Arrays

  • A single subscripted variable is called one dimensional array
  • In C x[5] represents the same concept as in mathematics X5 
  • To represent a set of five scores (75,12,33, 105, 67) by Tendulkar, we may define as follows, int Ten_scores[5];
  • Ten_scores[0] = 75 …

Elements

  • Space is reserved for each element as usual
  • Each element is treated as an individual variable
  • C provides no bound checking for array indices!
  • The subscript of an array can be an integer constant, integer variables, or an expression which yields integer value

Declaration of Arrays

  •  type_of_element    name_of_array[size]
  •  int amount[5], float marks[30] etc
  •  char name[10] can actually have only 9 characters in it!
  •  int ten_scores[5] = {75,12,33, 105, 67} initializes the array with respective values
  •  If lesser items are specified in the init list, other elements will have zeroes

Need For Static

  • Automatic arrays can not be initialized in K&R C, ANSI C allows that
  • In K&R C one need to have static arrays to initialize it
  • So if we write static word before initialization, it can work with old K&R compiler as well!

Other Issues for Initialization

  • The size may be omitted at the time of initialization like int no[]={2,4,7,4,6,9}
  • char name[]={‘S’,’y’,’n’,’t’,’a’,’x’} can also be written as
  • char name[]=“Syntax”
  • In C there is  no convenient way to initialize specified elements
  • No shortcut for large no of elements

Two Dimensional Arrays

Item1 Item2 Item3
SalesMan1 234 5876 4985
SalesMan2 123 4756 5869
SalesMan3 125 5964 5695
SalesMan4 241 5896 9876


Defining 2D Arrays

  •  type array_name [size1] [size2]
  •  int sales[4] [3] in above case
  •  As with single dimension arrays, each dimension of an array is indexed from 0 to 1- maximum size 
  •  The first index indicates row and the second index indicates column

Representation Of 2D Arrays

Column0 Column1 Column2
[0][0] [0][1] [0][2]
Row0 234 5876 4985
[1][0] [1][1] [1][2]
Row1 123 4756 5869
[2][0] [2][1] [2][2]
Row2 125 5964 5695
[3][0] [3][1] [3][2]
Row3 241 5896 9876


Initializing 2D Arrays

  •  static int table[2,3] = {1,3,5,7,9,2}
  •  static int table[2,3] = {{1,3,5}, {7,9,2}}
  •  static int table[2,3] = {{1,3,5}, {7,9,2}}
  •  Missing will initialized to zeros in static int[2,3] = { {1},{2,3} }
  •  Static int m[5] = {{0},{0},{0}} for all zeros

Multidimensional Arrays

  • C allows arrays of more than two dimensions.
  • Exact limit is determined by the compiler
  • Size is more imp than the no of dimensions
  • Survey of rainfall of last 3 years of all 12 months of five cities: int survey [3][12][5] 
  • View multidimensional arrays as repeated 2D arrays


  
Share: 


Didn't find what you were looking for? Find more on Simple Array, 2D Array, Multidimensional Array Or get search suggestion and latest updates.

Ryan Evans
Ryan Evans author of Simple Array, 2D Array, Multidimensional Array is from London, United Kingdom.
 
View All Articles

Related Articles and Code:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
Suren Dren from United States Comment on: Jul 02
Please read this article on

<a href="http://fresh2refresh.com/c/c-array/">c array</a>

Suren Dren from United States Comment on: Jul 02
Nice article on arrays with clear explanations. I also love to read the tutorial on c array

Vishal Sahu from India Comment on: Feb 04
how to create a header file
please give an example

View All Comments