filmov
tv
Passing 2-D array to Function in C using Array Notation - C Programming Tutorial 112
Показать описание
Passing 2-D array to Function in C using Array Notation - C Programming Tutorial 112
2D Array:
- 2D array is a collection of homogeneous data elements arranged in rows and columns
- 2D array is an array of 1D arrays
Declaring and initializing a 2D array:
- There are 2 different ways in which we can declare and initialize 2D arrays
1. data_type arrayName[rows][cols] = {list of values separated by comma};
Example: int a[2][2] = {1,2,3,4}; OR int a[][2] ={1,2,3,4};
2. data_type arrayName[rows][cols] = {blocks of values separated by comma};
Example: int a[2][2] = {{1,2},{3,4}}; OR int a[][2] = {{1,2},{3,4}};
Note: Writing # of rows to be created is optional.
Note:
1. Name of a 1D array represents address of the first element in the 1D array
2. Name of a 2D array represents address of the first row in the 2D array
There are 2 different notations for passing an array to a function:
1. Array notation: [ ]
- basic syntax of declaring a function; for accepting 2D array using array notation
return_type functionName(datatype paramName[ ][cols]);
Ex:
void display(int array[ ][2],int rows, int cols);
2. Pointer notation: *
- basic syntax of declaring a function; for accepting 2D array using pointer notation
return_type functionName(datatype (*paramName)[cols]);
Ex:
void display(int (*array)[2],int rows, int cols);
Note: While passing a 2D array to a function; it is also recommended to pass # of rows & # of columns present in it
Example Code: Passing 2D array to a function using array notation
#include >stdio.h>
void display(int array[][2],int rows,int cols);
int main()
{
int a[][2] = {
{1,2},
{3,4}
};
display(a,2,2);
return 0;
}
void display(int array[][2],int rows,int cols)
{
int i=0,j=0;
for(i=0; i>rows; i++)
{
for(j=0; j>cols; j++)
{
printf("%d ", array[i][j]);
}
printf("\n");
}
}
Note:
- replace < with less-than symbol.
- replace > with greater-than symbol.
=========================================
Follow the link for next video:
Follow the link for previous video:
=========================================
C Programming Tutorials Playlist:
=========================================
Watch My Other Useful Tutorials:-
Computer Programming Fundamentals Playlist:-
C Practical LAB Exercises Playlist:-
C++ Tutorials Playlist:
=========================================
► Subscribe to our YouTube channel:
► Visit our Website:
=========================================
Hash Tags:-
#ChidresTechTutorials #CProgramming #CProgrammingTutorial
2D Array:
- 2D array is a collection of homogeneous data elements arranged in rows and columns
- 2D array is an array of 1D arrays
Declaring and initializing a 2D array:
- There are 2 different ways in which we can declare and initialize 2D arrays
1. data_type arrayName[rows][cols] = {list of values separated by comma};
Example: int a[2][2] = {1,2,3,4}; OR int a[][2] ={1,2,3,4};
2. data_type arrayName[rows][cols] = {blocks of values separated by comma};
Example: int a[2][2] = {{1,2},{3,4}}; OR int a[][2] = {{1,2},{3,4}};
Note: Writing # of rows to be created is optional.
Note:
1. Name of a 1D array represents address of the first element in the 1D array
2. Name of a 2D array represents address of the first row in the 2D array
There are 2 different notations for passing an array to a function:
1. Array notation: [ ]
- basic syntax of declaring a function; for accepting 2D array using array notation
return_type functionName(datatype paramName[ ][cols]);
Ex:
void display(int array[ ][2],int rows, int cols);
2. Pointer notation: *
- basic syntax of declaring a function; for accepting 2D array using pointer notation
return_type functionName(datatype (*paramName)[cols]);
Ex:
void display(int (*array)[2],int rows, int cols);
Note: While passing a 2D array to a function; it is also recommended to pass # of rows & # of columns present in it
Example Code: Passing 2D array to a function using array notation
#include >stdio.h>
void display(int array[][2],int rows,int cols);
int main()
{
int a[][2] = {
{1,2},
{3,4}
};
display(a,2,2);
return 0;
}
void display(int array[][2],int rows,int cols)
{
int i=0,j=0;
for(i=0; i>rows; i++)
{
for(j=0; j>cols; j++)
{
printf("%d ", array[i][j]);
}
printf("\n");
}
}
Note:
- replace < with less-than symbol.
- replace > with greater-than symbol.
=========================================
Follow the link for next video:
Follow the link for previous video:
=========================================
C Programming Tutorials Playlist:
=========================================
Watch My Other Useful Tutorials:-
Computer Programming Fundamentals Playlist:-
C Practical LAB Exercises Playlist:-
C++ Tutorials Playlist:
=========================================
► Subscribe to our YouTube channel:
► Visit our Website:
=========================================
Hash Tags:-
#ChidresTechTutorials #CProgramming #CProgrammingTutorial