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 et...

Basic SQL Commands in Oracle Database #Diksha Oracle Training Centre

  Basic SQL Commands in Oracle Database.   Hello Everyone, In my previous blog I discussed about SQL and Classification of SQL . Today I will discuss about SQL basic commands which widely used in RDBMS. The Topics of SQL command which I am going to cover in this blog are mainly divided into four Categories: ·         DDL:   DDL consists of commands which are used to define and design the database. ·         DML:   DML consists of commands which are mainly used to manipulate the data in the database. ·        DCL: DCL Consists of commands which deal with the user permissions/access and controls in the database. ·       TCL:   TCL Consist of commands which deal with the transaction in the database. If you want to explore theory part please follow my below blog as in today blog I will discuss about how to use the commands. h...

Pseudo Column in Oracle #Diksha Oracle Training Centre

  Pseudo Column in Oracle. Hello Everyone, Today I will discuss about Pseudo column in Oracle. This topic is very important so before going to read this blog, I suggest you to please see my previous blogs which is in archive folder for your reference so that it’s easy for you to understand this topic. Let’s discuss about Pseudo Column in Oracle. Pseudo column: A pseudo-column is dynamic in Oracle as it behaves like a table column but is not stored in the table. Pseudo column behaves like a table column, but is not actually stored in the table. You can select from pseudo columns, but you cannot insert, update, or delete their values. A pseudo column is also similar to a function without arguments. In   Simple word we can say Pseudo column is a column that yields a value when selected, but which is not an actual column of the table. An example is RowID or SysDate. It can be use in combination with the DUAL table. Below are the Pseudo Column commonly used in Oracle Dat...