C

Topic: Variables

Explain the variable assignment in the declaration

int *(*p[10])(char *, char *); It is an array of function pointers that returns an integer pointer. Each function has two arguments which in turn are pointers to character type variable. p[0], p[1],....., p[9] are function pointers.return type : integer pointer.p[10] : array of function pointerschar * : arguments passed to the function Program: Example program to explain function pointers.#include<stdio.h>#include<stdlib.h>int *(*p[10])(char *, char *); //average function which returns pointer to integer whose value is average of ascii value of characters passed by pointersint *average(char *, char *); //function which returns pointer to integer whose value is sum of ascii value of characters passed by pointersint *sum(char *, char *);int retrn;int main(void) {int i;for (i = 0; i < 5; i++) {//p[0] to p[4] are pointers to average function. p[i] = &(average);}for (i = 5; i < 10; i++) {//p[5] to p[9] are pointers to sum functionp[i] = &(sum);}char str[10] = "vediklabs.in";int *intstr[10];for (i = 0; i < 9; i++) {//upto p[4] average function is called, from p[5] sum is called. intstr[i] = p[i](&str[i], &str[i + 1]);if (i < 5) {//prints the average of ascii of both charactersprintf(" \n average of %c and %c is %d",str[i], str[i + 1],*intstr[i]);}else {//prints the sum of ascii of both characters.printf(" \n sum of %c and %c is %d",str[i], str[i + 1], *intstr[i]);}}return 0;}//function average is defined hereint *average(char *arg1, char *arg2) {retrn = (*arg1 + *arg2) / 2;return (&retrn);}                                                     //function sum is defined hereint *sum(char *arg1, char *arg2) {retrn = (*arg1 + *arg2);return (&retrn);}Output:average of n and o is 110average of o and d is 105average of d and a is 98 average of d and a is 98average of a and l is 102average of l and o is 109sum of o and . is 157sum of . and c is 145sum of c and o is 210sum of o and m is 220Explanation:In this program p[10] is an array of function pointers. First five elements of p[10] point to the function: int *average(char *arg1,char *arg2). Next five elements point to the function int *sum(char *arg1,char *arg2). They return pointer to an integer and accept pointer to char as arguments. Function average:int *average(char *arg1,char *arg2) This function finds the average of the two values of the addresses passed to it as arguments and returns address of the average value as an integer pointer. Function sum:int *sum(char *arg1,char *arg2) This function finds the sum of the two values of the addresses passed to it as arguments and returns address of the sum value as an integer pointer. 

Browse random answers:

How do I write a function that takes variable number of arguments?
What are the different properties of variable number of arguments?
Define the scope of static variables.
What are volatile variables?
What is the benefit of using an enum rather than a #define constant? 
Constant volatile variable declaration is possible or not? if give any one example and reason?
What is an lvalue? 
Can a variable be both constant and volatile ? 
What are register variables? What are the advantages of using register variables?
Can we specify variable field width in a scanf() format string? If possible how?
Difference between pass by reference and pass by value?
Left shifting a number by 1 is always equivalent to multiplying it by 2 ?
Bitwise & and | are unary operators?
What is scope &amp; storage allocation of register, static and local variables?
What is the difference between 'for' and 'while' loops?
Swap two variables without using third variable. 
Are the variables argc and argv are always local to main?
What are the two forms of #include directive?
Write a program to swap two numbers using a temporary variable.
Where is the auto variables stored?
 What is a constant variable?
What are the advantages of auto variables?
Is it acceptable to declare/define a variable in a C header?
Difference between : - 1)Global variable and Local variable , 2)Static variable and Global variable ?
Differentiate between an internal static and external static variable?
What does a static variable mean?
How can you determine the maximum value that a numeric variable can hold?
In order to properly use a variable?
Can a variable be both const and volatile?
What is static identifier?
Constant volatile variable declaration is possible or not? if give any one example and reason.?
What is the difference between declaring a variable and defining a variable? 
Are the variables argc and argv are local to main()?
Can static variables be declared in a header file?
Where does global, static, local, register variables, free memory and C Program instructions get stored?
Are the variables argc and argv are always local to main?
Where are the auto variables stored? What is the use of register variables?
What is difference between static and global static variable?
How much memory does a static variable takes? 
What is a pointer variable? 
What is the type of the variable b in the following declaration?
#define FLOATPTR float*FLOATPTR a,b;
What is storage class and what are storage variable ?
Declaration of variables in c?
Explanation of value of a variable in c programming language by examples and questions and answers
How to swap the content of two variables without a temporary variable
Where in memory are my variables stored?
Do variables need to be initialized?
Should variables be stored in local blocks?
What is a register variable?
What is scope & storage allocation of extern and global variables?
What is the difference between 'for' and 'while' loops?
Are the variables argc and argv are local to main?
How do I print the contents of environment variables?
Explain the variable assignment in the declaration