'typedef' issue

R

Roman Mashak

Hello,

consider the following code snippet:

typedef int (read_proc_t)(char *page, char **start, off_t off,int count,
int *eof, void *data);

struct proc_dir_entry {
unsigned short low_ino;
unsigned short namelen;
...
read_proc_t *read_proc;
write_proc_t *write_proc;
...
}

struct proc_dir_entry *test_entry;

Suppose I succesfully initialized 'test_entry' (it's not NULL). Why can't
I declare the function like this:

test_entry->read_proc = test_proc_read;
static read_proc_t test_proc_read(char *page, char **start, off_t off
int count, int *eof, void *data)
{ ... }

I get an error: `test_proc_read' declared as function returning a
function"

I thought as I declared new type with typedef I can use it?

Thanks for explanations.
 
P

Peter Nilsson

Roman Mashak said:
consider the following code snippet:

typedef int (read_proc_t)(char *page, char **start, off_t off,int
count, int *eof, void *data);

This declares read_proc_t as having function type.
static read_proc_t test_proc_read(char *page, char **start, off_t off
int count, int *eof, void *data)
{ ... }

This declares (and defines) a function as returning a function.
I get an error: `test_proc_read' declared as function returning a
function"

There is no lambda calculus in C. A function cannot return a function.
A function can return a function pointer though...

static read_proc_t *test_proc_read(char *page, char **start, off_t
off, int count, int *eof, void *data)

Note the extra *.
 
B

Ben Pfaff

Roman Mashak said:
typedef int (read_proc_t)(char *page, char **start, off_t off,int count,
int *eof, void *data); ....
static read_proc_t test_proc_read(char *page, char **start, off_t off
int count, int *eof, void *data)

read_proc_t is the type "function taking char *, char **, off_t,
int, int, and void * parameters and returning int". You're
trying to declare a function that returns that type. The
compiler is justifiably complaining.

Apparently you want to do this:
static read_proc_t test_proc_read { ... }
However, the C standard explicitly disallows a function
definition that gives the function's type as a typedef,
presumably so that the definition has to include parameter names.
 
R

Roman Mashak

read_proc_t is the type "function taking char *, char **, off_t,
int, int, and void * parameters and returning int". You're
trying to declare a function that returns that type. The
compiler is justifiably complaining.

Then what's the point of declaring 'read_proc_t' if I can't use it?
 
B

Ben Pfaff

Roman Mashak said:
Then what's the point of declaring 'read_proc_t' if I can't use it?

You can use a pointer to read_proc_t as an object type, or you
can use it to declare (not define) a function.
 
R

Roman Mashak

You can use a pointer to read_proc_t as an object type, or you
can use it to declare (not define) a function.

But function definition and declaration should match, otherwise compiler
will complain.
 
B

Bill Pursell

You can use it...just not in the definition of the function.
For example, you can use it to describe a member
of a structure.
But function definition and declaration should match, otherwise compiler
will complain.

consider:

[tmp]$ cat a.c
#include "a.h"

int
main(void)
{
char *start="start";
int x;
return foo( "page", &start, 0, 0, &x, &x );
}
[tmp]$ cat a.h
#define _XOPEN_SOURCE /* for off_t */
#include <unistd.h>

typedef int (read_proc_t)(char *page, char **start, off_t off,int
count,
int *eof, void *data);

read_proc_t foo;
[tmp]$ cat foo.c

#define _XOPEN_SOURCE
#include <unistd.h>

int
foo(char *p, char **s, off_t o, int c, int e, void *d)
{
return 0;
}
 
F

Fred Kleinschmidt

Roman Mashak said:
Then what's the point of declaring 'read_proc_t' if I can't use it?

You don't say how you really intend to use this, but I think you
probably want to define your function as:

int test_proc(char *page, char **start, off_t off int count,
int *eof, void *data)
{
...
}

then you CAN set
test_entry->read_proc = test_proc_read;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top