array name vs function name

A

aarklon

Hi all,

I have read in Peter Van Der Linden's book array name decays to
pointer to the first element of the array in expressions
except in 3 circumstances namely
(1) sizeof() (2)& (3)string literal initializer

now my question are

1) does the same holds for function names...????

2) is it because of the fact that array name decays to pointer to
first element of the array,that we don't use & whenever reading a
string
using scanf function...????
 
E

Eric Sosman

Hi all,

I have read in Peter Van Der Linden's book array name decays to
pointer to the first element of the array in expressions
except in 3 circumstances namely
(1) sizeof() (2)& (3)string literal initializer

now my question are

1) does the same holds for function names...????

Using sizeof on a function name isn't allowed, and
a function name can't be a string literal initializer,
but for the remaining case, yes: Function name `f' in
an expression yields a pointer to the function, except
in the context `&f'. In that case, `f' is just the name
of the function and `&f' is a pointer to the function.
So the only time `f' does not become a function pointer
is when you apply `&' to it to get ... a function pointer.
2) is it because of the fact that array name decays to pointer to
first element of the array,that we don't use & whenever reading a
string
using scanf function...????

Yes. Read section 6 of the FAQ at http://www.c-faq.com/
 
T

Tomás Ó hÉilidhe

aarklon:
1) does the same holds for function names...????


Whenever you have a function's name in C, it's really just a pointer
to the function. You can never have a function itself (that I know of,
sizeof _might_ be an exception). When you have:

puts("Hello");

, then "puts" is a function pointer. The dereference operator and the
addressof operator have no effect whatsoever on function pointers, as can
be demonstrated from:

(&**&*&*&*&*&*****&*&*&*&***puts)("Hello");


This is why there's two common ways of assigning to a function pointer:

pfunc = EditDatabase;
pfunc = &EditDatabase;


As for applying sizeof to a function... well to be honest it's one of
those things I haven't put thought into because I've never had reason to
get the size of a function. If I was to guess though, I'd say it yields
the size of the function pointer.

Just now though I compiled the following code and it failed, so I can
confirm that I don't know what happens when you apply sizeof to a
function :-D

void Func(void) {}

int main(void)
{
void (*pfunc)(void);
char arr[sizeof Func == sizeof pfunc ? 5 : -5];
return 0;
}
 
F

Francine.Neary

aarklon:


Whenever you have a function's name in C, it's really just a pointer
to the function. You can never have a function itself (that I know of,
sizeof _might_ be an exception).

sizeof isn't a function.
When you have:

puts("Hello");

, then "puts" is a function pointer. The dereference operator and the
addressof operator have no effect whatsoever on function pointers, as can
be demonstrated from:

(&**&*&*&*&*&*****&*&*&*&***puts)("Hello");

This is why there's two common ways of assigning to a function pointer:

pfunc = EditDatabase;
pfunc = &EditDatabase;

As for applying sizeof to a function... well to be honest it's one of
those things I haven't put thought into because I've never had reason to
get the size of a function. If I was to guess though, I'd say it yields
the size of the function pointer.

Just now though I compiled the following code and it failed, so I can
confirm that I don't know what happens when you apply sizeof to a
function :-D

void Func(void) {}

int main(void)
{
void (*pfunc)(void);
char arr[sizeof Func == sizeof pfunc ? 5 : -5];
return 0;

}
 
T

Tomás Ó hÉilidhe

Francine.Neary:
sizeof isn't a function.


You're right, it's an operator. Perhaps I could have structured my
sentence better, but the "that I know of, sizeof might be an exception"
referred to sizeof being applied to a function pointer, rather than
sizeof being a function.
 
B

Barry Schwarz

snip
As for applying sizeof to a function... well to be honest it's one of
those things I haven't put thought into because I've never had reason to
get the size of a function. If I was to guess though, I'd say it yields
the size of the function pointer.

Why guess when you can find out in less than five minutes.
Just now though I compiled the following code and it failed, so I can
confirm that I don't know what happens when you apply sizeof to a
function :-D

Yes you do. Your compiler told you. You just refuse to believe it.
void Func(void) {}

int main(void)
{
void (*pfunc)(void);
char arr[sizeof Func == sizeof pfunc ? 5 : -5];

Why introduce so much complexity which only confuses the issue when
all you really want is
size_t x = sizeof(Func);
return 0;
}


Remove del for email
 
T

Tomás Ó hÉilidhe

Barry Schwarz:
Why guess when you can find out in less than five minutes.


I went on to test it out.

Yes you do. Your compiler told you. You just refuse to believe it.


The tests were inconclusive. All that it showed me was that the
function pointer had a different size to the pointer itself.

void Func(void) {}

int main(void)
{
void (*pfunc)(void);
char arr[sizeof Func == sizeof pfunc ? 5 : -5];

Why introduce so much complexity which only confuses the issue when all
you really want is
size_t x = sizeof(Func);
return 0;
}



Probably because _your_ code doesn't give any output, whereas mine
does because the compiler will tell me about a negative array length. Of
course there's the canonical method of using printf, but that would have
take about an extra ten seconds to code, so I went with the faster option.
 
B

Barry Schwarz

Barry Schwarz:



I went on to test it out.




The tests were inconclusive. All that it showed me was that the
function pointer had a different size to the pointer itself.

You mean your compiler did not generate a diagnostic for the
constraint violation? Were you running without extensions?
void Func(void) {}

int main(void)
{
void (*pfunc)(void);
char arr[sizeof Func == sizeof pfunc ? 5 : -5];

Why introduce so much complexity which only confuses the issue when all
you really want is
size_t x = sizeof(Func);
return 0;
}



Probably because _your_ code doesn't give any output, whereas mine
does because the compiler will tell me about a negative array length. Of

My statement and your declaration both should have produced a
diagnostic because the sizeof operator cannot be applied to a
function.
course there's the canonical method of using printf, but that would have
take about an extra ten seconds to code, so I went with the faster option.


Remove del for email
 
O

Old Wolf

void Func(void) {}
int main(void)
{
void (*pfunc)(void);
char arr[sizeof Func == sizeof pfunc ? 5 : -5];

Why introduce so much complexity which only confuses the issue when
all you really want is
size_t x = sizeof(Func);

That is a constraint violation (the sizeof operator
cannot be applied to an expression that has function
type). C99 6.5.3.4#1
 
B

Barry Schwarz

void Func(void) {}
int main(void)
{
void (*pfunc)(void);
char arr[sizeof Func == sizeof pfunc ? 5 : -5];

Why introduce so much complexity which only confuses the issue when
all you really want is
size_t x = sizeof(Func);

That is a constraint violation (the sizeof operator
cannot be applied to an expression that has function
type). C99 6.5.3.4#1

It is a constraint violation in both statements. My point was that
you could earn the diagnostic with a much simpler statement.


Remove del for email
 

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
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top