Friday, May 21, 2010

Can someone explain what's array in c language?

Array is the perfect answer to a problem of declaring too much variables. How so? It's simply because an array represents a set of elements of same data type. Instead of declaring 50 variables using 50 different names, you could just use one name and just add index to it. Take for example a program with 20 variables, i could declare an array consisting of 20 elements and name that array discover. to access the first element of discover i'll just access discover[0] (because the index starts always zero and not 1) and to access the second element, i'll put discover[1], and so on until discover[19]. you could loop this by declaring another variable for the index. say, discover[num], in which num is a variable for the index. you could make this to input the variable :


for (num=0; num%26lt;20; num++)


{cin%26gt;%26gt;discover[num]; }


it is quite simple... all you need is practice...

Can someone explain what's array in c language?
Array is a group of similar kind of datatypes. Like you can have 10 elements array of integers in C. So, you can store 10 integers in that array. The advantage is instead of declaring 10 variables of int, you declare one array of 10 integers. Also, array is easier to use in For and other loops.





For exact definition, you can Google on internet or look in wikipedia.
Reply:ritz is about right.


just some additional details about something in the background that some people don't pay attention to. This is just general purpose knowledge.


In general an array in C or all other languages is a data structure to hold data of the same type. In C, when u declare an array it reserve the amount of memory for that array depending on the type u assign to the array (int, float, char, pointer, etc). For example, for a 10 cell character array, a slot of 10 bytes will be reserved in memory at execution (10 x 1 byte per char = 10 bytes). Likewise, for a 10 cell float array, a slot of 40 bytes will be reserved in memory (10 x 4 bytes per float = 40 bytes).


As far as I can tell, the way C is written, it is not possible to change the type of an array and u can only have one type of data in the array.


No comments:

Post a Comment