Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use of function pointer ???

Status
Not open for further replies.

isaisa

Programmer
May 14, 2002
97
IN
Hi..
I came accross a function pointer article while reading Ivor Horton's C++ book. I got the grammer to use it. But can anyone tell me the exact use of it and in what condition they can be best utilised ????
As per my knowledge,function pointers are used in writting your own ISRs ???

Please suggest....

Thanks in advance..
Sanjay
 
Function pointers are generally more useful in C than they are in C++. C++'s object-oriented facilities tend to provide better ways of solving the same kinds of problems that are solved using function pointers in C.

In C, there are many uses for them, so it's hard to answer your question in a few sentences. So, I'll try in a few more sentences :)

Consider the standard function atexit() which is used to register functions to be called at program termination:

int atexit (void (* function)(void));

It accepts a pointer to a function that accepts no parameters and returns void. To use it, you would define one or more functions of the same type somewhere:

void cleanup1(void)
{
puts("cleanup1");
}

void cleanup2(void)
{
puts("cleanup2");
}

And then call atexit() like this:

atexit(cleanup1);
atexit(cleanup2);

Another example. Suppose you are writing a C program to process an HTML document. The program scans the document for tags and for each tag it finds, it invokes a different function depending on the tag. To locate the appropriate tag, you could create a lookup table composed of the tag name and function pointer (note that the table is arranged in ascending order by tag name):

typedef void (*tag_f)(char **,int,const char *);

struct tag {
char *tag_name;
tag_f tag_func;
} tag_funcs[]={
{"A",anchor_func},
{"BODY",body_func},
{"BR",br_func}
};

As the lexer code that scans the html document encounters each tag, it does a binary search in the lookup table for a match. It if finds a match, it then calls the appropriate function. with the tag attributes and, for some tags, any text that appears in between the opening and closing tags.

Here's a simple definition for anchor_func() that prints out the tag's attributes and url text:

void anchor_func(char **attribs,int attrib_count,const char *url)
{
int i;
printf("url=%s\n",url);
for (i=0;i<attrib_count;++i) {
printf(&quot;attribute %d=%s\n&quot;,
i+1,attribs);
}
}

Obviously, each function can do something vastly different. But the code inside the lexer is simplified somewhat because a single function pointer can be used to call each function.

There are lots and lots more ways to use function pointers. These will undoubtedly become apparent to you as you continue to program in C.
Russ
bobbitts@hotmail.com
 
An application that I have uses function pointers within a struct. It is for data processing and there are n functions with the same skeleton. For simplicity, i will bring it down to 3 funcitons.

typedef void (*pProcessFxn) ( char* buffer)
struct data{
char* dataType;
pProcessFxn fxn;
}

void fpf(char* buffer)
{
cout<<atof(buffer);
}

void ipf(char* buffer)
{
cout<<atoi(buffer);
}

void spf(char* buffer)
{
cout<<buffer;
}

data myData = {
{&quot;FLOAT PROCESS FUNCTION&quot;,fpf},
{&quot;INTEGER PROCESS FUNCTION&quot;,ipf},
{&quot;STRING PROCESS FUNCTION&quot;,spf}};

enum FxnTypes
{
FLOAT_FXN,INT_FXN,STR_FXN
};

void main()
{
ifstream infile(&quot;input.txt&quot;);

char buffer[128];
infile.getline(buffer,128);

if(*buffer == 'F' || *buffer == 'f')
{
cout<<myData[FLOAT_FXN].dataType<<end;
cout<<myData[FLOAT_FXN].f(buffer);
}
}

yes this is just a basic example. I have about 2000 different types i read in an process based on encodings in a file. These types are sorted in a list in ascending order by datatype and I use bsearch to find the appropriate location in my array to call the funciton from by searching. So now I have all these different datatypes but I dont care, I get the appropriat function back along with its type and value. I can then process the data and output to a file.

I hope this helped

matt

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top