constant function pointers declaration

I

InuY4sha

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
 
B

Ben Bacarisse

InuY4sha said:
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.
 
A

Antoninus Twink

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;
 
A

Andrey Tarasevich

InuY4sha said:
#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.
 
Å

志飞 é²

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

#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;
}
 
L

lawrence.jones

Antoninus Twink said:
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.
 
R

Richard Tobin

Antoninus Twink said:
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
 

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,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top