c - 如何将 struct * 作为函数的参数

标签 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;
  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 resistorparameter()之后功能。
您可以通过移动 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/

相关文章:

c - 如果我们释放线程的指针,线程会发生什么

c - 从字符串打印字符值时出错

C结构;克服将字符串传递给 char

c - 静态分配的数组作为 C 中的函数参数

c++ - C 中的 fork()、共享内存和指针

c - 基本框图未显示

c - 全局变量编译错误

c - 如何在我的 makelfile 中包含 (GPIO) 库? (在树莓派上)

c - 如何在 STM32 上启动和停止定时器?

c - 确定与等价函数的一对一对应关系的算法