arrays of function pointers impossible?

P

parjit

Hi, folks. I was just wondering -- none of my C or C++ manuals give
anything more than a cursory mention of function pointers, but I've
recently discovered them and found them quite useful.

However, apparently it's not possible under gcc, the gnu compiler
(version 4.3.4) to create arrays of function pointers. Can anybody tell
me if this is a compiler bug or a C feature?
 
S

Shao Miller

parjit said:
Hi, folks. I was just wondering -- none of my C or C++ manuals give
anything more than a cursory mention of function pointers, but I've
recently discovered them and found them quite useful.

However, apparently it's not possible under gcc, the gnu compiler
(version 4.3.4) to create arrays of function pointers. Can anybody tell
me if this is a compiler bug or a C feature?

Have you any thoughts on following-up with your last thread? ("How can
I access those memory areas which do NOT belong to me ?") I would
personally appreciate reading from you about if any of the responses
helped you.

An array of function pointers is possible to construct in GCC; I use
them all the time. I might have even used them in version 4.3.4; it
rings a bell with the Ubuntu distribution of Linux.

How about pasting your code, or a simple test case of that demonstrates
the C-related problem you are experiencing?
 
S

Seebs

Hi, folks. I was just wondering -- none of my C or C++ manuals give
anything more than a cursory mention of function pointers, but I've
recently discovered them and found them quite useful.
However, apparently it's not possible under gcc, the gnu compiler
(version 4.3.4) to create arrays of function pointers. Can anybody tell
me if this is a compiler bug or a C feature?

It is not a compiler bug *or* a language feature; it is a user error.

I tell you this because I use arrays of function pointers under gcc
all the time, every version from about 1.63 on to 4.4.x. They work fine.
If you think they are not working fine, you have done something wrong.

-s
 
P

parjit

Keith said:
Neither. It's certainly possible to create arrays of function pointers.
What makes you think it's not?

What did you try, and *exactly* how did it not work?

I tried making an array of function pointers returning an int. I got a
compile error

error: declaration of ‘f’ as array of functions

Maybe this feature was only implemented in a later version of gcc...
 
F

Faried Nawaz

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

I tried making an array of function pointers returning an int. I got a
compile error

error: declaration of ‘f’ as array of functions

Consider installing cdecl:

cdecl> declare fp as array of pointer to function (int) returning void
void (*fp[])(int )
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkyKk2oACgkQWVufAqdzMWOXiACfdAx8yL8267laKI+WmOQTFODj
7zwAnjOrT0hBnS0ZsRv91YI2l7e6TGxY
=6/G9
-----END PGP SIGNATURE-----
 
D

Dann Corbit

Hi, folks. I was just wondering -- none of my C or C++ manuals give
anything more than a cursory mention of function pointers, but I've
recently discovered them and found them quite useful.

However, apparently it's not possible under gcc, the gnu compiler
(version 4.3.4) to create arrays of function pointers. Can anybody tell
me if this is a compiler bug or a C feature?


#include<math.h>
#include<stdio.h>
typedef double (*f_t) (double);
static f_t f[] = {log, log10, sqrt, cos, cosh, exp, sin, sinh, tan,
tanh, 0};
int main(void)
{
int i;
f_t *flist = f;
for (i = 0; f; i++) {
printf("function %d of 0.5 = %g\n", i, f (0.5));
printf("function %d of 0.5 = %g\n", i, (*f) (0.5));
}
while (*flist) {
f_t ff = *flist;
printf("current function of 0.5 = %g\n", ff(0.5));
flist++;
} return 0;
}

dcorbit@DCORBIT2008 /c/dannfast/e_drive/tmp/ioccc
$ gcc -W -Wall -ansi -pedantic tt.c

dcorbit@DCORBIT2008 /c/dannfast/e_drive/tmp/ioccc
$ ./a
function 0 of 0.5 = -0.693147
function 0 of 0.5 = -0.693147
function 1 of 0.5 = -0.30103
function 1 of 0.5 = -0.30103
function 2 of 0.5 = 0.707107
function 2 of 0.5 = 0.707107
function 3 of 0.5 = 0.877583
function 3 of 0.5 = 0.877583
function 4 of 0.5 = 1.12763
function 4 of 0.5 = 1.12763
function 5 of 0.5 = 1.64872
function 5 of 0.5 = 1.64872
function 6 of 0.5 = 0.479426
function 6 of 0.5 = 0.479426
function 7 of 0.5 = 0.521095
function 7 of 0.5 = 0.521095
function 8 of 0.5 = 0.546302
function 8 of 0.5 = 0.546302
function 9 of 0.5 = 0.462117
function 9 of 0.5 = 0.462117
current function of 0.5 = -0.693147
current function of 0.5 = -0.30103
current function of 0.5 = 0.707107
current function of 0.5 = 0.877583
current function of 0.5 = 1.12763
current function of 0.5 = 1.64872
current function of 0.5 = 0.479426
current function of 0.5 = 0.521095
current function of 0.5 = 0.546302
current function of 0.5 = 0.462117
 
I

Ian Collins

I tried making an array of function pointers returning an int. I got a
compile error

error: declaration of ‘f’ as array of functions

Maybe this feature was only implemented in a later version of gcc...

What did you try? Post it! Arrays of function pointers have been
around since the dark ages.
 
S

Seebs

I tried making an array of function pointers returning an int. I got a
compile error
error: declaration of ???f??? as array of functions

Then you did not declare an array of function pointers, you declared
an array of functions.
Maybe this feature was only implemented in a later version of gcc...

It is quite possible that the feature has been implemented for longer
than you've been alive.

-s
 
L

lawrence.jones

parjit said:
I tried making an array of function pointers returning an int. I got a
compile error

error: declaration of `f' as array of functions

Clearly, your declaration is incorrect: you tried to declare an array of
functions rather than an array of function pointers. You probably left
out an important set of parentheses:

int *f[2](void); // WRONG: array of functions returning int *
int (*g[2])(void); // RIGHT: array of pointers to functions returning int
 
P

parjit

Ian said:
What did you try? Post it! Arrays of function pointers have been
around since the dark ages.

Here is some testcode.

main()
{
int (*f)[2]();
f[0]=strlen;
f[1]=strcmp;
printf("%d\n%d\n",(f[0])("hello"),(f[1])("abc","abd"));
}
 
I

Ian Collins

Here is some testcode.

main()

Are you trolling?
{
int (*f)[2]();
f[0]=strlen;
f[1]=strcmp;
printf("%d\n%d\n",(f[0])("hello"),(f[1])("abc","abd"));
}

Um,

cc /tmp/x.c
"/tmp/x.c", line 1: warning: old-style declaration or incorrect type
for: main
"/tmp/x.c", line 3: cannot declare array of functions or void
"/tmp/x.c", line 4: undefined symbol: strlen
"/tmp/x.c", line 4: left operand must be modifiable lvalue: op "="
"/tmp/x.c", line 5: undefined symbol: strcmp
"/tmp/x.c", line 5: left operand must be modifiable lvalue: op "="
"/tmp/x.c", line 6: warning: implicit function declaration: printf
"/tmp/x.c", line 6: function designator is not of function type
"/tmp/x.c", line 6: function designator is not of function type
"/tmp/x.c", line 7: cannot recover from previous errors

Not bad for 5 lines.

Try adding some sanity:

#include <stdio.h>
#include <string.h>

int main()
{
typedef int (*fn)();

fn f[2];
f[0]=strlen;
f[1]=strcmp;
printf("%d\n%d\n",(f[0])("hello"),(f[1])("abc","abd"));
}

cc /tmp/x.c
"/tmp/x.c", line 9: warning: assignment type mismatch:
pointer to function() returning int "=" pointer to function(pointer to
const char) returning unsigned int

So you are attempting to create an array of heterogeneous function
pointers, which isn't possible.
 
D

Dann Corbit

Ian said:
What did you try? Post it! Arrays of function pointers have been
around since the dark ages.

Here is some testcode.

main()
{
int (*f)[2]();
f[0]=strlen;
f[1]=strcmp;
printf("%d\n%d\n",(f[0])("hello"),(f[1])("abc","abd"));
}

dcorbit@DCORBIT2008 /c/tmp
$ cat t.c
#include <stdio.h>
#include <string.h>

typedef int (*prototypeless_int_function)();
static prototypeless_int_function f[] = {strlen, strcmp, 0};

int main(void)
{
printf ("%d\n%d\n", (f[0]) ("hello"), (f[1]) ("abc", "abd"));
return 0;
}

dcorbit@DCORBIT2008 /c/tmp
$ gcc -W -Wall -ansi -pedantic t.c
t.c:5:1: warning: initialization from incompatible pointer type

dcorbit@DCORBIT2008 /c/tmp
$ ./a.exe
5
-1

dcorbit@DCORBIT2008 /c/tmp
$
 
A

August Karlstrom

Here is some testcode.

main()
{
int (*f)[2]();
f[0]=strlen;
f[1]=strcmp;
printf("%d\n%d\n",(f[0])("hello"),(f[1])("abc","abd"));
}

As others have mentioned in this thread you need to put the array
subscript inside the parentheses surrounding f. You can make the
declaration of f easier to read by using typedef:

typedef int (*F)(void);

F f[2];

Note that strlen and strcmp have different signatures and cannot be
assigned as elements of f.


/August
 
M

Mark Bluemel

Hi, folks.  I was just wondering -- none of my C or C++ manuals give
anything more than a cursory mention of function pointers, but I've
recently discovered them and found them quite useful.

which manuals? Perhaps you need some better ones
 
K

Kenny McCormack

August Karlstrom said:
Note that strlen and strcmp have different signatures and cannot be
assigned as elements of f.

I think that's really the point - that since, in fully "standard C"
(the only thing we are allowed to discuss here), you need a different
type for every different function "signature", and, of course, an array
cannot store heterogeneous types, it thus *is* effectively true, as the
OP originally claimed, that you can't have arrays of function pointers
in C.

--
(This discussion group is about C, ...)

Wrong. It is only OCCASIONALLY a discussion group
about C; mostly, like most "discussion" groups, it is
off-topic Rorsharch [sic] revelations of the childhood
traumas of the participants...
 
R

Ralf Damaschke

They can, it requires just a cast.
I think that's really the point - that since, in fully "standard C"
(the only thing we are allowed to discuss here), you need a different
type for every different function "signature", and, of course, an array
cannot store heterogeneous types, it thus *is* effectively true, as the
OP originally claimed, that you can't have arrays of function pointers
in C.

Wrong again - as always.

-- Ralf
 
R

Ralf Damaschke

Ian said:
Try adding some sanity:

#include <stdio.h>
#include <string.h>

int main()
{
typedef int (*fn)();

fn f[2];
f[0]=strlen;
f[1]=strcmp;
printf("%d\n%d\n",(f[0])("hello"),(f[1])("abc","abd"));
}

cc /tmp/x.c
"/tmp/x.c", line 9: warning: assignment type mismatch:
pointer to function() returning int "=" pointer to
function(pointer to
const char) returning unsigned int

So you are attempting to create an array of heterogeneous function
pointers, which isn't possible.

Make that;
f[0] = (int (*)())strlen;
and
printf("%d\n", (int)((size_t (*)())f[0])("hello"));

and all is just fine.

-- Ralf
 
I

Ian Collins

Ian said:
Try adding some sanity:

#include<stdio.h>
#include<string.h>

int main()
{
typedef int (*fn)();

fn f[2];
f[0]=strlen;
f[1]=strcmp;
printf("%d\n%d\n",(f[0])("hello"),(f[1])("abc","abd"));
}

cc /tmp/x.c
"/tmp/x.c", line 9: warning: assignment type mismatch:
pointer to function() returning int "=" pointer to
function(pointer to
const char) returning unsigned int

So you are attempting to create an array of heterogeneous function
pointers, which isn't possible.

Make that;
f[0] = (int (*)())strlen;
and
printf("%d\n", (int)((size_t (*)())f[0])("hello"));

and all is just fine.

Well yes, just about anything can be made to work if you lie to the
compiler.
 
K

Kenny McCormack

Ian said:
Try adding some sanity:

#include<stdio.h>
#include<string.h>

int main()
{
typedef int (*fn)();

fn f[2];
f[0]=strlen;
f[1]=strcmp;
printf("%d\n%d\n",(f[0])("hello"),(f[1])("abc","abd"));
}

cc /tmp/x.c
"/tmp/x.c", line 9: warning: assignment type mismatch:
pointer to function() returning int "=" pointer to
function(pointer to
const char) returning unsigned int

So you are attempting to create an array of heterogeneous function
pointers, which isn't possible.

Make that;
f[0] = (int (*)())strlen;
and
printf("%d\n", (int)((size_t (*)())f[0])("hello"));

and all is just fine.

Well yes, just about anything can be made to work if you lie to the
compiler.

I could state that the sky is blue and the regs would pounce in to tell
me that I'm wrong.

In fact, let's count the responses from now until they do (i.e., until
one of them does).

--
"The anti-regulation business ethos is based on the charmingly naive notion
that people will not do unspeakable things for money." - Dana Carpender

Quoted by Paul Ciszek (pciszek at panix dot com). But what I want to know
is why is this diet/low-carb food author doing making pithy political/economic
statements?

Nevertheless, the above quote is dead-on, because, the thing is - business
in one breath tells us they don't need to be regulated (which is to say:
that they can morally self-regulate), then in the next breath tells us that
corporations are amoral entities which have no obligations to anyone except
their officers and shareholders, then in the next breath they tell us they
don't need to be regulated (that they can morally self-regulate) ...
 

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,954
Messages
2,570,114
Members
46,702
Latest member
VernitaGow

Latest Threads

Top