【问题标题】:Function with pointer arguments ANSI C带有指针参数的函数 ANSI C
【发布时间】:2014-03-11 18:16:48
【问题描述】:

我尝试了许多不同的方法 - 似乎没有一个是正确的......

我想向上、向下、向左、向右传递指针——这样我就可以在被调用函数中更改它们的值。 不知何故,我无法弄清楚正确的语法。 而且我也找不到 - 因为我确实尝试了我在教科书和网上找到的建议,但是......没有运气。

//long find_neighbours( long, long *up, long *down, long *left, long *)
long find_neighbours( long, long, long, long, long);                  
int main (int argc, char *farg[])
{
    ...
    neighbours = find_neighbours(i, &up, &down, &left, &right);
    ...
}

//long find_neighbours( long center_number, long *up, long *down, long *right) 

long find_neighbours( long, long, long, long, long)
long center_number, *up, *down, *left, *right;
{
long x, y, neighbours;
 y = (long)(center_number/Lmax);
 x = center_number - (y*Lmax);

 if( !( x % center_number) ) //lqwa stena
 {
     *left = center_number + Lmax;
     *right = center_number+1;
 }   
 else
 {
    if( !((x+1)%center_number) ) //dqsna stena
    {
        *left = center_number-1;
        *right = center_number-Lmax;
    }
    else
    {   
        *left = center_number - 1;
        *right = center_number + 1;
    }
 }   

 if( y == 0 ) //lqwa stena
 {
     *up = 1 + x;
     *down = Lmax1 + x;
 }
 else
 {   
    if( y == Lmax1 ) //dqsna stena
    {
        *up = x;
        *down = Lmax - 2 +x;
    }
    else
    {   
        *down = center_number - Lmax;
        *up = center_number + Lmax;  
    }
 }   

 neighbours = Numb[*left] + Numb[*right] + Numb[*up] + Numb[*down];

 return (neighbours);
}


>>>$ gcc -lm perco_concCyclingv2.c -o square_clusters

>>>perco_concCyclingv2.c: In function ‘main’:
>>>perco_concCyclingv2.c:183:20: warning: passing argument 2 of ‘find_neighbours’ makes >>>integer from pointer without a cast [enabled by default]
>>>                    neighbours = find_neighbours(i, &up, &down, &left, &right);
 >>>                   ^
