Hidden facts about Arrays


What happens when we are trying to access array beyond its size??
lets go with an example
Execute the following code and see the output

#include<stdio.h>
#include<conio.h>
void main()
 {
   int a[10],i;
   a[12]=9;
   printf("%d",a[12]);
 }

It compile and runs without any error and generates output as 9 this is because there is no bound for arrays in C.
If we consider the same scenario in java we’ll get an exception i.e., ArrayIndexOutOfBounds Exception.
Consider above example and try to print address of a[0] and value of a both will give you the same.
means that address of a[0] is stored in a.
This is the reason why we are not comparing two character arrays (STRINGS) by using "==" if we compare two strings such as a[10],b[10] using "==" then address of a[0] and b[0] are compared not the entire strings.

FACTS-ABOUT-ARRAYS
Output of this Program is Not Equal
For comparing two strings we need to compare the characters present in that array but not addresses.
This is the reason why we go for string handling function strcmp() for comparing two strings.


Frenzz if you have any suggestions please suggest me by using comments




Share on Google Plus
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment