ALLInterview.com :: Home Page KalAajKal.com
 Advertise your Business Here     
Browse  |   Placement Papers  |   Company  |   Code Snippets  |   Certifications  |   Visa Questions
Post Question  |   Post Answer  |   My Panel  |   Search  |   Articles  |   Topics  |   ERRORS new
   Refer this Site  Refer This Site to Your Friends  Site Map  Bookmark this Site  Set it as your HomePage  Contact Us     Login  |  Sign Up                      
tip   To Refer this Site to Your Friends   Click Here
Google
 
Categories  >>  Software  >>  Programming Languages  >>  C
 
 


 

 
 C interview questions  C Interview Questions
 C++ interview questions  C++ Interview Questions
 VC++ interview questions  VC++ Interview Questions
 Delphi interview questions  Delphi Interview Questions
 Programming Languages AllOther interview questions  Programming Languages AllOther Interview Questions
Question
what is the advantage of function pointer
 Question Submitted By :: Guest
I also faced this Question!!     Rank Answer Posted By  
 
  Re: what is the advantage of function pointer
Answer
# 1
code complexcity is less
 
Is This Answer Correct ?    4 Yes 6 No
Rajesh
 
  Re: what is the advantage of function pointer
Answer
# 2
It is useful when you want to send function as argument to 
another function.
 
Is This Answer Correct ?    13 Yes 3 No
Yogesh
 
 
 
  Re: what is the advantage of function pointer
Answer
# 3
Functional pointer are more readable and executed run time
 
Is This Answer Correct ?    4 Yes 2 No
Satty
 
  Re: what is the advantage of function pointer
Answer
# 4
function of pointer is  more useful for use of large 
function argument.it creats less complecty in function 
argument.
 
Is This Answer Correct ?    6 Yes 1 No
Maskfriend
 
  Re: what is the advantage of function pointer
Answer
# 5
Function pointers are very important while implementing 
Callback in C, for instance if Software has layered 
archiecture, Function pointer are used bye upper layer to 
register with below layer on certain conditions (i.e. some 
event or interrupt)
 
Is This Answer Correct ?    7 Yes 0 No
Mahend
 
  Re: what is the advantage of function pointer
Answer
# 6
TO implement call back functions.......
 
Is This Answer Correct ?    3 Yes 0 No
Ajay
 
  Re: what is the advantage of function pointer
Answer
# 7
Function pointers are very useful to send the function as a 
parameter to the another function.so,execution time will be 
saved.code complexity will be redused
 
Is This Answer Correct ?    4 Yes 1 No
Prakash
 
  Re: what is the advantage of function pointer
Answer
# 8
Answer:
Pointers to functions are interesting when you pass them to
other functions. A function that takes function
pointers says, in effect, “Part of what I do can be
customized. Give me a pointer to a function, and I’ll call
it when that part of the job needs to be done. That function
can do its part for me.” This is known as a
“callback.” It’s used a lot in graphical user interface
libraries, in which the style of a display is built into the
library but the contents of the display are part of the
application.
As a simpler example, say you have an array of character
pointers (char*s), and you want to sort it by the value
of the strings the character pointers point to. The standard
qsort() function uses function pointers to
perform that task. (For more on sorting, see Chapter III,
“Sorting and Searching Data.”) qsort() takes four
arguments,
u a pointer to the beginning of the array,
u the number of elements in the array,
u the size of each array element, and
u a comparison function,
and returns an int.
The comparison function takes two arguments, each a pointer
to an element. The function returns 0 if the
pointed-to elements compare equal, some negative value if
the first element is less than the second, and some
positive value if the first element is greater than the
second. A comparison function for integers might look
like this:
int icmp( const int *p1, const int *p2 )
{
return *p1 - *p2;
}
C Programming: 146 Just the FAQs
The sorting algorithm is part of qsort(). So is the exchange
algorithm; it just copies bytes, possibly by calling
memcpy() or memmove(). qsort() doesn’t know what it’s
sorting, so it can’t know how to compare them. That
part is provided by the function pointer.
You can’t use strcmp() as the comparison function for this
example, for two reasons. The first reason is that
strcmp()’s type is wrong; more on that a little later. The
second reason is that it won’t work. strcmp() takes
two pointers to char and treats them as the first characters
of two strings. The example deals with an array
of character pointers (char*s), so the comparison function
must take two pointers to character pointers
(char*s). In this case, the following code might be an
example of a good comparison function:
int strpcmp( const void *p1, const void *p2 )
{
char * const *sp1 = (char * const *) p1;
char * const *sp2 = (char * const *) p2;
return strcmp( *sp1, *sp2 );
}
The call to qsort() might look something like this:
qsort( array, numElements, sizeof( char * ), pf2 );
qsort() will call strpcmp() every time it needs to compare
two character pointers (char*s).
Why can’t strcmp() be passed to qsort(), and why were the
arguments of strpcmp() what they were?
A function pointer’s type depends on the return type of the
pointed-to function, as well as the number and
types of all its arguments. qsort() expects a function that
takes two constant void pointers:
void qsort( void *base,
size_t numElements,
size_t sizeOfElement,
int (*compFunct)( const void *, const void *) );
Because qsort() doesn’t really know what it’s sorting, it
uses a void pointer in its argument (base) and in
the arguments to the comparison function. qsort()’s void*
argument is easy; any pointer can be converted
to a void* without even needing a cast. The function pointer
is harder.
For an array of character arrays, strcmp() would have the
right algorithm but the wrong argument types. The
simplest, safest way to handle this situation is to pass a
function that takes the right argument types for
qsort() and then casts them to the right argument types.
That’s what strpcmp() does.
If you have a function that takes a char*, and you know that
a char* and a void* are the same in every
environment your program might ever work in, you might cast
the function pointer, rather than the pointedto
function’s arguments, in this way:
char table[ NUM_ELEMENTS ][ ELEMENT_SIZE ];
/* ... */
/* passing strcmp() to qsort for array of array of char */
qsort( table, NUM_ELEMENTS, ELEMENT_SIZE,
( int (*)( const void *, const void * ) ) strcmp );
Casting the arguments and casting the function pointer both
can be error prone. In practice, casting the
function pointer is more dangerous.
The basic problem here is using void* when you have a
pointer to an unknown type. C++ programs sometime
solve this problem with templates.
 
Is This Answer Correct ?    2 Yes 2 No
Vastram Naik
 
  Re: what is the advantage of function pointer
Answer
# 9
Function pointers are used to call functions at runt time 
instead of compile time .
 
Is This Answer Correct ?    4 Yes 0 No
Vels
 
  Re: what is the advantage of function pointer
Answer
# 10
It is more efficient. So it mostly used in real type 
application.
The use of pointer is to store address location of a 
variable or a function.as similar we are storing a variable 
address in a pointer variable and refering it ,we can store 
the address of a function in pointer variable.
 
Is This Answer Correct ?    0 Yes 0 No
Vidhya
 

 
 
 
Other C Interview Questions
 
  Question Asked @ Answers
 
Why preprocessor should come before source code?  2
write a program to generate 1st n fibonacci prime number  1
What is meaning of "Void main" in C Language. TCS8
code for copying two strings with out strcpy() function.  5
sqrt(x+sqrt(x+sqrt(x+sqrt(x))))=2; Find the value of x? Subex2
write a program for size of a data type without using sizeof() operator?  7
#define swap1(a,b) a=a+b;b=a-b;a=a-b; main() { int x=5,y=10; swap1(x,y); printf("%d %d\n",x,y); swap2(x,y); printf("%d %d\n",x,y); } int swap2(int a,int b) { int temp; temp=a; b=a; a=temp; return; } what are the outputs? Ramco4
Read N characters in to an array . Use functions to do all problems and pass the address of array to function. 1. Print only the alphabets . If in upper case print in lower case vice versa.  1
Write a program to print this triangle: * ** * **** * ****** * ******** * ********** Don't use printf statements;use two nested loops instead. you will have to use braces around the body of the outer loop if it contains multiple statements.  2
char ch=10;printf("%d",ch);what is the output Accenture11
what is the difference between const char *p, char const *p, const char* const p Accenture4
How do I declare an array of N pointers to functions returning pointers to functions returning pointers to characters?  1
You are given a string which contains some special characters. You also have set of special characters. You are given other string (call it as pattern string). Your job is to write a program to replace each special characters in given string by pattern string. You are not allowed to create new resulting string. You need to allocate some new memory to given existing string but constraint is you can only allocate memory one time. Allocate memory exactly what you need not more not less. Microsoft2
main() { int arr[5]={23,67}; printf("%d%d%d",arr[2],arr[3],arr[4]); } TCS6
biggest of two no's with out using if condition statement  2
write the program for maximum of the following numbers? 122,198,290,71,143,325,98  4
what is the hexidecimal number of 4100? Google14
Is it possible to create recycle bin in mobiles?  2
Write a programme to find even numbers without using any conditional statement? Infosys3
Will Macros support multiple arguments ? Oracle7
 
For more C Interview Questions Click Here 
 
 
 
 
 
   
Copyright Policy  |  Terms of Service  |  Help  |  Site Map 1  |  Articles  |  Site Map  |   Site Map  |  Contact Us interview questions urls   External Links 
   
Copyright © 2007  ALLInterview.com.  All Rights Reserved.

ALLInterview.com   ::  Forum9.com   ::  KalAajKal.com