Results 1 to 3 of 3
Hello
I am trying to make my function pointer assignment program works. But facing some problem in compilation.
>>START FILE mytest.h
#ifndef WIBBLE_INC
#define WIBBLE_INC
struct mystruct
{
int (*add)(int,int);
...
- 08-30-2011 #1Just Joined!
- Join Date
- Jul 2008
- Posts
- 9
A question on C
Hello
I am trying to make my function pointer assignment program works. But facing some problem in compilation.
>>START FILE mytest.h
#ifndef WIBBLE_INC
#define WIBBLE_INC
struct mystruct
{
int (*add)(int,int);
int (*sub)(int,int);
};
extern const struct mystruct mstr;
#endif
<< END OF FILE mytest.h
>>START FILE mytest.c
include "mytest.h"
static int add(int x, int y)
{
return x+y;
}
static int sub(int x, int y)
{
return x-y;
}
const struct mystruct mstr={add,sub};
<<END OF FILE mytest.c
This while compiling I am getting the following error
cc -c -o mytest.o mytest.c
mytest.c:2: error: expected â=â, â,â, â;â, âasmâ or â__attribute__â before string constant
mytest.c:15: error: variable âmstrâ has initializer but incomplete type
mytest.c:15: error: âaddâ undeclared here (not in a function)
mytest.c:15: warning: excess elements in struct initializer
mytest.c:15: warning: (near initialization for âmstrâ)
mytest.c:15: warning: excess elements in struct initializer
mytest.c:15: warning: (near initialization for âmstrâ)
What could be the problem
thanks in advance
~SLast edited by salilgk; 08-30-2011 at 03:29 AM.
- 08-30-2011 #2Linux Newbie
- Join Date
- Mar 2010
- Posts
- 121
Where you have "include" you should have "#include" (i.e. put a hash sign at the start of the line). Also, where you need square brackets after "mystr" (i.e. "mystr[]") to declare it as an array (assuming you want to do that).
- 08-30-2011 #3Just Joined!
- Join Date
- Jul 2008
- Posts
- 9
oops .. my bad ... yes # was missing in the include statement ... switching between scripting and compiled programming makes life tough
thanks for the pointers


Reply With Quote