Showing posts with label Accessed Two-Dimensional Array Elements. Show all posts
Showing posts with label Accessed Two-Dimensional Array Elements. Show all posts

Tuesday, 25 October 2016

Passing Arrays to Function in C Programming

Passing Arrays to Function 

To pass a single-dimension array as an argument in a function, you would have to declare a formal parameter in one of following three ways. 
 1st method  Formal parameters as a pointer
2nd method  Formal parameters as a sized array
3rd method Formal parameters as an unsized array
Example :- 

Return Arrays from to Function 

C programming does not allow to return an entire array as an argument to a function.However, you can return a pointer to an array by specifying the array's name without an index.


 Example :-
 
Pointer to a Array 
An array name is a constant pointer to the first element of the array.

 balance is a pointer to &balance[0], which is the address of the first element of the array balance. Thus, the following program fragment assigns p as the address of the first element of balance
Example :- Once you store the address of the first element in ‘p’, you can access the array elements using *p, *(p+1), *(p+2), and so on.
 

Arrays Types in C Programming

Arrays are important to C and should need a lot more attention.

Multidimensional Arrays

C programming language allows multidimensional arrays.
Example:- 

Two-Dimensional Array

The simplest form of multidimensional array is the two-dimensional array. A two-dimensional array is, in essence, a list of one-dimensional arrays.
A two-dimensional array a, which contains three rows and four columns.

Initialized Two-Dimensional Array 

Multidimensional arrays may be initialized by specifying bracketed values for each row.
The nested braces, which indicate the intended row, are optional.

Accessed Two-Dimensional Array Elements 

An element in a two-dimensional array is accessed by using the subscripts, i.e., row index and column index of the array.
Example :-