Array in C

  • Definition: Array is one of the secondary datatypes, which is useful for storing a similar type of data in a contiguous manner (side by side).
  • 2nd Definition: Array is a collection of similar elements which are in side by side memory locations.
  • Syntax:
datatype variable_name[<number of variable or elements>];

For example:

int a[5];      //a is an array of 5 integer elements

char ch[3];   //ch is an array of 3 character elements

float f[4];    //f is an array of 4 float elements
  • Here int a[5] that points the size of each element as 4bytes so the total size of the array is 20bytes.
  • Similarly, for character ch[3] size is 3bytes and,
  • For float f[4] size is 16bytes.

 

  • For storing data in given memory location indexing is used.

Untitled Diagram (5) (2) (1).jpg

There are two types of stacks: upgrowing and downgrowing.

  • Remember that array indexing always starts with zero. i.e. a[0]

For example:

main()
{
   int a[5]  = {10,20,30,40,50},elements;
   elements = sizeof(a)/sizeof(a[0]);

   printf("number of elements %d \n",elements);
   printf("%d \n",sizeof(a[0]));
}


amswer : number of elements 5
         4

Partial Initialization:

  • If we need 5 elements, but we initialized only 3 elements:

i.e. a[5] = {10,20,30};

output = 10 20 30 0 0

  • So rest will be taken as zero.
  • In array elements are stored in a little – endian manner.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

Powered by WordPress.com.

Up ↑

%d bloggers like this: