HELP: function pointer

E

emanuela

Hi,
I've a little question about C language:

according to the pthread_create signature:
int pthread_create(pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine)(void*),
void *arg);
which syntax is more correct?

1) void* function (void* args) {...}

main()
{ ...
pthread_create(&mythread, NULL, function, NULL)
...
}

2) void* function (void* args) {...}

main()
{ ...
pthread_create(&mythread, NULL, &function, NULL)
...
}

According to me, only the second one is correct
but I succeed in compiling both of them (gcc - Linux Fedora Core 1 (2.4.))
Why?

Thanks

Emanuela
 
S

Simon Massey

emanuela said:
Hi,
I've a little question about C language:
which syntax is more correct?

1) void* function (void* args) {...}

main()
{ ...
pthread_create(&mythread, NULL, function, NULL)
pthread_create(&mythread, NULL, &function, NULL)
...
}

According to me, only the second one is correct
but I succeed in compiling both of them (gcc - Linux Fedora Core 1 (2.4.))
Why?

Both are correct (perhaps &function, slightly more so) as the name of a
function is it's address. The leading & is ignored with function names as
the only thing you can do with a function is call it (using ()) or take it's
address.
 
R

red floyd

Hi,
I've a little question about C language:

according to the pthread_create signature:
int pthread_create(pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine)(void*),
void *arg);
which syntax is more correct?

1) void* function (void* args) {...}

main()
{ ...
pthread_create(&mythread, NULL, function, NULL)
...
}

2) void* function (void* args) {...}

main()
{ ...
pthread_create(&mythread, NULL, &function, NULL)
...
}

Either one is correct. IIRC, in the original K&R, you got a warning
with the second form. The naked name of a function (without parens)
refers to its address.
 
M

Mark A. Odell

(e-mail address removed) (emanuela) wrote in

Hi,
I've a little question about C language:

according to the pthread_create signature:
int pthread_create(pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine)(void*),
void *arg);
which syntax is more correct?

1) void* function (void* args) {...}

main()
{ ...
pthread_create(&mythread, NULL, function, NULL)
...
}

2) void* function (void* args) {...}

main()
{ ...
pthread_create(&mythread, NULL, &function, NULL)
...
}

According to me, only the second one is correct
but I succeed in compiling both of them (gcc - Linux Fedora Core 1
(2.4.)) Why?

Because you are wrong. Similar to how you can use char array[10]; 'array'
as the address of the first element or '&array[0]' so too can you use
'function' or '&function'. Also, your main() is wrong, it must return
'int'. Do not rely on implicit 'returns int' of non-prototyped functions.
 
J

Jack Klein

Both are correct (perhaps &function, slightly more so) as the name of a
function is it's address. The leading & is ignored with function names as
the only thing you can do with a function is call it (using ()) or take it's
address.

No, the name of a function is not an address, anymore than the name of
an array is an address. It is a "function designator", defined as
such by paragraph 4 of 6.3.2.1 of the current C standard:

"A function designator is an expression that has function type. Except
when it is the operand of the sizeof operator(54) or the unary &
operator, a function designator with type ‘‘function returning type’’
is converted to an expression that has type ‘‘pointer to
function returning type’’."

So a function designator is automatically and silently converted to a
function pointer in all contexts except when it is used as the operand
of the sizeof or & operators. The use of the & operator, as when this
operator is used on an lvalue of array type, explicitly converts the
name to a pointer.

The sizeof operator applied to a function designator, unlike applying
it to an lvalue of array type, is invalid, as stated explicitly in the
text of footnote 54, referenced above:

"54) Because this conversion does not occur, the operand of the sizeof
operator remains a function designator and violates the constraint in
6.5.3.4."

So given:

#include <stdio.h>
int func(int x)
{
return x + 1;
}

int (*fptr)(int) = func;

int main(void)
{
printf("size of function pointer %lu\n",
(unsigned long)sizeof(fptr));
printf("size of function %lu\n",
(unsigned long)sizeof(func));
return 0;
}

....a conforming compiler must issue a diagnostic for the sizeof
expression in the second printf() call.

As shown by:

Error c:\prog\lcc\projects\sample\sample.c: 14 invalid type argument
'int function(int)' to 'sizeof'

....from one compiler and this:

Compiling...
sample.c
c:\program files\microsoft visual
studio\myprojects\sample\sample.c(14) : error C2070: illegal sizeof
operand
Error executing cl.exe.

sample.obj - 1 error(s), 0 warning(s)

....from another.
 
E

emanuela

thanks

Emanuela

Mark A. Odell said:
(e-mail address removed) (emanuela) wrote in

Hi,
I've a little question about C language:

according to the pthread_create signature:
int pthread_create(pthread_t *thread,
const pthread_attr_t *attr,
void *(*start_routine)(void*),
void *arg);
which syntax is more correct?

1) void* function (void* args) {...}

main()
{ ...
pthread_create(&mythread, NULL, function, NULL)
...
}

2) void* function (void* args) {...}

main()
{ ...
pthread_create(&mythread, NULL, &function, NULL)
...
}

According to me, only the second one is correct
but I succeed in compiling both of them (gcc - Linux Fedora Core 1
(2.4.)) Why?

Because you are wrong. Similar to how you can use char array[10]; 'array'
as the address of the first element or '&array[0]' so too can you use
'function' or '&function'. Also, your main() is wrong, it must return
'int'. Do not rely on implicit 'returns int' of non-prototyped functions.
 

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
474,143
Messages
2,570,822
Members
47,368
Latest member
michaelsmithh

Latest Threads

Top