Skip to main content

Pointer to a Function in C Program #DOTC_Mdb

Pointer to a Function in C Program.

 

Hi Everyone,

Today we will discuss about Pointer to a Function in C Program and in other word we can say uses of Function in Pointers. In my previous blogs we already discussed about Pointers and Functions so please go through that blog carefully before this topic. Previous topics like Pointers and Functions you can able to see it in my archive folder.

dheeraj60.blogspot.com

Let’s Discuss about Pointer to a function or Pointer uses in Function programs. Just like variable pointers can also be passed to a function. C program allows passing a pointer to a function. For doing this, we simply declare the function parameter as a pointer type. Let’s understand with example of passing pointer to a function in C.

 

#include <stdio.h>

#include <stdlib.h>

 

void increment(int  *incr, int a)

{

    *incr = *incr+a;

}

int main()

{

    int salary=0, incentive=0;

    printf(" Please enter the employee current salary:");

    scanf("%d", &salary);

    printf(" Please Enter the incentive:");

    scanf("%d", &incentive);

    increment(&salary, incentive);

    printf("Final salary of Employee: %d", salary);

    return 0;

}

Here we are passing a pointer to a function , we pass a pointer as an argument instead of a variable then the address of the variable is passed instead of the value. So any change made by the function using the pointer is permanently made at the address of passed variable.

Note : If you write the same program without pointer, you would find that the increment amount will not reflect in the salary, this is because the change made by the function would be done to the local variables of the function. When we use pointers, the value is changed at the address of variable. That’s the reason in output we see the salary with increments added in final value as salary.

Please see the output of above program in compiler code block.

Let’s take an other example where the  function, which can accept a pointer which can also accept an array in below program to calculate the average value of the numbers. Below is the Program.


#include<stdio.h>

#include<stdlib.h>

double Average(int *arr, int value);

 

int main () {

   int value1[4] = {3000, 2000, 300, 700};

   double avg;

   avg = Average( value1, 4) ;

   printf("Average value of the numberis: %f\n", avg );

   return 0;

}

 

double Average(int *arr, int value) {

 

   int  i, sum = 0;

   double avg;

 

   for (i = 0; i < value; ++i) {

      sum += arr[i];

   }

 

   avg = (double)sum / value;

   return avg;

}

 

Here in above code we declare function Average . and we use int arr with 4 elements. And after that we pass pointer to the array as an argument by using avg = Average( value1, 4)  then we print the output and calculate the average of the value by using function in a pointer with array value declared in the program.

 

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

Here you are able to see the output of average value in image .

Hope you are able to understand the usage of Pointers in Function , also if you have any confusion on program please refer my previous blogs of Functions and Pointers for your reference . Please read this blog carefully and try to write more programs in Function that can use pointer as well as array .  If you have any doubts let me know. In next blog i will come up with another topics.

Thanks.

Comments

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 etc. SQL allows

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 query the ma

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 some common columns or conditions and also by using alias to fulfill the condition.   There are various types of Joins as li