473,657 Members | 2,574 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

* in front of function name?

Hi
Why put * in front of the function name in the declaration?

int (*write)(struct inode *, struct file *, const char *, int);

thanks
from Peter (cm****@hotmail .com)

Mar 4 '06 #1
9 5071
write is a pointer to functions which have four parameters, isn't it?

Mar 4 '06 #2
cm****@hotmail. com wrote:
Hi
Why put * in front of the function name in the declaration?

int (*write)(struct inode *, struct file *, const char *, int);

thanks
from Peter (cm****@hotmail .com)


It means that return type is pointer.
--
My Personal Weblog:
http://spaces.msn.com/cyberisblue
Mar 4 '06 #3
cm****@hotmail. com wrote:
Hi
Why put * in front of the function name in the declaration?

int (*write)(struct inode *, struct file *, const char *, int);

thanks
from Peter (cm****@hotmail .com)


The dec reads as:

write is a function pointer - and it should be set to point to a function
that returns an int, and that takes 4 parameters:

a struct inode *, a struct file *, a const char *, and an int
--
==============
*Not a pedant*
==============
Mar 4 '06 #4
yong wrote:
cm****@hotmail. com wrote:
Hi
Why put * in front of the function name in the declaration?

int (*write)(struct inode *, struct file *, const char *, int);

thanks
from Peter (cm****@hotmail .com)


It means that return type is pointer.


