Advertisement

C programming tutorial 127 - Passing 1D array to function in C using array notation

C programming tutorial 127 - Passing 1D array to function in C using array notation Passing 1D (one dimensional) array to function in C:

W.K.T. an array name represents address of the first element in the array. So, whenever we pass an array to a function, address of the first element in the array is passed.

W.K.T. while calling a function; if we pass address of a memory location; then it is called as pass by reference or call by reference.

There are 2 different notations for passing an array to a function:

1. Array notation : [ ]
- basic syntax of declaring a function; for accepting 1D array using array notation
return_type functionName(datatype paramName[ ]);
Ex:
void display(int arr[ ],int arraySize);

2. Pointer notation : *
- basic syntax of declaring a function; for accepting 1D array using pointer notation
return_type functionName(datatype *paramName);
Ex:
void display(int *arr,int arraySize);

Note: While passing an array to a function; it is also recommended to pass its size

Example Code: Passing 1D array to a function using array notation

#include <stdio.h>

void displayIArray(int array[],int arraySize);

int main()
{
int array1[] = {1,2,3,4,5};
displayIArray(array1,5);

return 0;
}

void displayIArray(int array[],int arraySize)
{
int i=0;
for(i=0; i<arraySize; i++)
{
printf("%d\n",array[i]);
}
}

=========================================

Follow the link for next video:
C programming tutorial 128 - Passing 1D array to function in C using pointer notation


Follow the link for previous video:
C programming tutorial 126 - Parameter passing mechanism in C


=========================================

passing 1d array to function in c,passing array to function in c,how to pass array to function in c,passing array to function using array notation,passing array to function using pointer notation,passing array to function by reference,pass array by reference in c,c programming tutorial,c tutorial,best c tutorial on youtube,easy way to learn c,c programming language,

Post a Comment

0 Comments