我想将 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;
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);
}
在参数中我想释放(分配)
我希望它能这样运作:
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'
最佳答案
您收到警告 incompatible pointer type
因为您正在使用 struct resistor
声明前:
void parameter(unsigned int argnum, struct resistor* alloc)
^^^^^^^^^^^^^^^
在您的程序中,声明 struct resistor
在parameter()
之后功能。
您可以通过移动 struct resistor
来解决此问题函数之前的声明parameter()
或者只是做 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);
}
关于c - 如何将 struct * 作为函数的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54051066/