Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

constant function pointers declaration

1 view
Skip to first unread message

InuY4sha

unread,
Nov 24, 2009, 5:44:12 PM11/24/09
to
Hi list,
I already asked something similar: I have this code

#include <stdio.h>
void foo(void){ printf("helloworld\n"); return; }
void (* foo2)(void) = foo;
struct data {void (* fptr)(void);};
static struct data myarray[] = {
{foo},
{foo2}, //THIS must be commented for the code to work!!!!
};
int main(){myarray[0].fptr(); return 0; }

and THIS generates the following error:
test.c:7: error: initializer element is not constant
test.c:7: error: (near initialization for ‘myarray[1].fptr’)

Can I fix it by placing a "const" in the right position?
I was thinking to use "(* const foo2)" but it doesn't change a thing.

Thanks in advance
RM


Ben Bacarisse

unread,
Nov 24, 2009, 6:11:54 PM11/24/09
to
InuY4sha <inuy...@gmail.com> writes:

> I already asked something similar: I have this code
>
> #include <stdio.h>
> void foo(void){ printf("helloworld\n"); return; }
> void (* foo2)(void) = foo;
> struct data {void (* fptr)(void);};
> static struct data myarray[] = {
> {foo},
> {foo2}, //THIS must be commented for the code to work!!!!
> };
> int main(){myarray[0].fptr(); return 0; }
>
> and THIS generates the following error:
> test.c:7: error: initializer element is not constant
> test.c:7: error: (near initialization for ‘myarray[1].fptr’)
>
> Can I fix it by placing a "const" in the right position?

Not in standard C. The permitted forms for an address constant don't
include the value of another object like foo2 no matter how it is
qualified.

> I was thinking to use "(* const foo2)" but it doesn't change a thing.

--
Ben.

Antoninus Twink

unread,
Nov 24, 2009, 6:13:31 PM11/24/09
to
On 24 Nov 2009 at 22:44, InuY4sha wrote:
> struct data {void (* fptr)(void);};
> static struct data myarray[] = {
> {foo},
> {foo2}, //THIS must be commented for the code to work!!!!
> };
>
> and THIS generates the following error:
> test.c:7: error: initializer element is not constant
> test.c:7: error: (near initialization for ‘myarray[1].fptr’)
>
> Can I fix it by placing a "const" in the right position?

No!

The address of a function is not a compile time constant - it doesn't
get assigned until the function is linked (possibly dynamically at
runtime).

Use assignment rather than initialization:

static struct data myarray[2];
myarray[0].fptr = foo;
myarray[1].fptr = foo2;

Andrey Tarasevich

unread,
Nov 24, 2009, 6:14:56 PM11/24/09
to
InuY4sha wrote:
>
> #include <stdio.h>
> void foo(void){ printf("helloworld\n"); return; }
> void (* foo2)(void) = foo;
> struct data {void (* fptr)(void);};
> static struct data myarray[] = {
> {foo},
> {foo2}, //THIS must be commented for the code to work!!!!
> };
> int main(){myarray[0].fptr(); return 0; }
>
> and THIS generates the following error:
> test.c:7: error: initializer element is not constant
> test.c:7: error: (near initialization for �myarray[1].fptr�)

>
> Can I fix it by placing a "const" in the right position?

No.

--
Best regards,
Andrey Tarasevich

志飞 鲍

unread,
Nov 25, 2009, 11:30:29 AM11/25/09
to

#include <stdio.h>

void
foo (void)
{
printf ("helloworld\n");
return;
}

void (* foo2)(void) = foo;

struct data
{
void (* fptr)(void);
};

static struct data myarray[2] = {
{foo},
// {foo2}, //THIS must be commented for the code to work!!!!
};

int
main()
{
myarray[1].fptr = foo2;


myarray[0].fptr();
return 0;
}

---------------------------------------------------------
hi,my english is pool,but this is code ok! it compiled ok by gcc.
and your code compiled ok by g++.use g++ to have a try!

---
baozf

lawrenc...@siemens.com

unread,
Nov 25, 2009, 2:31:09 PM11/25/09
to
Antoninus Twink <nos...@nospam.invalid> wrote:
>
> The address of a function is not a compile time constant

Wrong -- it's an address constant, which qualifies as a constant
expression and is thus valid in a static initializer. The value of a
variable, however, is never a constant expression, not even if the
variable is declared const.
--
Larry Jones

Ha! Wild zontars couldn't drag that information out of me! Do your worst!
-- Calvin

Richard Tobin

unread,
Nov 25, 2009, 4:58:48 PM11/25/09
to
In article <slrnhgoq4r...@nospam.invalid>,
Antoninus Twink <nos...@nospam.invalid> wrote:

>The address of a function is not a compile time constant - it doesn't
>get assigned until the function is linked (possibly dynamically at
>runtime).

It's the job of the linker to fix this up, just as it has to fix up
straightforward calls to the function.

-- Richard
--
Please remember to mention me / in tapes you leave behind.

0 new messages