Sunday, 21 January 2018

Declaration of Arrays


Declaration of Arrays

Declaration of Arrays
 Like any other variables, arrays must be declared before they are used.The general form of array declaration is type variable-name [size];
The type specifies the type of elements that will be contained in the array, such as int,float or char and the size indicates the maximum number of elements that can be stored inside the array.

For example: float height[50]; declares the height to be an array containing 50 real
elements. Any subscript 0 to 49 are valid.  Similarly,int group[10];
declares the group to be an array to contain maximum of 10 integer constants.Remember, any reference to the array outside the declared limits would not necessarily cause an

error.Rather, it might result in unpredictable program results.The C language treats character string simply as array of  characters. The size in a character string represents the maximum number of characters that the string can hold.For instance,
           char name[10];
   declares the name as a character array(string) variable that can hold a maximum of 10 characters.Suppose we declare the following string constant into the string variable name.
WELL DONE  each character of the string is treated as an element of the array name and is stored in the memory as follows:
          W
          E
          L
          L

          D
          O
          N
          E
          \0
     When the compiler sees a character string, it terminates it with an additional null character.
Thus,the element name[9] holds the null character \0 at the end. When declaring character array,we must always allow one  extra element space for the null terminator.

Related Posts:

  • Two Dimensional Array Of CharactersTwo Dimensional Array Of CharactersOur example program asks you to type your name.When you do so,it checks against a master list to see if you are worthy of entry to the palace.Example:    #include "string.h"&n… Read More
  • Pointers And StringsPointers And Strings    We cannot assign a string to another,whereas we can assigna char pointer to another char pointer. This is shown with an example:main(){ char str1[]="hello"; char str2[10]; char… Read More
  • Arrays With In The StructuresArrays With In The Structures C permits the use of arrays as structure members.we can use single or multidimensional arrays of type int or float.Ex:   struct marks      {      &nbs… Read More
  • Structures and FunctionsStructures and FunctionsThe main philosophy of C language is the use of functions.C supports the passing of structure values as arguments to function. In this ,the values of a structure can be transferred from one function to… Read More
  • Introduction To StructuresIntroduction To StructuresArrays can be used to represent a group of data items that belong to same type,such as int or float.However, If we want to represent a collection of data items of different types using a single name,… Read More

0 comments:

Post a Comment