>>>perco_concCyclingv2.c:20:6: note: expected ‘long int’ but argument is of type ‘long int >>>**’
>>> long find_neighbours( long, long, long, long, long);
>>>      ^
>>>perco_concCyclingv2.c:183:20: warning: passing argument 3 of ‘find_neighbours’ makes >>>integer from pointer without a cast [enabled by default]
>>>                    neighbours = find_neighbours(i, &up, &down, &left, &right);
>>>                    ^
>>>perco_concCyclingv2.c:20:6: note: expected ‘long int’ but argument is of type ‘long int >>>**’
>>> long find_neighbours( long, long, long, long, long);
>>>      ^
>>>perco_concCyclingv2.c:183:20: warning: passing argument 4 of ‘find_neighbours’ makes >>>integer from pointer without a cast [enabled by default]
>>>                    neighbours = find_neighbours(i, &up, &down, &left, &right);
>>>                    ^
>>>perco_concCyclingv2.c:20:6: note: expected ‘long int’ but argument is of type ‘long int >>>**’
>>> long find_neighbours( long, long, long, long, long);
>>>      ^
>>>perco_concCyclingv2.c:183:20: warning: passing argument 5 of ‘find_neighbours’ makes >>>integer from pointer without a cast [enabled by default]
>>>                    neighbours = find_neighbours(i, &up, &down, &left, &right);
>>>                   ^
>>>perco_concCyclingv2.c:20:6: note: expected ‘long int’ but argument is of type ‘long int >>>**’
>>> long find_neighbours( long, long, long, long, long);
>>>      ^
>>>perco_concCyclingv2.c:235:21: warning: passing argument 2 of ‘find_neighbours’ makes >>>integer from pointer without a cast [enabled by default]
>>>                     neighbours = find_neighbours(i, &up, &down, &left, &right);
>>>                     ^
>>>perco_concCyclingv2.c:20:6: note: expected ‘long int’ but argument is of type ‘long int >>>**’
>>> long find_neighbours( long, long, long, long, long);
>>>      ^
>>>perco_concCyclingv2.c:235:21: warning: passing argument 3 of ‘find_neighbours’ makes >>>integer from pointer without a cast [enabled by default]
>>>                     neighbours = find_neighbours(i, &up, &down, &left, &right);
>>>                     ^
>>>perco_concCyclingv2.c:20:6: note: expected ‘long int’ but argument is of type ‘long int >>>**’
>>>long find_neighbours( long, long, long, long, long);
>>>      ^
>>>perco_concCyclingv2.c:235:21: warning: passing argument 4 of ‘find_neighbours’ makes >>>integer from pointer without a cast [enabled by default]
>>>                     neighbours = find_neighbours(i, &up, &down, &left, &right);
>>>                     ^
>>>perco_concCyclingv2.c:20:6: note: expected ‘long int’ but argument is of type ‘long int >>>**’
>>> long find_neighbours( long, long, long, long, long);
>>>      ^
>>>perco_concCyclingv2.c:235:21: warning: passing argument 5 of ‘find_neighbours’ makes >>>integer from pointer without a cast [enabled by default]
>>>                     neighbours = find_neighbours(i, &up, &down, &left, &right);
>>>                     ^
>>>perco_concCyclingv2.c:20:6: note: expected ‘long int’ but argument is of type ‘long int >>>**’
>>> long find_neighbours( long, long, long, long, long);
>>>      ^
>>>perco_concCyclingv2.c: In function ‘find_neighbours’:
>>>perco_concCyclingv2.c:278:6: error: old-style parameter declarations in prototyped >>>function definition
>>> long find_neighbours( long, long, long, long, long)
>>>      ^
>>>perco_concCyclingv2.c:278:1: error: parameter name omitted
>>>long find_neighbours( long, long, long, long, long)
>>> ^
>>>perco_concCyclingv2.c:278:1: error: parameter name omitted
>>>perco_concCyclingv2.c:278:1: error: parameter name omitted
>>>perco_concCyclingv2.c:278:1: error: parameter name omitted
>>>perco_concCyclingv2.c:278:1: error: parameter name omitted
>>>perco_concCyclingv2.c:283:13: error: ‘center_number’ undeclared (first use in this >>>function)
>>>  y = (long)(center_number/Lmax);
>>>             ^
>>>perco_concCyclingv2.c:283:13: note: each undeclared identifier is reported only once >>>for each function it appears in
>>>perco_concCyclingv2.c:288:7: error: ‘left’ undeclared (first use in this function)
>>>      *left = center_number + Lmax;
>>>       ^
>>>perco_concCyclingv2.c:289:7: error: ‘right’ undeclared (first use in this function)
>>>      *right = center_number+1;
>>>       ^
>>>perco_concCyclingv2.c:307:7: error: ‘up’ undeclared (first use in this function)
>>>      *up = 1 + x;
>>>       ^
>>>perco_concCyclingv2.c:308:7: error: ‘down’ undeclared (first use in this function)
>>>      *down = Lmax1 + x;
>>>

请帮助...任何形式的? 谢谢...

【问题讨论】:

    标签: c function pointers arguments prototype


    【解决方案1】:

    当您需要一个函数来修改其参数时,请记住以下几点:

    1. 声明函数,使其接受变量的类型、指针 (*) 和名称。
    2. 在函数中,为变量分配前缀 *.
    3. 在函数的调用者中,在将变量传递给函数时,在变量前面加上 &。

    这里有一个代码 sn-p 来演示。它定义了一个将其参数设置为 100 的函数。然后它在一个值为 0 的变量上调用该函数,并在调用后打印该变量的值以表明它确实被修改了。

    int f(int* x) {
        *x = 100;
    }
    
    int main(void) {
        int q = 0;
        f(&q);
        printf("%d\n", q);
        return 0;
    }
    

    我希望这会有所帮助。

    【讨论】:

      【解决方案2】:

      谢谢 - 我发现我做错了什么,在阅读之后......一切,我没有阅读 - 一直 - 唯一的错误 - 甚至没有看我如何声明变量第一名。 “因为它是如此简单”。

      在“主要”中:

      long *x, *y, left, right, up, down, xp, yp; // the right way
      
      long *x, *y, *left, *right, *up, *down, *xp, *yp; // the way it was left... 
      

      我完全被错误和警告弄糊涂了 - 并且再也没有回到声明行。

      很抱歉耽误了您的时间。 谢谢大家。

      【讨论】:

        猜你喜欢
        • 2018-01-03
        • 2015-09-03
        • 2013-11-03
        • 2019-10-20
        • 2018-09-20
        • 1970-01-01
        • 2012-09-28
        • 1970-01-01
        • 2018-08-25
        相关资源
        最近更新 更多