-->
Search here and hit enter....

Array in C language ( Part 2 )

Hello Viewer! How are you I hope you well? In the previous lesson, we learn  Loop Control Instruction in C language. in this lesson we'll learn Array in C language PART 1...


BB ki vines

Array is very popular datatype for storing a collection of elements. This is because of the convenience. arrays lend themselves to programming. This features which make the arrays so convenient to program.

Array Initialization
In the previous post, we have used that array that did not have any values in them. we managed to store the value in them during program execution. let us now see how to initialize an array while declaring it. Understand with following examples...

int num [ 4 ] = { 2,4,6,8 } ;
int n [ ] = { 3,4,6,8,1,9 };
float a [ ] = { 3.3,4.3,4.58,.9 };

Remember:
a) Till the array elements are not given any specific values, they are supposed to contain garbage values.

b) If the array is initialized where its declared, mentioning the dimension of the array is optional as in the 2nd and 3rd example in above.

Array Elements in Memory
Suppose the following array declaration:
int arr [ 8 ];
What happens in memory when we make this declaration?  32 bytes get reserved in memory. ( because each integer datatype contain 4 bytes & here 8 integers then it takes 32 bytes )  and since the array is not initialized, all eight values present int would be garbage values. This so happens because the storage class of array is assumed to be auto. if the array class is declared to be static, then all the array elements would have a default initial values zero. whatever be the initial values, all the array elements would always be present in contiguous memory locations. This arrangement of array elements in memory is shown in the figure:


array in c , c++


Bounds Checking
In C there is no check to see if the subscript is used for an array exceeds the size of the array. Data entered with a subscript exceeding the array size will simply place in memory outside the arrays, probably on top of other data, or on the program itself. This will need to unpredictable result, to say the least, and there will be no error message to warn you that you are going beyond the array size. Some cases, the computer may just hang! See the following example...

#include
int main( )
{
 int n [ 40 ] , i ;
 for ( i = 0 ; i < = 100 ; i ++ )
 {
  n [ i ] = i ;
 }
return 0;
}

Thus to see to it that we do not reach beyond the array size.



Passing ArraysElements to a Function
Array elements can be passed to a function by calling the function by value, or by reference. In the call by value, we pass values of array elements to the function, whereas, in the call by reference, we pass addresses of array elements to the function. These two calls are elaborate below...

/* Call by Value */

PROGRAM :

#include
void display ( int ) ;
int main( )
{
 int i ;
 int marks [ ] = { 55, 65, 75, 56, 78, 45, 90 } ;
 for ( i=0 ; i < = 6 ; i++ )
 {
   display ( marks [ i ] ) ;
 }
return 0;
}

void display ( int m )
{
  printf ( " %d " , m ) ;
}

OUTPUT :
55 65 75 56 78 45 90


Here, we are passing an individual array element at a time to the function display ( ) and getting it printed in the function display ( ).


And Now /* Call by Reference */

PROGRAM :

#include
void disp ( int * );
int main ( )
{
 int i ;
 int marks [ ] = { 55, 65, 75, 56, 78, 45, 90 } ;
 for ( i =0 ; i< = 6 ; i ++ )
{
  disp ( &marks [ i ] );
}
return 0 ;
}
void disp ( int *n )
{
 printf (" %d " , *n );
}


OUTPUT :
55 65 75 56 78 45 90

Here, we are passing addresses of individual array elements to the function disp ( ).



Pointer and Arrays
To be able to see what pointers have got to do with arrays, let us first learn some pointer arithmetic. See the following program...

#include < stdio . h >
int main( ) 
{
 int i, *x  ;
 float j = 1.5, * y ;
 char k = ' c ' , *z ;
 printf ( " Value of  i = %d ", i ) ; 
 printf ( " Value of  j = %f\n ", j ) ; 
 printf ( " Value of  k = %c\n ", k ) ; 
 x = &i ;
 y = &j ;
 z = &k ;
printf ( " Original Address in x = %u\n " , x );
printf ( " Original Address in y = %u\n " , y );
printf ( " Original Address in z = %u\n " , z );
x ++ ;
y ++ ;
z ++ ;
printf ( " New Address in x = %u\n ", x );
printf ( " New Address in y = %u\n", y);
printf ( " New Address in z = %u\n ", z );
return 0 ;
}

OUTPUT :
Value of i = 3
Value of j = 1.500000
Value of k = c
Original Adress in x = 65524
Original Adress in y = 65520
Original Adress in z = 65519
New Adress in x = 65528
New Adress in y = 65524
New Adress in z = 65520

Observe the last line of the output. 65528 is original value in x plus 4, 65524 is original value in y plus 4, and 65520 is original value in z plus 1. This so happens because every time a pointer is incremented, it points to the immediately next location of its type. That is why when integer pointer x is incremented, it points to an address for locations after the current location since an int is always 4 bytes long. Similarly, y points to an address 4 location after the current location and z point 1 location after the current location. This is a very important result can be effectively used while passing the entire array to function. The way a pointer can be incremented, it can be decremented as well, to point to the earlier location.



The Real Thing
If you have grasped the concept of storage of array elements in memory and the arithmetic of pointers, here is some real food for thought, Once again consider the following array :

amit bhadana, pornhub


This is how we would declare the above array in C,

int num [ ] = { 24, 34, 12, 44, 56, 17 } ;

We also know, that on mentioning the name of the array, we get its base address, Thus, bu saying * num, we would be able to refer to the 0th element of the array, that is,24. One can easily see that *num and *( num + 0 ) both refer to 24.
Similarly, by saying *( num + 1 ), we can refer to the 1st element of the array, that is 34. In fact, this is what the C compiler does internally. when we say, num [ i ], the C compiler internally converts it to *( num + i ). This means that all the following notation are same :

num [ i ] 
*( num + i )
* ( i + num )
i [ num ] .


Summary:
1) An array is similar to an ordinary variable except that it can store multiple elements of similar type.

2) A compiler doesn't perform bounds checking on an array.
3) The array variable act as a pointer to the 0th element of the array. In a 1-D array, 0th element is a single value, whereas, in a 2-D array this element is 1-D array.
4) On incrementing a pointer it points to the next location of its type.
5) Array elements are stored in a continuous memory location and so they can be accessed using the pointer.
6) Ony limited arithmetic can be done on a pointer.





In this post are so enough 4 today. we'll discussed new topic in future post. I hope you like it.

Thank You 4 reading my blog!  if you have any problem or issue with this post please comment/contact.

Important :
 In this post, Article/Paragraph is taken from Let Us C book that is written by Yashavant Kanetkar.


FB COMMENTS ()