No it does not. (That would've been: int *write(/* params */);)

The other reply (which didn't quote context) was right. It declares
`write` as pointer to function with 4 parameters as described above.

Consider using `cdecl`.

--
BR, Vladimir

Beware of a dark-haired man with a loud tie.

Mar 4 '06 #5
Vladimir S. Oka wrote:
yong wrote:
cm****@hotmail. com wrote:
Hi
Why put * in front of the function name in the declaration?

int (*write)(struct inode *, struct file *, const char *, int);

thanks
from Peter (cm****@hotmail .com)

It means that return type is pointer.


No it does not. (That would've been: int *write(/* params */);)

The other reply (which didn't quote context) was right. It declares
`write` as pointer to function with 4 parameters as described above.


Which returns an `int`.
Consider using `cdecl`.


I should have pasted it's output in the first place. :-/

--
BR, Vladimir

She always believed in the old adage -- leave them while you're looking
good.
-- Anita Loos, "Gentlemen Prefer Blondes"

Mar 4 '06 #6
yong wrote:
cm****@hotmail. com wrote:
Hi
Why put * in front of the function name in the declaration?

int (*write)(struct inode *, struct file *, const char *, int);

thanks
from Peter (cm****@hotmail .com)


It means that return type is pointer.


No.

--
==============
*Not a pedant*
==============
Mar 4 '06 #7
cm****@hotmail. com writes:
Hi
Why put * in front of the function name in the declaration?

int (*write)(struct inode *, struct file *, const char *, int);


This does not declare a function; it declares a /pointer/ to a
function. write is a pointer to a function that takes four arguments
and returns int.
Mar 4 '06 #8

cm****@hotmail. com wrote:
Hi
Why put * in front of the function name in the declaration?

int (*write)(struct inode *, struct file *, const char *, int);

thanks
from Peter (cm****@hotmail .com)


This declares write as a pointer to a function taking 4 arguments and
returning an int. As to *why* you want to do this, there are a couple
of reasons:

1. You can pass a pointer to a function as a parameter to another
function; this is known as a callback, and is useful in a number of
contexts. The canonical example is the qsort() library function, which
can sort 1D arrays elements of any type. Here's how it's used:

/*
* prototype for qsort() as found in stdlib.h
*
* void qsort(void *base, size_t count, size_t size, int (*cmp)(const
void *, const void *));
*/
#include <stdlib.h>
#include <string.h>

int compareInts(con st void *arg1, const void *arg2)
{
int *a = arg1;
int *b = arg2;

if (*a < *b)
return -1;
else if (*a > *b)
return 1;
else
return 0;
}

int compareMyStruct s(const void *arg1, const void *arg2)
{
MyStruct *a = arg1;
MyStruct *b = arg2;

/*
* Assume the key value for MyStruct is a character string
*/
char *key1 = a->getKey();
char *key2 = b->getKey();

if (strcmp(a, b) < 0)
return -1;
else if (strcmp(a, b) > 0)
return 1;
else
return 0;
}

int main(void)
{
int iArr[20];
MyStruct sArr[30];

...

qsort(iArr, sizeof iArr/sizeof iArr[0], sizeof iArr[0],
compareInts);
qsort(sArr, sizeof sArr/sizeof sArr[0], sizeof sArr[0],
compareMyStruct s);

...

return 0;
}

The signal() library function does something similar; you pass it a
pointer to a function you want executed every time a particular signal
is raised:

void handleInterrupt (sig_atomic_t sig)
{
gInterrupt = 1;
}

int main(void)
{
signal(SIGINT, handleInterrupt );
...
}

2. With function pointers, you can implement a primitive form of
polymorphism, where you can refer to a generic "write" function in your
logic that resolves to one of several different functions based at
runtime. One project I did many years ago involved writing parsers for
a set of data files. I created a lookup table that was keyed by file
type and contained pointers to the actual parse functions, and a
function that performed the actual lookup. It was structured somewhat
like this:

extern void parseGraFile(ch ar *fileName);
extern void parsePWaveFile( char *fileName);
....

struct parseTable {
char *fileType;
void (*parse)(char *fileName);
};

static struct parseTable theTable[] = {{"GRA", parseGraFile}, {"PWV",
parsePWaveFile} , ...}

/*
* The following declaration reads as "lookup is a function returning a
pointer to a function
* taking a char * parameter and returning void"
*/
void (*lookup(char *fileType))(cha r *fileName)
{
size_t tableCount = sizeof theTable/sizeof theTable[0];
size_t i;

for (i = 0; i < tableCount; i++)
{
if (!strcmp(fileTy pe, theTable[i].fileType))
return theTable[i].parse;
}

return NULL;
}

char *typeFromName(c har *fileName)
{
/* parse the file type out of the file name and return it */
}

void parseFiles(cons t char **fileList, const size_t numFiles)
{
size_t i;

for (i = 0; i < numFiles; i++)
{
char *type;
void (*parser)(char *fileName);

type = typeFromName(fi leList[i]);
parser = lookup(type);
if (parser)
{
parser(fileList[i]);
}
else
{
/* handle unrecognized file type */
}
}
}

Basically, the right parsing function would be called based on the file
type. The advantage to this approach is that you don't have to hack
the main application logic every time a new file type is added or
removed; you just update the lookup table.

Mar 5 '06 #9
On 5 Mar 2006 06:25:56 -0800, "John Bode" <jo*******@my-deja.com>
wrote:
<snip>
This declares write as a pointer to a function taking 4 arguments and
returning an int. As to *why* you want to do this, there are a couple
of reasons:

1. You can pass a pointer to a function as a parameter to another
function; this is known as a callback, and is useful in a number of
contexts. The canonical example is the qsort() library function, which
can sort 1D arrays elements of any type. Here's how it's used:

/*
* prototype for qsort() as found in stdlib.h
*
* void qsort(void *base, size_t count, size_t size, int (*cmp)(const
void *, const void *));
*/
#include <stdlib.h>
#include <string.h>

int compareInts(con st void *arg1, const void *arg2)
{
int *a = arg1;
int *b = arg2;
These need to be const int * to avoid a constraint violation and
diagnostic. And should be anyway, since a comparison routine should
not be changing the things it compares, at least not simple things.

<snip> int compareMyStruct s(const void *arg1, const void *arg2)
{
MyStruct *a = arg1;
MyStruct *b = arg2;
ditto.
/*
* Assume the key value for MyStruct is a character string
*/
char *key1 = a->getKey();
char *key2 = b->getKey();
Perhaps ditto. And while it is certainly possible to have function
pointers in a struct, unless they vary, it would be more common to
have something more like:
const char *key1 = MyStruct_getKey (a); /* etc. */

Or even:
/*unconst*/ char key1 [KEY_SIZE], ...
MyStruct_getKey (a, key1); ...
if (strcmp(a, b) < 0)
return -1;
else if (strcmp(a, b) > 0)
return 1;
else
return 0;


The qsort comparator is only required to return <0/0/>0, not
specifically -1/0/+1, so you can just return strcmp (a, b).

<snip rest>

- David.Thompson1 at worldnet.att.ne t
Mar 20 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1820
by: windandwaves | last post by:
Hi Folk I am an PHP novice. Right now, I am redeveloping the wheel by creating my own type of PHP MyAdmin tool for my clients so that they can manage some data. A good exercise, but probably done before!
3
2342
by: Mark Horton | last post by:
I thought would be a super simple project, but I was wrong. I need Help!!!! I am using the Word.Application.Mailmerge.OpenDataSource to try and send a Dynamic Query to my stated Document.Open File. For the life of me I cannot fiqure out how to pass a parameter from this code to the "Proposal" Query in the Access Front End. If I do not add the param variable then everthing works fine except that it pulls all of the matching records...
2
2187
by: Lauren Quantrell | last post by:
In an Insert Into statement I have used the Host_Name() function to identify which user has suppied a record to a table that holds temporary data. I'm using an Access2K front end. Code: Alter procedure SPName @parameter1 int AS Set nocount On
49
14322
by: Yannick Turgeon | last post by:
Hello, We are in the process of examining our current main application. We have to do some major changes and, in the process, are questionning/validating the use of MS Access as front-end. The application is relatively big: around 200 tables, 200 forms and sub-forms, 150 queries and 150 repports, 5GB of data (SQL Server 2000), 40 users. I'm wondering what are the disadvantages of using Access as front-end? Other that it's not...
5
17654
by: Lauren Wilson | last post by:
Hi folks, Somewhere, I recently saw an article header titled: "How to compact and Repair a back-end Access db from the front-end using VBA" or words to that effect. Now that I need it, I cannot find it. Does anyone know were to find such an article?
5
1798
by: rdemyan via AccessMonster.com | last post by:
I have code in my front end that opens a form for backing up the front end. I'll give a brief description of what the form does: 1) When the backup form opens, it closes all open forms except for three hidden forms and the backup form itself. 2) It automatically creates a backup name for the front end and displays the folder where it will be backed up. 3) The user clicks the backup button and the following code executes:
8
9628
by: Greg Strong | last post by:
Hello All, The short questions are 1 Do you know how to make DSN connection close in Access to Oracle 10g Express Edition? &/or 2 Do you know how to make a DSN-less pass-through query work from
7
1876
by: John | last post by:
I design the front end of my app in MyAppName-develop.mdb. When I want to deploy it I copy it and change the name to MyAppName.mdb. In the development mdb I want a different start up option then in the mdb for the users: start up with no database window but with the main menu form of my app. Is there a way to prevent myself from having to change the start up settings manually everytime I want to deploy my app. How do you guys do that?...
14
9432
by: Brian Nelson | last post by:
Sorry for the long post, I've tried so hard to solve this one but I'm just stuck! We've had a multiuser .mdb file on a network share for years. I know it's not ideal, but it's worked well from Access 2000 to 2003. Ever since we upgraded to 2007, we've noticed some serious bloating. When we were on 2003, after a fresh compact, the database would be at about 20 MB. After normal use it would grow to about 25 MB and then significantly...
0
8392
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8305
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8823
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8730
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7321
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1950
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.