14. C-Arrays and Functions

C-Arrays: Previous                                                                                 Next: C-Strings
We have already  defined array and function in details. Functions an the array are  used together before we were passing  single element of an array to  function . In this section we will pass entire array to function. Array is passed to function just by writing the name.
Since  name of an array itself contains the base address of an array to pointer variable is used to accept the array in function .
Eg.
#include<stdio.h>
                                #include<conio.h>
                                Void main()
                                {
                                                float num [20];
                                                int i;
                                                Input(num );
                                }
                                void Input(float * nu)
                                {
                                                int i;
                                                for (i=0;I < 20;i++)
                                                {
                                                                printf(“\n  Enter numbers”);
                                                                 scanf (“%f”, n+i);
                                                }
                                }
Array and Pointers:
Array of pointers variables can be made. Suppose we are writing int *parr; in a pointer variable named *parr, an array element can be assigned since array element is accessed by following:
arr[0] for element stored at 0th index.
arr[1] for element stored at 1st  index.
And so on…
By using array and pointer together these value`s base addresses can be assigned to pointer variable. Suppose we have a array named arr[3]. Values are arr[3] = {2,3,8};
This can be shown as follows:
Name            Address              Value
arr[0]            2002                      2
arr[1]            2004                      3
arr[2]            2008                      8
Now, store arr[1] in a pointer variable name parr declared before.
parr = &arr[1];
It will display 2004, since pointer variable refers to base address.
If we perform scaling(Increment/Decrement) to a pointer variable, then pointer variable will point to another array element.
Eg:                parr++ == varr[2];
The advantage of this is, we can access each content of array using pointer variable scaling
parr = &arr[0]    [2002]
parr+1 = &arr[1]                [2004]
parr+2 = &arr[2]                [2006]
The following defined all statements are equal in C programming:
arr[1]                  3
                     *(arr+1)              3
                     *(1+arr)              3

Array of Pointers: -
As we can make array of simple variables, we can make array of pointer variables also. Since pointer variables contains the address of normal variable in same way array of pointer variables contains collection of addresses belonging to normal or simple variables.

Array of pointers are mainly used with string i.e. array of char data type`s value.

C-Arrays: Previous                                                                                 Next: C-Strings