简体   繁体   English

将静态变量作为参数传递给函数

[英]Passing static variables as arguments to a function

Is there any problem with passing a static variable to a function as a parameter? 将静态变量作为参数传递给函数是否有问题? The program printed 1 0. So, are static variables also passed by value as usual? 程序输出10。那么,静态变量也照常按值传递吗?

#include<stdio.h>
int main()
{
    static main;
    int x;
    x=call(main);
    printf("%d %d",x,main);
}
int call(int address)
{
    address++;
    return address;
}

Yes, static variables are passed just like any other variable. 是的,像其他任何变量一样传递静态变量。

But I would expect you to have some compile errors when you name your variable with the same name as the function. 但是,当您使用与函数相同的名称命名变量时,我希望您会遇到一些编译错误。

There's no difference in variable passing with statics. 与静态变量传递没有区别。

Note you need to include the type in your code: static int main; 注意,您需要在代码中包括类型: static int main;

And I wouldn't name a variable as main because that might be a reserved keyword. 而且我不会将变量命名为main因为这可能是保留关键字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM