[英]Function with pointer arguments ANSI C
I tried many different ways - and none seems to be the right one... 我尝试了许多不同的方法-似乎没有一个是正确的方法...
I want to pass the pointers up, down, left, right - so that I can change their values in the called function. 我想向上,向下,向左,向右传递指针,以便可以在调用的函数中更改它们的值。 Somehow I can't figure out the right syntaxes.
我不知何故找不到正确的语法。 And I couldn't find it either - as in I did try the suggestions I found in my textbooks and on the net, but... no luck.
而且我也找不到它-就像我确实尝试了在教科书和网上找到的建议一样,但是...没有运气。
//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;
>>>
Please, help... of any kind? 请帮助...任何形式的? Thank you...
谢谢...
Whenever you need a function to modify its parameter, keep the following in mind: 每当您需要一个函数来修改其参数时,请记住以下几点:
Here's a code snippet to demonstrate. 这是一个演示代码段。 It defines a function that sets its parameter to 100. It then calls this function on a variable whose value was 0, and prints the value of this variable after the call to show that it indeed was modified.
它定义了一个将其参数设置为100的函数。然后,对一个值为0的变量调用此函数,并在调用后打印此变量的值,以表明确实对其进行了修改。
int f(int* x) {
*x = 100;
}
int main(void) {
int q = 0;
f(&q);
printf("%d\n", q);
return 0;
}
I hope this helps. 我希望这有帮助。
Thanks - I found what I was doing wrong, after reading... everything, I didn't read - all times - the only thing that was wrong - and didn't even look at how I declared the variables in the first place. 谢谢-在阅读完所有内容后,我发现自己在做错事情,所有的事情我都没看过-唯一的错误是-甚至根本没有看我如何声明变量。 "Because it is so simple".
“因为它是如此简单”。
in "main": 在“主要”中:
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...
I got totally confused by the errors and warnings - and never got back to the declaration line. 我对错误和警告完全感到困惑-从未回到声明行。
I'm sorry to have waisted your time. 很抱歉花了您很多时间。 Thanks, everyone.
感谢大家。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.