如何将struct*作为函数的参数
我想通过alloc作为一个论点,但不知道如何通过,有人能帮我吗如何将struct*作为函数的参数,c,C,我想通过alloc作为一个论点,但不知道如何通过,有人能帮我吗 void parameter(unsigned int argnum, struct resistor* alloc) { /*...*/ } struct resistort { const double E6[6]; double E12[12]; const double E24[24]; char e[3]; double value; double log10val; double val;
void parameter(unsigned int argnum, struct resistor* alloc)
{
/*...*/
}
struct resistort
{
const double E6[6];
double E12[12];
const double E24[24];
char e[3];
double value;
double log10val;
double val;
double serielval[2];
double reset;
}rv;
int main(int argc, char *argv[])
{
struct resistor *alloc = NULL;
alloc = (struct resistor *)malloc(sizeof(struct resistor));
parameter(argc, alloc);
}
在参数中,我要释放(alloc)
我希望它能这样运作:
void parameter(unsigned int argnum, struct resistor* alloc);
但我明白了
warning: passing argument 2 of 'parameter' from incompatible pointer type [-Wincompatible-pointer-types]|
note: expected 'struct resistor *' but argument is of type 'struct resistor *'
error: conflicting types for 'parameter'
由于在声明之前使用了结构电阻器,您将收到警告
不兼容指针类型
:
void parameter(unsigned int argnum, struct resistor* alloc)
^^^^^^^^^^^^^^^
在您的程序中,struct resistor
的声明在parameter()
函数之后。您可以通过在函数
参数()
之前移动结构电阻
声明来解决此问题,或者只需在参数()
函数之前向前声明结构电阻
,如下所示:
struct resistor; //forward declaration
void parameter(unsigned int argnum, struct resistor* alloc)
{
/*...*/
}
struct resistor
{
const double E6[6];
double E12[12];
const double E24[24];
char e[3];
double value;
double log10val;
double val;
double serielval[2];
double reset;
}rv;
int main(int argc, char *argv[])
{
struct resistor *alloc = NULL;
alloc = (struct resistor *)malloc(sizeof(struct resistor));
parameter(argc, alloc);
}
结构电阻器的定义在哪里?
参数
函数在哪里声明?请尝试创建一个显示给我们的。另外,请在构建时包含完整输出的副本粘贴(您显示的只是较大消息的较小注释)。请阅读,以及…你没有收到任何警告吗?如果我在编译器资源管理器中运行此命令,我会收到警告“警告:在此定义或声明之外,在参数列表中声明的:'struct resistor'将不可见”
@KamilCuk您的结构定义在函数参数之前struct resistor
必须是struct resistor
?