【问题标题】:Error expected ) while passing a pointer member of a structure to a function预期错误),同时将结构的指针成员传递给函数
【发布时间】:2019-06-16 14:20:57
【问题描述】:

我是 C 的初学者。在我的程序中,我有一个结构和一个函数。我正在尝试将结构中存在的指针作为函数中的参数传递。但它在点运算符处显示错误“预期)”。这很令人困惑,因为我的函数的其他参数也来自结构,但在这些参数中没有看到这个错误。

我尝试将函数的返回类型更改为所有类型,但仍然没有。

struct signal
{
bool *input;
int previousop;
int n;
}s; //my structure
void noiseremove(bool *input, int n, int count1, int count0, bool 
previousop)//function i declared and defined before main function
{//my function here}
void main()
{
void noiseremove(bool *s.input , int s.n, int s.count1, int s.count0, bool 
s.previousop); //this is where i call the function and facing an error at 
*s.input 
}

我不确定我在哪里出错或语法是否错误。我希望函数接受参数,但事实并非如此。

【问题讨论】:

  • “我不确定我哪里出错了” - void noiseremove(bool *s.input , int s.n, int s.count1, int s.count0, bool s.previousop); 是错误的。那不是函数调用;那是一个构造不当的原型。如果语法错误,它将无法编译(这也不会)。如果逻辑错误(但语法正确,但事实并非如此),它要么无法正确运行,要么在最坏的情况下,似乎 会正确运行,但这只是因为未定义的行为就是这样;不明确的。转到您的书/教程并查找如何调用函数。
  • 调用函数时,应该是-noiseremove(s.input, s.n, s.count1, s.count0, s.previousop);调用函数时不应定义参数类型等。

标签: c function pointers structure


【解决方案1】:

在另一个函数中包含函数是not possible in C...

所以,你的代码应该有点像这样:

struct signal
{
    bool *input, previousop;
    int n, count0, count1;
} s;

void noiseremove(bool *input, int n, int count1, int count0, bool previousop)
{
    /* Try using multi-line comments since single-line comments can comment out the end
       braces as well...*/
}

void main()
{
    /* Initialize the structure before accessing any of its variables, or it will lead
       to undefined behavior! */
    s = {0};
    /* Don't declare the identifiers again... It is 'syntax error' and 
       your calling convention doesn't in the least look like a calling 
       convention but more like a function declaration with invalid identifiers
       for the parameters... */
    noiseremove(s.input , s.n, s.count1, s.count0, s.previousop);
}

【讨论】:

    【解决方案2】:
    struct signal
    {
        bool *input, previousop;
        int n, count1, count0;
    } s; //my structure
    
    void noiseremove(bool *input, int n, int count1, int count0, bool previousop) //function i declared and defined before main function
    {
        // my function here
    }
    void main()
    {
        // this should compile but s has not been initialized
        noiseremove(s.input , s.n, s.count1, s.count0, s.previousop);
        // not sure what you were going for here
        //*s.input 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-08-01
      • 1970-01-01
      • 2020-05-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多