Skip to main content

Array to Function in C Program #DOTC_Mdb

Array to Function Program in C.

Hi Everyone,

Today we will discuss about Array to Function in C. This Topic is interesting and we can use array in C functions like function call value and function call by reference. Before starting this topic please go through below topics.

https://dheeraj60.blogspot.com/2020/06/array-and-its-beneifts-in-c-program.html

https://dheeraj60.blogspot.com/2020/06/multi-dimensional-array2d-and-3d-in-c.html

https://dheeraj60.blogspot.com/2020/06/call-by-reference-function-uses-and.html

https://dheeraj60.blogspot.com/2020/06/function-call-value-uses-and-benefits.html

 

Let’s Discuss about Array to function. Just like variables, array can also be passed to a function as an argument as of now we used variables in functions. Passing array elements to a function is similar to passing variables to a function.

 

Let’s understand passing simple array in Function by below program.

 

#include <stdio.h>

#include <stdlib.h>

void value(int marks1, int marks2)

{

    printf("%d\n", marks1);

    printf("%d\n", marks2);

}

 

int main()

{

    int markarray[] = {80, 90, 95, 85};

 

    value(markarray[1], markarray[2]);

    return 0;

}

 

Here we simply use value function for passing second and third element of Array. In other word we can say Passing second and third elements to function value(). Array we already discussed in previous blog so you can take reference from that blog. So output will be 90 and 95

 

Below is the output of above program in compiler Code block.

 

 

Now understand with different program by using passing array to function using call by method by below simple program.

 

#include <stdio.h>

#include <stdlib.h>

void dotc( char abc)

{

   printf("\n%c ", abc);

}

int main()

{

   char arr[] = {'a', 'b', 'c', 'd', 'e'};

   for (int i=0; i<5; i++)

   {

       dotc (arr[i]);

   }

    return 0;

}

Here in above program we use character variable   and I am passing each element one by one using subscript and function dotc . For iteration we use loop. So output will be a b c d e

Below is the output of above program in compiler code-block.

 

 

Hope you are able to understand both the program. Let’s understand how we can use array to function using call by reference method.

Note: When we pass the address of an array while calling a function then this is called function call by reference and when we pass an address as an argument, the function declaration should have a pointer as a parameter to receive the  address. We already discuss this in my previous blog here we use the same in array. Below is the Program  for printing value from 1 to 10 by using array and function call by reference method.

#include <stdio.h>

#include <stdlib.h>

void value( int *a)

{

    printf("%d \n ", *a);

}

 

int main()

{

     int arr[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};

     for (int i=0; i<10; i++)

     {

         value (&arr[i]);

     }

 

    return 0;

}

 

Here  value (&arr[i]) passing address of array elements. So we use function value which we declare as void value int data type and by using array and printing the value from 1 to 10.

 

Hope you are able to understand above programs by using Arrays in Function .

Below is the output of above program in compiler code block.

 

To understand more we can use 2D(two dimensional) array in C function Program.

Below is the 2D array program used by Function and taking input from user:

#include <stdio.h>

#include <stdlib.h>

void display(int a[2][2]);

int main()

{

    int a[2][2];

    printf(" Please enter 4 numbers:\n");

    for (int i = 0; i < 2; ++i)

        for (int j = 0; j < 2; ++j)

            scanf("%d", &a[i][j]);

        display(a);

    return 0;

}

 

void display(int a[2][2])

{

    printf("Display the value:\n");

    for (int i = 0; i < 2; ++i) {

        for (int j = 0; j < 2; ++j) {

           printf("%d\n", a[i][j]);

        }

    }

}

 

Here we use display function for passing two-dimensional array to a function. Hope you are able to understand this program as in previous blogs we already discussed about 2D array.

 

Below is the output of above Program in compiler code block.

 

   

Hope  you are able to understand how to use arrays in several functions used in C program and also we use 2D array to display value by using function . Please go through this blog carefully and write some programs  if any issue let me know . In next blog i will come up with new topic Structure in C programming which is important in C program. Please go through all previous blogs so that it’s easy for you to understand structure in C program.

Thanks.

Comments

Post a Comment

Popular posts from this blog

SQL and Classification of SQL in Oracle Database #Diksha Oracle Training Centre

  SQL and Classification of SQL in Oracle Database.   SQL is Structured Query Language , which is used for storing, manipulating and retrieving data stored in a relational database .SQL is the standard language for RDBMS. All the Relational Database Management Systems (RDMS) like Oracle, MySQL, Sybase, Informix, IBM DB2 and Microsoft SQL Server use SQL as their standard database language. Oracle is one of the more secured database as compared to other databases. Importance of   SQL : SQL and PL/SQL is a backend process where all data is stored and retrieved in GUI which created either by any programming languages like Java, C++, PHP etc. so we need to have very secure database so that there will be no impact for users. SQL allows users to access data in the relational database management systems. SQL is used to communicate with a database.SQL and PL/SQL allows users to create and drop databases tables , views , stored procedures , functions , packages , trigger et...

Materialized View uses and Benefits in Database #DOTC_Mdb

Materialized View uses and Benefits in Database. Hello Everyone, Today we will discuss about Materialized view as it’s play important role in database. We already discussed about Simple Views and complex views in my previous blog. Before Materialized view go through my previous blog which related to simple view. https://dheeraj60.blogspot.com/2020/05/benefits-of-creating-and-using-view-in.html As we know View is not a database object and not like table which is stored in database, but view can be created from base table a view is an SQL statement that’s stored in the database. This statement, or view, has a name.A view looks and acts a lot like a table. It has columns and rows, and can be included in SELECT queries just like a table. In other word we can say View is a virtual/logical table which is basically used for security purpose. Let’s understand   about   Materialized view : A materialized view is a view that stores the results of the view’s query. Whenever you que...

Top 50 Interview Questions On SQL and PL/SQL #DOTC_Mdb

                    Top 50 Interview Questions On SQL and PL/SQL. Today we will Discuss Top 50 interview questions and answers of SQL and PL/SQL which is frequently asked in interview.     Question 1: What is SQL and Classification of SQL? Answer SQL is a Structure Query Language which is vastly used in RDBMS database like Oracle, Sybase, DB2 , Microsoft SQL server etc.   Classification of SQL is as below: DDL (Data Definition Language):  Commands are  create , alter , drop , truncate etc DML (Data Manipulation Language) : Commands are  insert,update and delete . TCL (Transaction Control Language ) : Commands are  Commit , Rollback and Save point. DCL (Data Control Language) : Commands are Grant , Revoke Question 2:    What is meant by Joins? What are the types of join? Answer Joins are basically used to extract/get data from multiple tables using s...