function prototypes and declarations

G

goldfita

I have the following two prototypes in separate source files. Both
programs compile and work correctly.

(1) void general_foo(void (*foo)(), int opt, ...);
(2) static int __wait_get_general(struct ast_connection *conn, int
(*_get)(), int opt, ...);

When I compile the second source file, gcc says
ast_man.c:47: warning: function declaration isn't a prototype

There are no warnings from compiliation of the first file. I don't
understand what the problem is. Even more confusing, I don't
understand what's different. I'm guessing it has something to do with
the function pointers since all the rest of my prototypes were accepted
by the compiler. The function pointers don't declare arguments because
the number and types are unknown until runtime.
 
J

jacob navia

(e-mail address removed) a écrit :
I have the following two prototypes in separate source files. Both
programs compile and work correctly.

(1) void general_foo(void (*foo)(), int opt, ...);
(2) static int __wait_get_general(struct ast_connection *conn, int
(*_get)(), int opt, ...);

When I compile the second source file, gcc says
ast_man.c:47: warning: function declaration isn't a prototype

There are no warnings from compiliation of the first file. I don't
understand what the problem is. Even more confusing, I don't
understand what's different. I'm guessing it has something to do with
the function pointers since all the rest of my prototypes were accepted
by the compiler. The function pointers don't declare arguments because
the number and types are unknown until runtime.

My gcc has no problems with that... Maybe you did not define the
structure ast_connection?

[gateway]# cat tg1.c
struct ast_connection
{ int a;}; // Note that I define the struct ast_connection

static int __wait_get_general(struct ast_connection *conn, int
(*_get)(), int opt, ...);

[gateway]# gcc -c tg1.c
[gateway]# ls -l tg1.o
-rw-r--r-- 1 root root 702 May 13 06:51 tg1.o
[gateway]#
 
M

Michael Mair

jacob said:
(e-mail address removed) a écrit :
I have the following two prototypes in separate source files. Both
programs compile and work correctly.

(1) void general_foo(void (*foo)(), int opt, ...);
(2) static int __wait_get_general(struct ast_connection *conn, int
(*_get)(), int opt, ...);

When I compile the second source file, gcc says
ast_man.c:47: warning: function declaration isn't a prototype

There are no warnings from compiliation of the first file. I don't
understand what the problem is. Even more confusing, I don't
understand what's different. I'm guessing it has something to do with
the function pointers since all the rest of my prototypes were accepted
by the compiler. The function pointers don't declare arguments because
the number and types are unknown until runtime.

My gcc has no problems with that... Maybe you did not define the
structure ast_connection?

[gateway]# cat tg1.c
struct ast_connection
{ int a;}; // Note that I define the struct ast_connection

A forward declaration is entirely sufficient for a prototype
declaration:
struct ast_connection;
as only a struct ast_connection * is passed.
static int __wait_get_general(struct ast_connection *conn, int
(*_get)(), int opt, ...);

[gateway]# gcc -c tg1.c
[gateway]# ls -l tg1.o
-rw-r--r-- 1 root root 702 May 13 06:51 tg1.o
[gateway]#

The same holds even for
gcc -std=c89 -pedantic -Wall -O prot.c -c
and
gcc -std=c99 -pedantic -Wall -O prot.c -c
which only warns about the static declaration not
followed by a use.

Cheers
Michael
 
G

Guest

I have the following two prototypes in separate source files. Both
programs compile and work correctly.

(1) void general_foo(void (*foo)(), int opt, ...);
(2) static int __wait_get_general(struct ast_connection *conn, int
(*_get)(), int opt, ...);

When I compile the second source file, gcc says
ast_man.c:47: warning: function declaration isn't a prototype

There are no warnings from compiliation of the first file. I don't
understand what the problem is. Even more confusing, I don't
understand what's different. I'm guessing it has something to do with
the function pointers since all the rest of my prototypes were accepted
by the compiler. The function pointers don't declare arguments because
the number and types are unknown until runtime.

Well, the declarations are valid (ignoring the reserved identifier
issue), but any compiler may give warnings for any valid code; it
doesn't mean there's an actual problem. if you have a reason to use
non-prototyped function pointers, either tell your compiler to not
complain about it, or ignore the warnings. (In gcc's case, this warning
isn't enabled by default, and both generate the warning with
-Wstrict-prototypes for me, so I'm guessing you simply used different
compiler options for file 1 and file 2.)
 
G

goldfita

jacob said:
My gcc has no problems with that... Maybe you did not define the
structure ast_connection?

[gateway]# cat tg1.c
struct ast_connection
{ int a;}; // Note that I define the struct ast_connection

static int __wait_get_general(struct ast_connection *conn, int
(*_get)(), int opt, ...);

[gateway]# gcc -c tg1.c
[gateway]# ls -l tg1.o
-rw-r--r-- 1 root root 702 May 13 06:51 tg1.o
[gateway]#

I compiled with Wall and Wstrict-prototypes (gcc 3.3.6). The struct is
defined.

http://svn.sourceforge.net/viewcvs.cgi/pyastman/ast_man.h?view=markup
 
G

goldfita

Harald said:
Well, the declarations are valid (ignoring the reserved identifier
issue), but any compiler may give warnings for any valid code; it
doesn't mean there's an actual problem. if you have a reason to use
non-prototyped function pointers, either tell your compiler to not
complain about it, or ignore the warnings. (In gcc's case, this warning
isn't enabled by default, and both generate the warning with
-Wstrict-prototypes for me, so I'm guessing you simply used different
compiler options for file 1 and file 2.)

Yes! That's it. Ok, so there's nothing wrong with it. That's all I
wanted to hear.
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top