Reputation: 75
I am trying to initialize an array of structs. the structs have function pointers in them and are defined as such:
typedef struct{
char str[512];
char *fptr;
} infosection;
then I try to make an array:
infosection section[] = {
("Software", *SoftwarePtr),
("Hardware", *HardwarePtr),
}
I defined the function pointers using a simple tutorial. Function Software returns an int
int (*SoftwarePtr)()=NULL;
SoftwarePtr = &Software;
My question is about the warnings I get upon compiling.
Initialization makes integer from pointer without a cast
The warning references the lines in the section array.
So I have two doubts:
Can I declare an instance of the infosection struct
in an array as i do here?I have seen many examples where people declare their array of struct
in a for loop
,
However my final product requires a long list of structs to be contained in that array.I would like to make my code portable
such that people can add structs
( strings which correspond with functions to point to) in an easy list-like fashion as seen about.
Is this the only way to declare an instance of the array
infosection section[];
section[0].str="software";
section[0].fptr=SoftwarePtr;
Just to clarify, i have done quite a bit of research on structs
, array of structs
, and function pointers
. It just seems that the combination of the 3 is causing trouble.
Upvotes: 5
Views: 5681
Reputation: 58271
One
One mistake I an find, in structure declaration of function pointer is incorrect:
typedef struct{
char str[512];
int (*fptr)(); // not char* fptr
} infosection;
Two
declaration:
infosection section[] = {
("Software", *SoftwarePtr),
("Hardware", *HardwarePtr),
}
Should be:
infosection section[] = {
{"Software", SoftwarePtr},
{"Hardware", HardwarePtr}
}
Remove *
. and replace inner (
)
with {
}
.
three:
infosection section[];
section[0].str="software"; // compiler error
section[0].fptr=SoftwarePtr;
Is wrong you can't assign string in this way. you need to use strcpy()
as follows;
strcpy(section[0].str, "software");
Upvotes: 7
Reputation: 450
been a while since i was playin around with pointers but maybe a constructor would help?
struct infosection{
char str[512];
char *fptr;
infosection(char* strN, char* fptrN): str(strN), fptr(fptrN) {}
};
infosection[] isxn = { new infosection(&str1,fptr1),
new infosection(&str2,fptr2)},
... };
my apologies...where c stops and c++ begins has become blurred in my mind, i have seen a workaround for c that looks like this (but not exactly, im sure some of the pointer stuff is mixed up but this was the idea)
struct infosection{
char str[512];
char *fptr;
};
infosection* myIS (char *strN, char *fptrN) {
infosection i = new infosection();
i.str = &strN;
i.fptr = fptrN;
return *i;
}
//infosection* newInfoSection = myIS(myStrPtr,myFptr);
Upvotes: 1
Reputation: 4154
The struct member is currently a char*, change to the correct pointer type for your function signature.
typedef struct{
char str[512];
int (*fptr)();
} infosection;
* dereference the pointer, read *foo as "what foo points at". You want to set the struct member to the actual pointer, also use bracets instead of paranthesis.
infosection section[] = {
{"Software", SoftwarePtr},
{"Hardware", HardwarePtr}
}
Upvotes: 2