Arrays and String: 1. Single Dimension Array
Arrays and String: 1. Single Dimension Array
Knowledege
0 of 5 /
Votes
Arrays in C is a group of same type of variables refrenced under a common name using a index. String in C on other hand is another n character and has some special properties. Both Arrays and C are stored in continous memmory location and can be combined with po single or multidimension widening the field of situation which they can applied to. C Array and C string find wide application in c prog programming which you should master. Contents 1. 2. 3. 4. 5. 6. 7. 8. 9. Single Dimension Array Two Dimensional Array Multi Dimension Array Passing Array as Parameter Initalizing Array Variable Length Array Strings Pointers With Array Pointers With Strings
Here 'type' is the type of the varibles collected under the common name. 'name' is the common name used to refrence the collected vari variables collected in the array. One important thing is that in this declaration the size should be constant hence making array of this t very big restriction on arrays but dynamic variable length arrays can be created in C using dynamic memmory allocation. Also a new f arrays with great ease. Diagram below shows the logical memmory representation of 1-D Array (A - Array Name , X - Array Starting Memmory Location, V -
Each element in the array is stored in contiguous memmory location. Total size of array is product of size of the each variable and num
Elements in array are accessed thorugh an index with valid value 0 to n-1 if n is the size of the array.
One other point to be always kept in mind is that the array boundary can be overrunned since C provides no boundary checking and it very dangerous situation because your code is modifying memmory outside of what is allocated to it. You may write into some critical d may lead to severe program and O.S. crashes. Following C Source Code shows how to use a one dimensional array in C. // Code showing potential use of 1-D Array.
#include <stdio.h> int main () { int value [1000],i; // Declaring an Array for (i = 0; i<1000; i++) { value [i] = i; // Loading the Array } for (i = 0; i<1000; i++) { printf ("\n%d",value [i]); // Displaying the Array } return 0; }
Back To Top
Next in complexity to one dimensional array is Two dimensional array, also known as 2-D array. In simplest terms two dimensional ar
2-D arrays can be mentally represented as a matrix with 'size1' number of rows and 'size2' number of columns. This can be shown in th
Total size of the 2-D array = size1 * size2 * size of each variable.
In 2-D array also the index begins with 0 for both parameters. Row value can have possible values 0 to size1 - 1 and columns can have C Source code below shows the declaration and input/output operations on a 2-D array. // Code showing potential use of 2-D Array.
#include <stdio.h> int main () { int value [100] [100],i,j; // Declaring an Array for (j = 0; j<100; j++) { for (i = 0; i<100; i++) { value [j] [i] = j*100+i; // Loading the Array } } for (j = 0; j<100; j++) { for (i = 0; i<100; i++) { printf ("\n%d",value [j] [i]); // Displaying the Array } } return 0; } Back To Top
C Programming language allows more than 2-D array where most of other languages of its class doesnot showing that C is designed to
Though allowed array of greater dimension than 3-D is not used because size of the array increases exponentially as dimension increa decreasing the programs efficiency. Following diagram shows logical memmory representation of a 3-D array as array of 2-D arrays -
2006 cencyclopedia.com