How to apply sizeof operator on an array-of-pointers-to-functions?

S

Sune

Hi,

I'm stuck at the moment with the following:

I'm writing test functions for a module written in C. Since I'm lazy I
want to type as little as possible. I came up with this idea:

// Declare functions and an array of pointers to those functions
int t1(),t2();
int (*func[])() = { t1,t2 };
:
:
// Execute the functions in a for-loop like
for( int i=0; i<VAR1/VAR2; i++ )
{
if( (*func)() == BAD )
printf("Test nr: %d failed.\n", i );
}

So now I want to set VAR1 to the size of the array and VAR2 to the size
of a function pointer to get the number of test functions to execute.

Can someone, please show me how do I do that? I get compilation errors
when I use the sizeof operator for this.

Thanks in advance
/Sune
 
M

Mike Wahler

Sune said:
Hi,

I'm stuck at the moment with the following:

I'm writing test functions for a module written in C. Since I'm lazy I
want to type as little as possible. I came up with this idea:

// Declare functions and an array of pointers to those functions
int t1(),t2();
int (*func[])() = { t1,t2 };
:
:
// Execute the functions in a for-loop like
for( int i=0; i<VAR1/VAR2; i++ )
{
if( (*func)() == BAD )
printf("Test nr: %d failed.\n", i );
}

So now I want to set VAR1 to the size of the array and VAR2 to the size
of a function pointer to get the number of test functions to execute.

Can someone, please show me how do I do that? I get compilation errors
when I use the sizeof operator for this.


But you don't say what those errors are.

Try this:

#include <stdlib.h>

int t1(void)
{
return 1;
}

int t2(void)
{
return 2;
}

int main()
{
int (*func[])(void) = {t1, t2};

size_t i = 0;

for(i = 0; i < sizeof func / sizeof *func; ++i)
func();

return 0;
}

-Mike
 
S

Sune

Hi Mike,

needeless to say, I'm new to C, but what you suggested worked like a
charm.

Thanks a lot
/Sune
 
C

Clark S. Cox III

Hi,

I'm stuck at the moment with the following:

I'm writing test functions for a module written in C. Since I'm lazy I
want to type as little as possible. I came up with this idea:

// Declare functions and an array of pointers to those functions
int t1(),t2();
int (*func[])() = { t1,t2 };
:
:
// Execute the functions in a for-loop like
for( int i=0; i<VAR1/VAR2; i++ )
{
if( (*func)() == BAD )
printf("Test nr: %d failed.\n", i );
}

So now I want to set VAR1 to the size of the array and VAR2 to the size
of a function pointer to get the number of test functions to execute.

Can someone, please show me how do I do that? I get compilation errors
when I use the sizeof operator for this.


What errors do you get?

You should be able to use:

sizeof func / sizeof *func
 
M

Mike Wahler

Sune said:
Hi Mike,

needeless to say, I'm new to C,

Have you read the C FAQ? I don't recall offhand if it
directly addresses the questions you asked, but imo it
will go a long way toward helping you with learning C.

http://www.eskimo.com/~scs/C-faq/faq.html

Also it's probably a good idea for you to tell us which
textbook(s) you're studying. There are many good ones
available, but just as many or more bad ones which can
hinder your learning.
but what you suggested worked like a
charm.

Glad I could help. If there's anything in my code
you don't understand, feel free to ask about it, and
I and/or someone else will try to explain.

Good luck with your studies.

-Mike
 
E

Emmanuel Delahaye

Sune wrote on 24/09/05 :
Can someone, please show me how do I do that? I get compilation errors
when I use the sizeof operator for this.

Learn to use is correctly:

#include <stdio.h>

#define NELEM(a) (sizeof(a)/sizeof*(a))

static int t1 (void)
{
printf ("t1\n");
return 0;
}

static int t2 (void)
{
printf ("t2\n");
return 0;
}

int main (void)
{
int (*func[]) (void) =
{t1, NULL, t2};

// Execute the functions in a for-loop like
for (int i = 0; i < NELEM (func); i++)
{
if (func == NULL)
{
printf ("Test nr: %d failed.\n", i);
}
else
{
func ();
}
}
return 0;
}

--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Mal nommer les choses c'est ajouter du malheur au
monde." -- Albert Camus.
 
E

Emmanuel Delahaye

(supersedes <[email protected]>)

Sune wrote on 24/09/05 :
Can someone, please show me how do I do that? I get compilation errors
when I use the sizeof operator for this.

So, learn to use it correctly:

#include <stdio.h>

#define NELEM(a) (sizeof(a)/sizeof*(a))

static int t1 (void)
{
printf ("t1\n");
return 0;
}

static int t2 (void)
{
printf ("t2\n");
return 0;
}

int main (void)
{
int (*func[]) (void) =
{t1, NULL, t2};

// Execute the functions in a for-loop like
for (int i = 0; i < NELEM (func); i++)
{
if (func == NULL)
{
printf ("Test nr: %d failed.\n", i);
}
else
{
func ();
}
}
return 0;
}


--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"It's specified. But anyone who writes code like that should be
transmogrified into earthworms and fed to ducks." -- Chris Dollin CLC
 
R

ritesh

Hi,

This solution works fine. I printed the values of
sizeof(func)
and
sizeof(*func)
and they were 8 and 4 on my system.

Could you explain what the they mean... my understanding is -
sizeof(func) - gives the total size of the func pointer array
sizeof(*func) - gives the size of a single value of func pointer array
so when you 8/4 = 2 as expected.

If I'm correct then why dosen't the statment
sizeof(*func[0])
or
sizeof(*func[1])
compile. I get this error with g++ -
ISO C++ forbids applying `sizeof' to a function type

-ritesh
 
K

Keith Thompson

ritesh said:
This solution works fine. I printed the values of
sizeof(func)
and
sizeof(*func)
and they were 8 and 4 on my system.

Could you explain what the they mean... my understanding is -
sizeof(func) - gives the total size of the func pointer array
sizeof(*func) - gives the size of a single value of func pointer array
so when you 8/4 = 2 as expected.

If I'm correct then why dosen't the statment
sizeof(*func[0])
or
sizeof(*func[1])
compile.

We can't tell without seeing how func is declared. I think the
declaration is in a previous article, but you didn't quote it.

One more time:

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
I get this error with g++ -
ISO C++ forbids applying `sizeof' to a function type

C++ is a different language; they discuss it over in comp.lang.c++.
 

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,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top