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
我使用一个简单的教程定义了函数指针。 Function Software返回一个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.
警告引用section数组中的行。
So I have two doubts:
所以我有两个疑问:
- Am I misusing the */&/neither for the pointer?I have tried combinations of each and i still seem to get the same warning.I am aware of the meaning of each, just unsure how they apply in this particular instance.
-
Can I declare an instance of the
infosection struct
in an array as i do here?I have seen many examples where people declare theirarray of struct
in afor loop
, However my final product requires a long list of structs to be contained in that array.I would like to make my codeportable
such that people canadd 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结构的实例吗?我已经看到很多例子,人们在for循环中声明它们的struct数组,但是我的最终产品需要包含在该数组中的一长串结构。我想让我的代码可移植,以便人们可以像看到的那样以简单的列表方式添加结构(与要指向的函数对应的字符串)。这是声明数组实例的唯一方法
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.
为了澄清,我对结构,结构数组和函数指针做了大量研究。似乎3的组合造成了麻烦。
3 个解决方案
#1
7
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 {
}
.
去掉 *。并用{}替换inner()。
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(),如下所示;
strcpy(section[0].str, "software");
#2
2
The struct member is currently a char*, change to the correct pointer type for your function signature.
struct成员当前是char *,更改为函数签名的正确指针类型。
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.
*取消引用指针,将* foo读为“foo指向的内容”。您希望将struct成员设置为实际指针,也可以使用bracets而不是paranthesis。
infosection section[] = {
{"Software", SoftwarePtr},
{"Hardware", HardwarePtr}
}
#3
1
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)
我的道歉... c停止和c ++开始在我的脑海中变得模糊,我已经看到c的解决方法看起来像这样(但不完全是,我确定一些指针的东西混淆但这是想法)
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);
#1
7
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 {
}
.
去掉 *。并用{}替换inner()。
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(),如下所示;
strcpy(section[0].str, "software");
#2
2
The struct member is currently a char*, change to the correct pointer type for your function signature.
struct成员当前是char *,更改为函数签名的正确指针类型。
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.
*取消引用指针,将* foo读为“foo指向的内容”。您希望将struct成员设置为实际指针,也可以使用bracets而不是paranthesis。
infosection section[] = {
{"Software", SoftwarePtr},
{"Hardware", HardwarePtr}
}
#3
1
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)
我的道歉... c停止和c ++开始在我的脑海中变得模糊,我已经看到c的解决方法看起来像这样(但不完全是,我确定一些指针的东西混淆但这是想法)
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);