Monday, May 24, 2010

What is an array in C?

Array = multi-element box, a bit like a filing cabinet, and uses an indexing system to find each variable stored within it.





The following link gives more info abt using Arrays in C.





http://gd.tuwien.ac.at/languages/c/progr...

What is an array in C?
An array is an organized sequential collection of similar data types.


For ex:


int marks[3];





declares three integer variables named 'marks' and indexed from 0 through 2 (i.e. marks[0], marks[1] and marks[2]).


Generally all the variables of the array are stored sequentially in the contiguous memory space.
Reply:ARRAY:


In row-major storage, a multidimensional array in linear memory is accessed such that rows are stored one after the other. It is the approach used by the C programming language as well as many other languages, with the notable exception of Fortran.





When using row-major order, the difference between addresses of array cells in increasing rows is larger than addresses of cells in increasing columns. For example, consider this 2×3 array:





1 2 3


4 5 6


Declaring this array in C as





int A[2][3] = { {1, 2, 3}, {4, 5, 6} };


would find the array laid-out in linear memory as:





1 2 3 4 5 6


The difference in offset from one column to the next is 1 and from one row to the next is 3. The linear offset from the beginning of the array to any given element A[row][column] can then be computed as:





offset = row + column*NUMROWS


where NUMROWS represents the number of rows in the array—in this case, 2.





[edit]


Column-major order


Column-major order is a similar method of flattening arrays onto linear memory, but the columns are listed in sequence. The programming language Fortran uses column-major ordering. The array





1 2 3


4 5 6


if stored in memory with column-major order would look like the following:





1 4 2 5 3 6


With columns listed first. The memory offset could then be computed as:





offset = row*NUMCOLS + column


Where NUMCOLS is the number of columns in the array.





It is possible to generalize both of these concepts to arrays with greater than two dimensions. For higher dimension arrays, the ordering determines which dimension of the array is listed off first. Any of the dimensions could be listed first, just the same way that a two-dimensional array could be listed column-first or row-first. The difference in offset between listings of that dimension would then be determined by a product of other dimensions. It is uncommon to have any variation except ordering dimensions first to last or last to first--equating to row-major and column-major respectively.





Treating a row-major array as a column-major array is the same as transposing it.
Reply:Arrays and Strings


In principle arrays in C are similar to those found in other languages. As we shall shortly see arrays are defined slightly differently and there are many subtle differences due the close link between array and pointers. We will look more closely at the link between pointer and arrays later in Chapter 9.





Single and Multi-dimensional Arrays


Let us first look at how we define arrays in C:








int listofnumbers[50];





BEWARE: In C Array subscripts start at 0 and end one less than the array size. For example, in the above case valid subscripts range from 0 to 49. This is a BIG difference between C and other languages and does require a bit of practice to get in the right frame of mind.





Elements can be accessed in the following ways:-











thirdnumber=listofnumbers[2];


listofnumbers[5]=100;





Multi-dimensional arrays can be defined as follows:











int tableofnumbers[50][50];





for two dimensions.





For further dimensions simply add more [ ]:











int bigD[50][50][40][30]......[50];





Elements can be accessed in the following ways:











anumber=tableofnumbers[2][3];


tableofnumbers[25][16]=100;
Reply:Array is a group of elements where all the elements that are stored are same type. In simple Array is a way to store Homogeneous Elements. Memories are allocated conquetively
Reply:Arrays in C act to store related data under a single variable name with an index, also known as a subscript. It is easiest to think of an array as simply a list or ordered grouping of variables. As such, arrays often help a programmer organize collections of data efficiently and intuitively.
Reply:int a[100];


This is an array declaration in C.


This instructs the compiler to find and allocate a contiguos area of the size 100 * sizeof (int).


You can access each element of the array using a subscript (or index). e.g. : a[5] or a[i] (where i is an integral type: (unsigned) int / long / char).


The catch with subscripts is that it must reside in the range [0, 99] in this case. If you declare a larger or smaller array, the subscript must be in the range [0, SIZE - 1]. If you don't respect this rule, the compiler WILL NOT give you any warning, it'll accept any (positive?) index as valid, but at runtime you'll get a 'nice' crash if you try to write that area (e.g. write in a[100] in our case).


The downside of using arrays is that you WILL need a CONTIGUOS area, and if that area is too big, it will fail big time. So my advice is use pointers if you are unsure how big is the area you need. In C, you have *alloc (...) and free (...).


Usage of arrays and pointers are somehow interchangeable, but you need to read some materials in order to understand that.


All you need to remeber when working with arrays is double-checking the index and not using too big of a value for the size.


Also, you can use another type for the values in the array (double, float, unsigned long, you name it, even pointers: void* a[256]).


Another catch is: the size of the array must be constant. Not as in const int SIZE = 100, but as in an actual number. Because consts are not actually consts, they're read - only variables. A #define SIZE 100 would do the job, but use #defines only in C code, not C++.


Remember to always think twice if you really need arrays . If the size of the data stored varies at runtime, then you need to take a journey down The Pointer Path. It'll be bumpy, but VERY useful.


I also recommend using C++ instead of C if you have the choice ('cause it has the wonderful two instructions, new and delete to use instead of *alloc (...) and free (...), respectively)


No comments:

Post a Comment